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,25 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Build Project
add_executable(${DAWN_TARGET_NAME})
# Includes
target_include_directories(${DAWN_TARGET_NAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
# Subdirs
add_subdirectory(components)
add_subdirectory(game)
add_subdirectory(save)
# Assets
set(DIR_GAME_ASSETS games/tictactoe)
tool_language(locale_en ${DIR_GAME_ASSETS}/locale/en.xml)
tool_tileset(tileset_xo texture_xo ${DIR_GAME_ASSETS}/xo.png 1 4)
tool_truetype(truetype_bizudp ${DIR_GAME_ASSETS}/font/BIZUDPGothic-Bold.ttf truetype_bizudp 2048 2048 120)

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

View File

@ -0,0 +1,10 @@
# 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
DawnGame.cpp
)

View File

@ -0,0 +1,45 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "DawnGame.hpp"
#include "scenes/TicTacToeScene.hpp"
using namespace Dawn;
DawnGame::DawnGame(DawnHost *host) :
host(host),
renderManager(this),
inputManager(this),
localeManager(this),
saveManager(this)
{
}
int32_t DawnGame::init() {
this->assetManager.init();
this->localeManager.init();
this->renderManager.init();
this->scene = new TicTacToeScene(this);
return DAWN_GAME_INIT_RESULT_SUCCESS;
}
int32_t DawnGame::update(float_t delta) {
this->assetManager.update();
this->inputManager.update();
this->timeManager.update(delta);
if(this->scene != nullptr) this->scene->update();
this->renderManager.update();
return DAWN_GAME_UPDATE_RESULT_SUCCESS;
}
void DawnGame::sceneCutover(Scene *scene) {
if(scene == nullptr) scene = this->scene;
this->sceneToCutTo = scene;
}

View File

@ -0,0 +1,29 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "game/_DawnGame.hpp"
#include "save/DawnGameSaveManager.hpp"
namespace Dawn {
class DawnGame : public IDawnGame {
private:
Scene *sceneToCutTo = nullptr;
public:
DawnHost *host;
RenderManager renderManager;
AssetManager assetManager;
InputManager inputManager;
TimeManager timeManager;
LocaleManager localeManager;
DawnGameSaveManager saveManager;
DawnGame(DawnHost *host);
int32_t init() override;
int32_t update(float_t delta) override;
void sceneCutover(Scene *scene) override;
};
}

View File

@ -0,0 +1,19 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "input/InputManager.hpp"
#define INPUT_BIND(n) ((inputbind_t)n)
#define INPUT_BIND_ACCEPT INPUT_BIND(1)
#define INPUT_BIND_NEGATIVE_X INPUT_BIND(2)
#define INPUT_BIND_POSITIVE_X INPUT_BIND(3)
#define INPUT_BIND_NEGATIVE_Y INPUT_BIND(4)
#define INPUT_BIND_POSITIVE_Y INPUT_BIND(5)
#define INPUT_BIND_MOUSE_X INPUT_BIND(6)
#define INPUT_BIND_MOUSE_Y INPUT_BIND(7)
#define INPUT_BIND_MOUSE_CLICK INPUT_BIND(8)
#define INPUT_BIND_CANCEL INPUT_BIND(9)

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

@ -0,0 +1,62 @@
// 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/display/TiledSprite.hpp"
#include "scene/components/display/MeshHost.hpp"
#include "scene/components/display/MeshRenderer.hpp"
#include "scene/components/display/material/SimpleTexturedMaterial.hpp"
#include "scene/components/physics/3d/CubeCollider.hpp"
#include "components/TicTacToeTile.hpp"
namespace Dawn {
class TicTacToeTilePrefab : public SceneItemPrefab<TicTacToeTilePrefab> {
public:
static std::vector<Asset*> prefabAssets(AssetManager *ass) {
return std::vector<Asset*>{
ass->get<TextureAsset>("texture_xo"),
ass->get<TilesetAsset>("tileset_xo")
};
}
//
MeshHost *meshHost;
TiledSprite *sprite;
MeshRenderer *meshRenderer;
SimpleTexturedMaterial *material;
TicTacToeTile *ticTacToe;
CubeCollider *collider;
TicTacToeTilePrefab(Scene *s, sceneitemid_t i) :
SceneItemPrefab<TicTacToeTilePrefab>(s,i)
{
}
void prefabInit(AssetManager *man) override {
auto tileset = man->get<TilesetAsset>("tileset_xo");
auto texture = man->get<TextureAsset>("texture_xo");
meshHost = this->addComponent<MeshHost>();
meshRenderer = this->addComponent<MeshRenderer>();
material = this->template addComponent<SimpleTexturedMaterial>();
material->texture = &texture->texture;
sprite = this->addComponent<TiledSprite>();
sprite->setTileset(&tileset->tileset);
sprite->setSize(glm::vec2(1, 1));
sprite->setTile(0x01);
collider = this->addComponent<CubeCollider>();
collider->min = glm::vec3(-0.5f, -0.5f, -0.1f);
collider->max = glm::vec3( 0.5f, 0.5f, 0.1f);
ticTacToe = this->addComponent<TicTacToeTile>();
}
};
}

View File

@ -0,0 +1,10 @@
# 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
DawnGameSaveManager.cpp
)

View File

@ -0,0 +1,28 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "DawnGameSaveManager.hpp"
using namespace Dawn;
DawnGameSaveManager::DawnGameSaveManager(DawnGame *game) : SaveManager(game) {
}
bool_t DawnGameSaveManager::validateSave(struct SaveFile raw) {
if(!raw.has(POKER_SAVE_KEY_EXAMPLE)) return true;
this->currentSave.copy(raw, POKER_SAVE_KEY_EXAMPLE);
return false;
}
void DawnGameSaveManager::setExample(int32_t val) {
savedata_t value;
value.i32 = val;
this->currentSave.set(POKER_SAVE_KEY_EXAMPLE, value);
}
int32_t DawnGameSaveManager::getExample() {
return this->currentSave.get(POKER_SAVE_KEY_EXAMPLE).i32;
}

View File

@ -0,0 +1,22 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "save/SaveManager.hpp"
#define POKER_SAVE_KEY_EXAMPLE "poker.example"
namespace Dawn {
class DawnGameSaveManager : public SaveManager {
protected:
virtual bool_t validateSave(struct SaveFile raw) override;
public:
DawnGameSaveManager(DawnGame *game);
void setExample(int32_t value);
int32_t getExample();
};
}

View File

@ -0,0 +1,72 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/Scene.hpp"
#include "prefabs/SimpleSpinningCubePrefab.hpp"
#include "prefabs/TicTacToeTilePrefab.hpp"
#include "display/mesh/TriangleMesh.hpp"
#include "components/TicTacToeScoreboard.hpp"
#include "prefabs/SimpleLabel.hpp"
#include "scene/components/ui/menu/UISimpleMenu.hpp"
namespace Dawn {
class TicTacToeScene : public Scene {
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));
float_t s = 2.0f;
camera->orthoTop = s;
camera->orthoBottom = -s;
float_t ratio = 1.0f / 9.0f * 16.0f;
camera->orthoLeft = -s * ratio;
camera->orthoRight = s * ratio;
auto gameItem = this->createSceneItem();
auto game = gameItem->addComponent<TicTacToeGame>();
uint8_t i = 0;
for(int32_t x = -1; x <= 1; x++) {
for(int32_t y = -1; y <= 1; y++) {
auto tile = TicTacToeTilePrefab::create(this);
tile->transform.setLocalPosition(glm::vec3(x * 1, y * 1, 0));
tile->ticTacToe->tile = i++;
}
}
auto canvasItem = this->createSceneItem();
canvas = canvasItem->addComponent<UICanvas>();
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 {
auto assMan = &this->game->assetManager;
std::vector<Asset*> assets;
assets.push_back(assMan->get<TrueTypeAsset>("truetype_bizudp"));
vectorAppend(&assets, SimpleSpinningCubePrefab::getRequiredAssets(assMan));
vectorAppend(&assets, TicTacToeTilePrefab::getRequiredAssets(assMan));
return assets;
}
public:
TicTacToeScene(DawnGame *game) : Scene(game) {}
};
}