tic tac toe done

This commit is contained in:
2023-03-08 21:13:44 -08:00
parent 326e42bef3
commit 12b6f4091d
8 changed files with 137 additions and 67 deletions

View File

@ -33,7 +33,7 @@ namespace Dawn {
StateProperty<float_t> fontSize;
StateProperty<Font*> font;
StateProperty<float_t> maxWidth;
struct Color textColor = COLOR_MAGENTA;
struct Color textColor = COLOR_WHITE;
struct FontMeasure measure;
int32_t startQuad = 0;
int32_t quadCount = -1;

View File

@ -6,6 +6,7 @@
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
TicTacToeTile.cpp
TicTacToeGame.cpp
TicTacToeScoreboard.cpp
TicTacToeTile.cpp
)

View File

@ -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<uint8_t> 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) {

View File

@ -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<bool_t> gameOver;
std::map<int32_t, TicTacToeTile*> tiles;
StateProperty<enum TicTacToeTileState> nextMove;
TicTacToeGame(SceneItem *item);
std::map<uint8_t, enum TicTacToeTileState> 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<bool_t> gameOver;
std::map<int32_t, TicTacToeTile*> tiles;
StateProperty<enum TicTacToeTileState> nextMove;
StateProperty<int32_t> scoreCross;
StateProperty<int32_t> scoreNought;
TicTacToeGame(SceneItem *item);
std::map<uint8_t, enum TicTacToeTileState> getBoard();
void makeMove(uint8_t tile, enum TicTacToeTileState player);
void onStart() override;
};
}

View File

@ -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<TicTacToeGame>();
assertNotNull(game);
useEffect([&]{
auto label = item->getComponent<UILabel>();
assertNotNull(label);
label->text = std::to_string(game->scoreNought) + " - " + std::to_string(game->scoreCross);
}, { &game->scoreCross, &game->scoreNought })();
}

View File

@ -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;
};
}

View File

@ -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<SimpleLabel> {
public:
static std::vector<Asset*> prefabAssets(AssetManager *ass) {
return { ass->get<TrueTypeAsset>("truetype_bizudp") };
}
//
UILabel *label;
SimpleLabel(Scene *s, sceneitemid_t i) :
SceneItemPrefab<SimpleLabel>(s, i)
{
}
void prefabInit(AssetManager *man) override {
auto font = man->get<TrueTypeAsset>("truetype_bizudp");
label = this->addComponent<UILabel>();
label->font = &font->font;
}
};
}

View File

@ -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<void()> 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<UICanvas>();
auto menu = canvasItem->addComponent<UIMenuController>();
auto simpleMenu = canvasItem->addComponent<UISimpleMenu>();
canvas = canvasItem->addComponent<UICanvas>();
for(int32_t x = 0; x < 2; x++) {
for(int32_t y = 0; y < 2; y++) {
auto labelItem = this->createSceneItem();
auto label = labelItem->addComponent<UILabel>();
auto labelMenuItem = labelItem->addComponent<UISimpleMenuItem>();
label->font = &this->game->assetManager.get<TrueTypeAsset>("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<TicTacToeScoreboard>();
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<Asset*> getRequiredAssets() override {