From 12b6f4091d98c36af3a86fa81b3a6b14ba6dff9e Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 8 Mar 2023 21:13:44 -0800 Subject: [PATCH] tic tac toe done --- src/dawn/scene/components/ui/UILabel.hpp | 2 +- src/dawntictactoe/components/CMakeLists.txt | 3 +- .../components/TicTacToeGame.cpp | 18 ++++++- .../components/TicTacToeGame.hpp | 52 ++++++++++--------- .../components/TicTacToeScoreboard.cpp | 25 +++++++++ .../components/TicTacToeScoreboard.hpp | 20 +++++++ src/dawntictactoe/prefabs/SimpleLabel.hpp | 32 ++++++++++++ src/dawntictactoe/scenes/TicTacToeScene.hpp | 52 +++++-------------- 8 files changed, 137 insertions(+), 67 deletions(-) create mode 100644 src/dawntictactoe/components/TicTacToeScoreboard.cpp create mode 100644 src/dawntictactoe/components/TicTacToeScoreboard.hpp create mode 100644 src/dawntictactoe/prefabs/SimpleLabel.hpp diff --git a/src/dawn/scene/components/ui/UILabel.hpp b/src/dawn/scene/components/ui/UILabel.hpp index 276fa3ad..5964dc64 100644 --- a/src/dawn/scene/components/ui/UILabel.hpp +++ b/src/dawn/scene/components/ui/UILabel.hpp @@ -33,7 +33,7 @@ namespace Dawn { StateProperty fontSize; StateProperty font; StateProperty maxWidth; - struct Color textColor = COLOR_MAGENTA; + struct Color textColor = COLOR_WHITE; struct FontMeasure measure; int32_t startQuad = 0; int32_t quadCount = -1; diff --git a/src/dawntictactoe/components/CMakeLists.txt b/src/dawntictactoe/components/CMakeLists.txt index 58264fcd..1194deaf 100644 --- a/src/dawntictactoe/components/CMakeLists.txt +++ b/src/dawntictactoe/components/CMakeLists.txt @@ -6,6 +6,7 @@ # Sources target_sources(${DAWN_TARGET_NAME} PRIVATE - TicTacToeTile.cpp TicTacToeGame.cpp + TicTacToeScoreboard.cpp + TicTacToeTile.cpp ) \ No newline at end of file diff --git a/src/dawntictactoe/components/TicTacToeGame.cpp b/src/dawntictactoe/components/TicTacToeGame.cpp index 620d1ca1..1da6e217 100644 --- a/src/dawntictactoe/components/TicTacToeGame.cpp +++ b/src/dawntictactoe/components/TicTacToeGame.cpp @@ -14,7 +14,9 @@ TicTacToeGame::TicTacToeGame(SceneItem *item) : SceneItemComponent(item), winner(TIC_TAC_TOE_EMPTY), nextMove(TIC_TAC_TOE_NOUGHT), - gameOver(false) + gameOver(false), + scoreCross(0), + scoreNought(0) { } @@ -33,7 +35,19 @@ void TicTacToeGame::onStart() { auto board = this->getBoard(); std::vector winningCombo; winner = ticTacToeDetermineWinner(board, &winningCombo); - std::cout << "Winner is " << winner << std::endl; + + switch(winner) { + case TIC_TAC_TOE_CROSS: + scoreCross++; + break; + + case TIC_TAC_TOE_NOUGHT: + scoreNought++; + break; + + default: + break; + } }, gameOver); useEvent([&](float_t delta) { diff --git a/src/dawntictactoe/components/TicTacToeGame.hpp b/src/dawntictactoe/components/TicTacToeGame.hpp index 67bcaed6..af950ba8 100644 --- a/src/dawntictactoe/components/TicTacToeGame.hpp +++ b/src/dawntictactoe/components/TicTacToeGame.hpp @@ -1,26 +1,28 @@ -// 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 "TicTacToeTile.hpp" -#include "physics/3d/Ray3D.hpp" - -namespace Dawn { - class TicTacToeGame : public SceneItemComponent { - public: - enum TicTacToeTileState winner; - StateProperty gameOver; - std::map tiles; - StateProperty nextMove; - - TicTacToeGame(SceneItem *item); - - std::map getBoard(); - void makeMove(uint8_t tile, enum TicTacToeTileState player); - - void onStart() override; - }; +// 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 "TicTacToeTile.hpp" +#include "physics/3d/Ray3D.hpp" + +namespace Dawn { + class TicTacToeGame : public SceneItemComponent { + public: + enum TicTacToeTileState winner; + StateProperty gameOver; + std::map tiles; + StateProperty nextMove; + StateProperty scoreCross; + StateProperty scoreNought; + + TicTacToeGame(SceneItem *item); + + std::map getBoard(); + void makeMove(uint8_t tile, enum TicTacToeTileState player); + + void onStart() override; + }; } \ No newline at end of file diff --git a/src/dawntictactoe/components/TicTacToeScoreboard.cpp b/src/dawntictactoe/components/TicTacToeScoreboard.cpp new file mode 100644 index 00000000..04de9000 --- /dev/null +++ b/src/dawntictactoe/components/TicTacToeScoreboard.cpp @@ -0,0 +1,25 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "TicTacToeScoreboard.hpp" + +using namespace Dawn; + +TicTacToeScoreboard::TicTacToeScoreboard(SceneItem *item) : + SceneItemComponent(item) +{ + +} + +void TicTacToeScoreboard::onStart() { + auto game = getScene()->findComponent(); + assertNotNull(game); + + useEffect([&]{ + auto label = item->getComponent(); + assertNotNull(label); + label->text = std::to_string(game->scoreNought) + " - " + std::to_string(game->scoreCross); + }, { &game->scoreCross, &game->scoreNought })(); +} \ No newline at end of file diff --git a/src/dawntictactoe/components/TicTacToeScoreboard.hpp b/src/dawntictactoe/components/TicTacToeScoreboard.hpp new file mode 100644 index 00000000..c94eb8c3 --- /dev/null +++ b/src/dawntictactoe/components/TicTacToeScoreboard.hpp @@ -0,0 +1,20 @@ +// 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 "components/TicTacToeGame.hpp" +#include "scene/components/ui/UILabel.hpp" + +namespace Dawn { + class TicTacToeScoreboard : public SceneItemComponent { + protected: + TicTacToeGame *game = nullptr; + + public: + TicTacToeScoreboard(SceneItem *item); + void onStart() override; + }; +} \ No newline at end of file diff --git a/src/dawntictactoe/prefabs/SimpleLabel.hpp b/src/dawntictactoe/prefabs/SimpleLabel.hpp new file mode 100644 index 00000000..6bf2cfc2 --- /dev/null +++ b/src/dawntictactoe/prefabs/SimpleLabel.hpp @@ -0,0 +1,32 @@ +// 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/ui/UILabel.hpp" + +namespace Dawn { + class SimpleLabel : public SceneItemPrefab { + public: + static std::vector prefabAssets(AssetManager *ass) { + return { ass->get("truetype_bizudp") }; + } + + // + UILabel *label; + + SimpleLabel(Scene *s, sceneitemid_t i) : + SceneItemPrefab(s, i) + { + } + + void prefabInit(AssetManager *man) override { + auto font = man->get("truetype_bizudp"); + + label = this->addComponent(); + label->font = &font->font; + } + }; +} \ No newline at end of file diff --git a/src/dawntictactoe/scenes/TicTacToeScene.hpp b/src/dawntictactoe/scenes/TicTacToeScene.hpp index 68b96ea9..e1331b71 100644 --- a/src/dawntictactoe/scenes/TicTacToeScene.hpp +++ b/src/dawntictactoe/scenes/TicTacToeScene.hpp @@ -8,23 +8,23 @@ #include "prefabs/SimpleSpinningCubePrefab.hpp" #include "prefabs/TicTacToeTilePrefab.hpp" #include "display/mesh/TriangleMesh.hpp" -#include "components/TicTacToeGame.hpp" -#include "scene/components/ui/UILabel.hpp" +#include "components/TicTacToeScoreboard.hpp" +#include "prefabs/SimpleLabel.hpp" #include "scene/components/ui/menu/UISimpleMenu.hpp" #include "state/State.hpp" namespace Dawn { - class TicTacToeScene : public Scene, public StateOwner { protected: Camera *camera; std::function evtUnsub; + UICanvas *canvas; + void stage() override { camera = Camera::create(this); - // camera->transform->lookAt(glm::vec3(0, 0, 8), glm::vec3(0, 0, 0)); - camera->transform->lookAt(glm::vec3(32, 32, 32), glm::vec3(0, 0, 0)); + camera->transform->lookAt(glm::vec3(0, 0, 8), glm::vec3(0, 0, 0)); float_t s = 2.0f; camera->orthoTop = s; @@ -47,40 +47,16 @@ namespace Dawn { } auto canvasItem = this->createSceneItem(); - auto canvas = canvasItem->addComponent(); - auto menu = canvasItem->addComponent(); - auto simpleMenu = canvasItem->addComponent(); + canvas = canvasItem->addComponent(); - for(int32_t x = 0; x < 2; x++) { - for(int32_t y = 0; y < 2; y++) { - auto labelItem = this->createSceneItem(); - auto label = labelItem->addComponent(); - auto labelMenuItem = labelItem->addComponent(); - - label->font = &this->game->assetManager.get("truetype_bizudp")->font; - label->text = "Item " + std::to_string(x) + ", " + std::to_string(y); - label->fontSize = 36.0f; - labelItem->transform.setParent(canvas->transform); - - label->alignment = glm::vec4(label->fontSize * 8 * x, label->fontSize * 1.5f * y, 350, 100); - label->maxWidth = 0; - - labelMenuItem->menuX = x; - labelMenuItem->menuY = y; - - useEvent(std::bind([&](UISimpleMenuItem *itm){ - std::cout << "Hover on " << std::to_string(itm->menuX) << ", " << std::to_string(itm->menuY) << std::endl; - }, labelMenuItem), labelMenuItem->eventHoveredOn); - - useEvent(std::bind([&](UISimpleMenuItem *itm){ - std::cout << "Hover off " << std::to_string(itm->menuX) << ", " << std::to_string(itm->menuY) << std::endl; - }, labelMenuItem), labelMenuItem->eventHoveredOff); - - useEvent([&]{ - std::cout << "Selected" << std::endl; - }, labelMenuItem->eventSelected); - } - } + auto labelScore = SimpleLabel::prefabCreate(this); + labelScore->addComponent(); + labelScore->transform.setParent(canvas->transform); + labelScore->label->fontSize = 36.0f; + labelScore->label->alignX = UI_COMPONENT_ALIGN_MIDDLE; + labelScore->label->alignment = glm::vec4( + 0, 16, 0, 0 + ); } std::vector getRequiredAssets() override {