Fixed hard crash

This commit is contained in:
2023-03-03 23:15:26 -08:00
parent 6e5f151b9a
commit 855ac79095

View File

@ -1,119 +1,119 @@
// 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
#include "TicTacToeGame.hpp" #include "TicTacToeGame.hpp"
#include "game/DawnGame.hpp" #include "game/DawnGame.hpp"
#include "scene/components/example/ExampleSpin.hpp" #include "scene/components/example/ExampleSpin.hpp"
#include "scene/components/physics/3d/CubeCollider.hpp" #include "scene/components/physics/3d/CubeCollider.hpp"
using namespace Dawn; using namespace Dawn;
TicTacToeGame::TicTacToeGame(SceneItem *item) : 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)
{ {
} }
void TicTacToeGame::onStart() { void TicTacToeGame::onStart() {
// Map tiles by tile number = tile // Map tiles by tile number = tile
auto ts = getScene()->findComponents<TicTacToeTile>(); auto ts = getScene()->findComponents<TicTacToeTile>();
auto itTiles = ts.begin(); auto itTiles = ts.begin();
while(itTiles != ts.end()) { while(itTiles != ts.end()) {
this->tiles[(*itTiles)->tile] = *itTiles; this->tiles[(*itTiles)->tile] = *itTiles;
++itTiles; ++itTiles;
} }
useEffect([&]{ useEffect([&]{
if(!gameOver) return; if(!gameOver) return;
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; std::cout << "Winner is " << winner << std::endl;
}, gameOver); }, gameOver);
useEvent([&](float_t delta) { useEvent([&](float_t delta) {
// Only allow player input if it's their turn. // Only allow player input if it's their turn.
if(winner != TIC_TAC_TOE_EMPTY) return; if(gameOver) return;
Camera *camera = getScene()->findComponent<Camera>(); Camera *camera = getScene()->findComponent<Camera>();
if(camera == nullptr) return; if(camera == nullptr) return;
TicTacToeTile *hovered = nullptr; TicTacToeTile *hovered = nullptr;
bool_t isPlayerMove = nextMove == TIC_TAC_TOE_NOUGHT; bool_t isPlayerMove = nextMove == TIC_TAC_TOE_NOUGHT;
// Get hovered tile (for player move only) // Get hovered tile (for player move only)
if(isPlayerMove) { if(isPlayerMove) {
// Get mouse in screen space. // Get mouse in screen space.
auto mouse = getGame()->inputManager.getAxis2D(INPUT_BIND_MOUSE_X, INPUT_BIND_MOUSE_Y); auto mouse = getGame()->inputManager.getAxis2D(INPUT_BIND_MOUSE_X, INPUT_BIND_MOUSE_Y);
mouse *= 2.0f; mouse *= 2.0f;
mouse -= glm::vec2(1, 1); mouse -= glm::vec2(1, 1);
struct Ray3D ray; struct Ray3D ray;
ray.origin = camera->transform->getWorldPosition(); ray.origin = camera->transform->getWorldPosition();
ray.direction = camera->getRayDirectionFromScreenSpace(mouse); ray.direction = camera->getRayDirectionFromScreenSpace(mouse);
// Find the hovered tile (if any) // Find the hovered tile (if any)
auto results = getPhysics()->raycast3DAll(ray); auto results = getPhysics()->raycast3DAll(ray);
auto itResult = results.begin(); auto itResult = results.begin();
while(itResult != results.end()) { while(itResult != results.end()) {
auto result = *itResult; auto result = *itResult;
auto tile = result.collider->item->getComponent<TicTacToeTile>(); auto tile = result.collider->item->getComponent<TicTacToeTile>();
if(tile == nullptr) { if(tile == nullptr) {
++itResult; ++itResult;
continue; continue;
} }
hovered = tile; hovered = tile;
break; break;
} }
} }
// Now update the hover state(s) // Now update the hover state(s)
auto itTiles = tiles.begin(); auto itTiles = tiles.begin();
while(itTiles != tiles.end()) { while(itTiles != tiles.end()) {
auto t = itTiles->second; auto t = itTiles->second;
if(t == hovered) { if(t == hovered) {
t->hovered = true; t->hovered = true;
} else { } else {
t->hovered = false; t->hovered = false;
} }
++itTiles; ++itTiles;
} }
if(isPlayerMove) { if(isPlayerMove) {
if(getGame()->inputManager.isPressed(INPUT_BIND_MOUSE_CLICK)) { if(getGame()->inputManager.isPressed(INPUT_BIND_MOUSE_CLICK)) {
this->makeMove(hovered->tile, nextMove); this->makeMove(hovered->tile, nextMove);
} }
} else if(nextMove != TIC_TAC_TOE_NOUGHT) { } else if(nextMove != TIC_TAC_TOE_NOUGHT) {
auto board = this->getBoard(); auto board = this->getBoard();
auto move = ticTacToeGetAiMove(board, nextMove); auto move = ticTacToeGetAiMove(board, nextMove);
this->makeMove(move, nextMove); this->makeMove(move, nextMove);
} }
// Did game just end? // Did game just end?
if(ticTacToeIsGameOver(this->getBoard())) gameOver = true; gameOver = ticTacToeIsGameOver(this->getBoard());
}, getScene()->eventSceneUpdate); }, getScene()->eventSceneUpdate);
} }
std::map<uint8_t, enum TicTacToeTileState> TicTacToeGame::getBoard() { std::map<uint8_t, enum TicTacToeTileState> TicTacToeGame::getBoard() {
std::map<uint8_t, enum TicTacToeTileState> tileMap; std::map<uint8_t, enum TicTacToeTileState> tileMap;
auto itTiles = tiles.begin(); auto itTiles = tiles.begin();
while(itTiles != tiles.end()) { while(itTiles != tiles.end()) {
auto t = itTiles->second; auto t = itTiles->second;
tileMap[t->tile] = t->tileState; tileMap[t->tile] = t->tileState;
++itTiles; ++itTiles;
} }
return tileMap; return tileMap;
} }
void TicTacToeGame::makeMove(uint8_t tile, enum TicTacToeTileState player) { void TicTacToeGame::makeMove(uint8_t tile, enum TicTacToeTileState player) {
this->tiles[tile]->tileState = player; this->tiles[tile]->tileState = player;
nextMove = player == TIC_TAC_TOE_NOUGHT ? TIC_TAC_TOE_CROSS : TIC_TAC_TOE_NOUGHT; nextMove = player == TIC_TAC_TOE_NOUGHT ? TIC_TAC_TOE_CROSS : TIC_TAC_TOE_NOUGHT;
} }