I just made C++ God and it's scary

This commit is contained in:
2023-02-26 21:56:39 -08:00
parent e8892b6477
commit 0ae51cd4dd
15 changed files with 221 additions and 96 deletions

View File

@ -67,16 +67,16 @@ void TicTacToeGame::onSceneUpdate() {
auto t = itTiles->second;
if(t == hovered) {
if(t->state == TIC_TAC_TOE_EMPTY && getGame()->inputManager.isPressed(INPUT_BIND_MOUSE_CLICK)) {
t->setState(nextMove);
if(t->tileState == TIC_TAC_TOE_EMPTY && getGame()->inputManager.isPressed(INPUT_BIND_MOUSE_CLICK)) {
t->tileState = nextMove;
nextMove = nextMove == TIC_TAC_TOE_NOUGHT ? TIC_TAC_TOE_CROSS : TIC_TAC_TOE_NOUGHT;
} else if(t->state == TIC_TAC_TOE_EMPTY) {
} else if(t->tileState == TIC_TAC_TOE_EMPTY) {
t->hovered = true;
}
} else {
t->hovered = false;
}
tileMap[itTiles->second->tile] = itTiles->second->getState();
tileMap[itTiles->second->tile] = itTiles->second->tileState;
++itTiles;
}

View File

@ -12,17 +12,19 @@ TicTacToeTile::TicTacToeTile(SceneItem *item) : SceneItemComponent(item) {
}
void TicTacToeTile::onStart() {
this->setState(TIC_TAC_TOE_EMPTY);
}
tileState = useState(TIC_TAC_TOE_EMPTY);
hovered = useState(false);
enum TicTacToeTileState TicTacToeTile::getState() {
return this->state;
}
auto cb = [&]{
auto sprite = this->item->getComponent<TiledSprite>();
if(this->hovered) {
sprite->setTile(0x03);
} else {
sprite->setTile(tileState);
}
};
void TicTacToeTile::setState(enum TicTacToeTileState state) {
auto ts = this->item->getComponent<TiledSprite>();
ts->setTile(state);
this->state = state;
useEffect(tileState, cb);
useEffect(hovered, cb);
cb();
}

View File

@ -11,16 +11,12 @@
namespace Dawn {
class TicTacToeTile : public SceneItemComponent {
public:
enum TicTacToeTileState state = TIC_TAC_TOE_EMPTY;
bool_t hovered = false;
StateProperty<enum TicTacToeTileState> tileState;
StateProperty<bool_t> hovered;
uint8_t tile;
TicTacToeTile(SceneItem *item);
void onStart() override;
enum TicTacToeTileState getState();
void setState(enum TicTacToeTileState state);
};
}

View File

@ -10,8 +10,11 @@
#include "display/mesh/TriangleMesh.hpp"
#include "components/TicTacToeGame.hpp"
#include "state/State.hpp"
namespace Dawn {
class TicTacToeScene : public Scene {
class TicTacToeScene : public Scene, public StateOwner {
protected:
Camera *camera;
@ -39,6 +42,10 @@ namespace Dawn {
}
}
}
void onStateUpdate() override {
std::cout << "State Update Invoked" << std::endl;
}
std::vector<Asset*> getRequiredAssets() override {
auto assMan = &this->game->assetManager;