tic tac toe done
This commit is contained in:
		@@ -33,7 +33,7 @@ namespace Dawn {
 | 
				
			|||||||
      StateProperty<float_t> fontSize;
 | 
					      StateProperty<float_t> fontSize;
 | 
				
			||||||
      StateProperty<Font*> font;
 | 
					      StateProperty<Font*> font;
 | 
				
			||||||
      StateProperty<float_t> maxWidth;
 | 
					      StateProperty<float_t> maxWidth;
 | 
				
			||||||
      struct Color textColor = COLOR_MAGENTA;
 | 
					      struct Color textColor = COLOR_WHITE;
 | 
				
			||||||
      struct FontMeasure measure;
 | 
					      struct FontMeasure measure;
 | 
				
			||||||
      int32_t startQuad = 0;
 | 
					      int32_t startQuad = 0;
 | 
				
			||||||
      int32_t quadCount = -1;
 | 
					      int32_t quadCount = -1;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,6 +6,7 @@
 | 
				
			|||||||
# Sources
 | 
					# Sources
 | 
				
			||||||
target_sources(${DAWN_TARGET_NAME}
 | 
					target_sources(${DAWN_TARGET_NAME}
 | 
				
			||||||
  PRIVATE
 | 
					  PRIVATE
 | 
				
			||||||
    TicTacToeTile.cpp
 | 
					 | 
				
			||||||
    TicTacToeGame.cpp
 | 
					    TicTacToeGame.cpp
 | 
				
			||||||
 | 
					    TicTacToeScoreboard.cpp
 | 
				
			||||||
 | 
					    TicTacToeTile.cpp
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -14,7 +14,9 @@ TicTacToeGame::TicTacToeGame(SceneItem *item) :
 | 
				
			|||||||
  SceneItemComponent(item),
 | 
					  SceneItemComponent(item),
 | 
				
			||||||
  winner(TIC_TAC_TOE_EMPTY),
 | 
					  winner(TIC_TAC_TOE_EMPTY),
 | 
				
			||||||
  nextMove(TIC_TAC_TOE_NOUGHT),
 | 
					  nextMove(TIC_TAC_TOE_NOUGHT),
 | 
				
			||||||
  gameOver(false)
 | 
					  gameOver(false),
 | 
				
			||||||
 | 
					  scoreCross(0),
 | 
				
			||||||
 | 
					  scoreNought(0)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -33,7 +35,19 @@ void TicTacToeGame::onStart() {
 | 
				
			|||||||
    auto board = this->getBoard();
 | 
					    auto board = this->getBoard();
 | 
				
			||||||
    std::vector<uint8_t> winningCombo;
 | 
					    std::vector<uint8_t> winningCombo;
 | 
				
			||||||
    winner = ticTacToeDetermineWinner(board, &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);
 | 
					  }, gameOver);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  useEvent([&](float_t delta) {
 | 
					  useEvent([&](float_t delta) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,26 +1,28 @@
 | 
				
			|||||||
// Copyright (c) 2023 Dominic Masters
 | 
					// Copyright (c) 2023 Dominic Masters
 | 
				
			||||||
// 
 | 
					// 
 | 
				
			||||||
// This software is released under the MIT License.
 | 
					// This software is released under the MIT License.
 | 
				
			||||||
// https://opensource.org/licenses/MIT
 | 
					// https://opensource.org/licenses/MIT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
#include "scene/SceneItemComponent.hpp"
 | 
					#include "scene/SceneItemComponent.hpp"
 | 
				
			||||||
#include "TicTacToeTile.hpp"
 | 
					#include "TicTacToeTile.hpp"
 | 
				
			||||||
#include "physics/3d/Ray3D.hpp"
 | 
					#include "physics/3d/Ray3D.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Dawn {
 | 
					namespace Dawn {
 | 
				
			||||||
  class TicTacToeGame : public SceneItemComponent {
 | 
					  class TicTacToeGame : public SceneItemComponent {
 | 
				
			||||||
    public:
 | 
					    public:
 | 
				
			||||||
      enum TicTacToeTileState winner;
 | 
					      enum TicTacToeTileState winner;
 | 
				
			||||||
      StateProperty<bool_t> gameOver;
 | 
					      StateProperty<bool_t> gameOver;
 | 
				
			||||||
      std::map<int32_t, TicTacToeTile*> tiles;
 | 
					      std::map<int32_t, TicTacToeTile*> tiles;
 | 
				
			||||||
      StateProperty<enum TicTacToeTileState> nextMove;
 | 
					      StateProperty<enum TicTacToeTileState> nextMove;
 | 
				
			||||||
 | 
					      StateProperty<int32_t> scoreCross;
 | 
				
			||||||
      TicTacToeGame(SceneItem *item);
 | 
					      StateProperty<int32_t> scoreNought;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      std::map<uint8_t, enum TicTacToeTileState> getBoard();
 | 
					      TicTacToeGame(SceneItem *item);
 | 
				
			||||||
      void makeMove(uint8_t tile, enum TicTacToeTileState player);
 | 
					
 | 
				
			||||||
 | 
					      std::map<uint8_t, enum TicTacToeTileState> getBoard();
 | 
				
			||||||
      void onStart() override;
 | 
					      void makeMove(uint8_t tile, enum TicTacToeTileState player);
 | 
				
			||||||
  };
 | 
					
 | 
				
			||||||
 | 
					      void onStart() override;
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										25
									
								
								src/dawntictactoe/components/TicTacToeScoreboard.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/dawntictactoe/components/TicTacToeScoreboard.cpp
									
									
									
									
									
										Normal 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 })();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										20
									
								
								src/dawntictactoe/components/TicTacToeScoreboard.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/dawntictactoe/components/TicTacToeScoreboard.hpp
									
									
									
									
									
										Normal 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;
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										32
									
								
								src/dawntictactoe/prefabs/SimpleLabel.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/dawntictactoe/prefabs/SimpleLabel.hpp
									
									
									
									
									
										Normal 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;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -8,23 +8,23 @@
 | 
				
			|||||||
#include "prefabs/SimpleSpinningCubePrefab.hpp"
 | 
					#include "prefabs/SimpleSpinningCubePrefab.hpp"
 | 
				
			||||||
#include "prefabs/TicTacToeTilePrefab.hpp"
 | 
					#include "prefabs/TicTacToeTilePrefab.hpp"
 | 
				
			||||||
#include "display/mesh/TriangleMesh.hpp"
 | 
					#include "display/mesh/TriangleMesh.hpp"
 | 
				
			||||||
#include "components/TicTacToeGame.hpp"
 | 
					#include "components/TicTacToeScoreboard.hpp"
 | 
				
			||||||
#include "scene/components/ui/UILabel.hpp"
 | 
					#include "prefabs/SimpleLabel.hpp"
 | 
				
			||||||
#include "scene/components/ui/menu/UISimpleMenu.hpp"
 | 
					#include "scene/components/ui/menu/UISimpleMenu.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "state/State.hpp"
 | 
					#include "state/State.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Dawn {
 | 
					namespace Dawn {
 | 
				
			||||||
 | 
					 | 
				
			||||||
  class TicTacToeScene : public Scene, public StateOwner {
 | 
					  class TicTacToeScene : public Scene, public StateOwner {
 | 
				
			||||||
    protected:
 | 
					    protected:
 | 
				
			||||||
      Camera *camera;
 | 
					      Camera *camera;
 | 
				
			||||||
      std::function<void()> evtUnsub;
 | 
					      std::function<void()> evtUnsub;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      UICanvas *canvas;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      void stage() override {
 | 
					      void stage() override {
 | 
				
			||||||
        camera = Camera::create(this);
 | 
					        camera = Camera::create(this);
 | 
				
			||||||
        // camera->transform->lookAt(glm::vec3(0, 0, 8), glm::vec3(0, 0, 0));
 | 
					        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));
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        float_t s = 2.0f;
 | 
					        float_t s = 2.0f;
 | 
				
			||||||
        camera->orthoTop = s;
 | 
					        camera->orthoTop = s;
 | 
				
			||||||
@@ -47,40 +47,16 @@ namespace Dawn {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        auto canvasItem = this->createSceneItem();
 | 
					        auto canvasItem = this->createSceneItem();
 | 
				
			||||||
        auto canvas = canvasItem->addComponent<UICanvas>();
 | 
					        canvas = canvasItem->addComponent<UICanvas>();
 | 
				
			||||||
        auto menu = canvasItem->addComponent<UIMenuController>();
 | 
					 | 
				
			||||||
        auto simpleMenu = canvasItem->addComponent<UISimpleMenu>();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for(int32_t x = 0; x < 2; x++) {
 | 
					        auto labelScore = SimpleLabel::prefabCreate(this);
 | 
				
			||||||
          for(int32_t y = 0; y < 2; y++) {
 | 
					        labelScore->addComponent<TicTacToeScoreboard>();
 | 
				
			||||||
            auto labelItem = this->createSceneItem();
 | 
					        labelScore->transform.setParent(canvas->transform);
 | 
				
			||||||
            auto label = labelItem->addComponent<UILabel>();
 | 
					        labelScore->label->fontSize = 36.0f;
 | 
				
			||||||
            auto labelMenuItem = labelItem->addComponent<UISimpleMenuItem>();
 | 
					        labelScore->label->alignX = UI_COMPONENT_ALIGN_MIDDLE;
 | 
				
			||||||
 | 
					        labelScore->label->alignment = glm::vec4(
 | 
				
			||||||
            label->font = &this->game->assetManager.get<TrueTypeAsset>("truetype_bizudp")->font;
 | 
					          0, 16, 0, 0
 | 
				
			||||||
            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);
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      
 | 
					      
 | 
				
			||||||
      std::vector<Asset*> getRequiredAssets() override {
 | 
					      std::vector<Asset*> getRequiredAssets() override {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user