From 4bfec16ba7dd9be3e538e08c84de76806b86d291 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sat, 18 Feb 2023 21:10:14 -0800 Subject: [PATCH] Physics time --- src/dawn/scene/SceneItemComponent.hpp | 1 + src/dawntictactoe/CMakeLists.txt | 4 +- src/dawntictactoe/components/CMakeLists.txt | 10 ++++ .../components/TicTacToeTile.cpp | 20 +++++++ .../components/TicTacToeTile.hpp | 25 +++++++++ src/dawntictactoe/game/DawnGame.cpp | 4 +- .../prefabs/TicTacToeTilePrefab.hpp | 56 +++++++++++++++++++ src/dawntictactoe/scenes/TicTacToeScene.hpp | 45 +++++++++++++++ 8 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 src/dawntictactoe/components/CMakeLists.txt create mode 100644 src/dawntictactoe/components/TicTacToeTile.cpp create mode 100644 src/dawntictactoe/components/TicTacToeTile.hpp create mode 100644 src/dawntictactoe/prefabs/TicTacToeTilePrefab.hpp create mode 100644 src/dawntictactoe/scenes/TicTacToeScene.hpp diff --git a/src/dawn/scene/SceneItemComponent.hpp b/src/dawn/scene/SceneItemComponent.hpp index a877ee10..12f54c96 100644 --- a/src/dawn/scene/SceneItemComponent.hpp +++ b/src/dawn/scene/SceneItemComponent.hpp @@ -64,6 +64,7 @@ namespace Dawn { */ virtual void onDispose(); + /** * Cleanup the SceneItemComponent. */ diff --git a/src/dawntictactoe/CMakeLists.txt b/src/dawntictactoe/CMakeLists.txt index c31f3496..d23fdf6f 100644 --- a/src/dawntictactoe/CMakeLists.txt +++ b/src/dawntictactoe/CMakeLists.txt @@ -16,10 +16,12 @@ target_include_directories(${DAWN_TARGET_NAME} ) # Subdirs +add_subdirectory(components) add_subdirectory(game) add_subdirectory(save) # Assets set(DIR_GAME_ASSETS games/tictactoe) -tool_language(locale_en ${DIR_GAME_ASSETS}/locale/en.xml) \ No newline at end of file +tool_language(locale_en ${DIR_GAME_ASSETS}/locale/en.xml) +tool_tileset(tileset_xo texture_xo ${DIR_GAME_ASSETS}/xo.png 1 3) \ No newline at end of file diff --git a/src/dawntictactoe/components/CMakeLists.txt b/src/dawntictactoe/components/CMakeLists.txt new file mode 100644 index 00000000..cc28f296 --- /dev/null +++ b/src/dawntictactoe/components/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright (c) 2023 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DAWN_TARGET_NAME} + PRIVATE + TicTacToeTile.cpp +) \ No newline at end of file diff --git a/src/dawntictactoe/components/TicTacToeTile.cpp b/src/dawntictactoe/components/TicTacToeTile.cpp new file mode 100644 index 00000000..df7cc20e --- /dev/null +++ b/src/dawntictactoe/components/TicTacToeTile.cpp @@ -0,0 +1,20 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "TicTacToeTile.hpp" +#include "scene/SceneItem.hpp" + +using namespace Dawn; + +TicTacToeTile::TicTacToeTile(SceneItem *item) : SceneItemComponent(item) { +} + +void TicTacToeTile::setState(enum TicTacToeTileState state) { + auto ts = this->item->getComponent(); + + ts->setTile(state); + + this->state = state; +} \ No newline at end of file diff --git a/src/dawntictactoe/components/TicTacToeTile.hpp b/src/dawntictactoe/components/TicTacToeTile.hpp new file mode 100644 index 00000000..09efbe03 --- /dev/null +++ b/src/dawntictactoe/components/TicTacToeTile.hpp @@ -0,0 +1,25 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "scene/SceneItemComponent.hpp" +#include "scene/components/display/TiledSprite.hpp" + +namespace Dawn { + enum TicTacToeTileState { + TIC_TAC_TOE_BLANK, + TIC_TAC_TOE_NOUGHT, + TIC_TAC_TOE_CROSS + }; + + class TicTacToeTile : public SceneItemComponent { + public: + enum TicTacToeTileState state = TIC_TAC_TOE_BLANK; + + TicTacToeTile(SceneItem *item); + + void setState(enum TicTacToeTileState state); + }; +} \ No newline at end of file diff --git a/src/dawntictactoe/game/DawnGame.cpp b/src/dawntictactoe/game/DawnGame.cpp index d99f1e70..b76e816e 100644 --- a/src/dawntictactoe/game/DawnGame.cpp +++ b/src/dawntictactoe/game/DawnGame.cpp @@ -4,7 +4,7 @@ // https://opensource.org/licenses/MIT #include "DawnGame.hpp" -#include "scenes/TestScene.hpp" +#include "scenes/TicTacToeScene.hpp" using namespace Dawn; @@ -23,7 +23,7 @@ int32_t DawnGame::init() { this->localeManager.init(); this->renderManager.init(); - this->scene = new TestScene(this); + this->scene = new TicTacToeScene(this); return DAWN_GAME_INIT_RESULT_SUCCESS; } diff --git a/src/dawntictactoe/prefabs/TicTacToeTilePrefab.hpp b/src/dawntictactoe/prefabs/TicTacToeTilePrefab.hpp new file mode 100644 index 00000000..e73e3927 --- /dev/null +++ b/src/dawntictactoe/prefabs/TicTacToeTilePrefab.hpp @@ -0,0 +1,56 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "prefab/SceneItemPrefab.hpp" +#include "scene/components/display/TiledSprite.hpp" +#include "scene/components/display/MeshHost.hpp" +#include "scene/components/display/MeshRenderer.hpp" +#include "scene/components/display/material/SimpleTexturedMaterial.hpp" +#include "components/TicTacToeTile.hpp" + +namespace Dawn { + class TicTacToeTilePrefab : public SceneItemPrefab { + public: + static std::vector prefabAssets(AssetManager *ass) { + return std::vector{ + ass->get("texture_xo"), + ass->get("tileset_xo") + }; + } + + // + MeshHost *meshHost; + TiledSprite *sprite; + MeshRenderer *meshRenderer; + SimpleTexturedMaterial *material; + TicTacToeTile *ticTacToe; + + TicTacToeTilePrefab(Scene *s, sceneitemid_t i) : + SceneItemPrefab(s,i) + { + } + + + void prefabInit(AssetManager *man) override { + auto tileset = man->get("tileset_xo"); + auto texture = man->get("texture_xo"); + + meshHost = this->addComponent(); + + meshRenderer = this->addComponent(); + + material = this->template addComponent(); + material->texture = &texture->texture; + + sprite = this->addComponent(); + sprite->setTileset(&tileset->tileset); + sprite->setSize(glm::vec2(1, 1)); + sprite->setTile(0x00); + + ticTacToe = this->addComponent(); + } + }; +} \ No newline at end of file diff --git a/src/dawntictactoe/scenes/TicTacToeScene.hpp b/src/dawntictactoe/scenes/TicTacToeScene.hpp new file mode 100644 index 00000000..3bee8b7b --- /dev/null +++ b/src/dawntictactoe/scenes/TicTacToeScene.hpp @@ -0,0 +1,45 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "scene/Scene.hpp" +#include "prefabs/SimpleSpinningCubePrefab.hpp" +#include "prefabs/TicTacToeTilePrefab.hpp" + +namespace Dawn { + class TicTacToeScene : public Scene { + protected: + Camera *camera; + TicTacToeTilePrefab* tiles[9]; + + void stage() override { + camera = Camera::create(this); + camera->transform->lookAt(glm::vec3(5, 5, 5), glm::vec3(0, 0, 0)); + + int32_t i = 0; + for(int32_t x = -1; x <= 1; x++) { + for(int32_t y = -1; y <= 1; y++) { + auto tile = TicTacToeTilePrefab::prefabCreate(this); + tile->transform.setLocalPosition(glm::vec3(x * 1.25f, y * 1.25f, 0)); + // tile->ticTacToe->setState(i % 2 == 0 ? TIC_TAC_TOE_CROSS : TIC_TAC_TOE_NOUGHT); + tiles[i++] = tile; + } + } + + + } + + std::vector getRequiredAssets() override { + auto assMan = &this->game->assetManager; + std::vector assets; + vectorAppend(&assets, SimpleSpinningCubePrefab::getRequiredAssets(assMan)); + vectorAppend(&assets, TicTacToeTilePrefab::getRequiredAssets(assMan)); + return assets; + } + + public: + TicTacToeScene(DawnGame *game) : Scene(game) {} + }; +} \ No newline at end of file