Just breaking stuff

This commit is contained in:
2023-03-14 22:27:46 -07:00
parent 795e69237c
commit 09cb20271b
156 changed files with 4218 additions and 4389 deletions

View File

@ -0,0 +1,12 @@
# 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
TicTacToeGame.cpp
TicTacToeScoreboard.cpp
TicTacToeTile.cpp
)

View File

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

View File

@ -0,0 +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;
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,31 @@
// 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"
#include "game/DawnGame.hpp"
using namespace Dawn;
TicTacToeTile::TicTacToeTile(SceneItem *item) :
SceneItemComponent(item),
tileState(TIC_TAC_TOE_EMPTY),
hovered(false)
{
}
void TicTacToeTile::onStart() {
auto cb = [&]{
auto sprite = this->item->getComponent<TiledSprite>();
if(this->hovered && tileState == TIC_TAC_TOE_EMPTY) {
sprite->setTile(0x03);
} else {
sprite->setTile(tileState);
}
};
useEffect(cb, tileState);
useEffect(cb, hovered)();
}

View File

@ -0,0 +1,21 @@
// 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"
#include "games/tictactoe/TicTacToeLogic.hpp"
namespace Dawn {
class TicTacToeTile : public SceneItemComponent {
public:
StateProperty<enum TicTacToeTileState> tileState;
StateProperty<bool_t> hovered;
uint8_t tile;
TicTacToeTile(SceneItem *item);
void onStart() override;
};
}