From 9bce7592590fcfee0af4216e6032707b7ac17385 Mon Sep 17 00:00:00 2001 From: sebastian Date: Sun, 2 Aug 2026 20:11:05 +0200 Subject: [PATCH] ADD: Spawn Pawns --- CMakeLists.txt | 2 ++ game/src/GameLayer.cpp | 5 +---- game/src/GameLayer.h | 15 ++++++++++----- game/src/GameScene.cpp | 28 ++++++++++++++++++---------- game/src/GameScene.h | 2 ++ game/src/ludo/LudoGameMode.cpp | 2 +- game/src/ludo/LudoGameMode.h | 5 +++-- game/src/ludo/LudoGameState.h | 9 +++++++++ game/src/ludo/LudoTypes.h | 15 +++++++++++++++ game/src/ludo/PiecePresenter.cpp | 28 ++++++++++++++++++++++++++++ game/src/ludo/PiecePresenter.h | 31 +++++++++++++++++++++++++++++++ 11 files changed, 120 insertions(+), 22 deletions(-) create mode 100644 game/src/ludo/PiecePresenter.cpp create mode 100644 game/src/ludo/PiecePresenter.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 405612d..3d077cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -205,6 +205,8 @@ add_executable(ColorRace game/src/ludo/LudoGameMode.h game/src/ludo/BoardLayout.cpp game/src/ludo/BoardLayout.h + game/src/ludo/PiecePresenter.cpp + game/src/ludo/PiecePresenter.h ) diff --git a/game/src/GameLayer.cpp b/game/src/GameLayer.cpp index 531a585..d10b7ee 100644 --- a/game/src/GameLayer.cpp +++ b/game/src/GameLayer.cpp @@ -23,10 +23,7 @@ void GameLayer::onAttachImpl() { m_renderer = std::make_unique(); m_cameraController = std::make_unique(getInputStack(), getInputContext()); - - for (int i = 0; i <= 3; i++) { - std::cout << glm::to_string( m_boardLayout.getWorldPosition(ludo::PlayerColor::Red, ludo::PieceState::AtHome, i)) << std::endl; - } + m_piecePresenter.spawnPieces(m_gameMode->getState()); } void GameLayer::onDetachImpl() { diff --git a/game/src/GameLayer.h b/game/src/GameLayer.h index 39be3a4..9c1360a 100644 --- a/game/src/GameLayer.h +++ b/game/src/GameLayer.h @@ -10,17 +10,18 @@ #include "ecs/EntityRegistry.h" #include "layer/Layer.h" #include "ludo/BoardLayout.h" +#include "ludo/PiecePresenter.h" #include "renderer/MeshRenderer.h" #include "renderer/RenderQueue.h" #include "utils/openglWrapper/GLRenderState.h" - +#include "ludo/LudoGameMode.h" class GameLayer: public engine::Layer { public: - GameLayer(engine::EntityRegistry& entityRegistry, std::shared_ptr cam, std::shared_ptr light, - engine::Keyboard& keyboard, engine::Mouse& mouse, std::shared_ptr boardModel) - : Layer(keyboard, mouse), m_entityRegistry(entityRegistry), m_camera(std::move(cam)), m_renderer(std::make_unique()), - m_pointLight(std::move(light)), m_boardLayout(std::move(boardModel)){} + GameLayer(std::shared_ptr gameMode, engine::EntityRegistry& entityRegistry, std::shared_ptr cam, std::shared_ptr light, + engine::Keyboard& keyboard, engine::Mouse& mouse, std::shared_ptr boardModel, std::unordered_map> pieceModdels) + : Layer(keyboard, mouse), m_gameMode(std::move(gameMode)), m_entityRegistry(entityRegistry), m_camera(std::move(cam)), m_renderer(std::make_unique()), + m_pointLight(std::move(light)), m_boardLayout(std::move(boardModel)), m_piecePresenter(m_entityRegistry, std::move(pieceModdels), m_boardLayout){} void onUpdate(float deltaTime) override; void onRender() override; @@ -37,6 +38,10 @@ private: std::unique_ptr m_cameraController; ludo::BoardLayout m_boardLayout; + ludo::PiecePresenter m_piecePresenter; + engine::PlayerID m_localPlayer = 0; + std::shared_ptr m_gameMode; + }; #endif //COLORRACE_GAMELAYER_H diff --git a/game/src/GameScene.cpp b/game/src/GameScene.cpp index 8b70d2c..7afa47d 100644 --- a/game/src/GameScene.cpp +++ b/game/src/GameScene.cpp @@ -11,6 +11,8 @@ GameScene::GameScene(engine::InputContextStack& inputStack, engine::Keyboard& keyboard, engine::Mouse& mouse) : Scene(inputStack, keyboard, mouse), m_camera(std::make_unique()) { m_pointLight = std::make_shared(glm::vec3(.0f, 10.f, -5.0f), glm::vec3(1.0f, 1.0f, 1.0f)); + const std::vector players = {0,1,2,3}; + m_gameMode = std::make_shared(players); } void GameScene::onEnter() { @@ -20,18 +22,21 @@ void GameScene::onEnter() { m_camera->setYaw(0.0f); m_camera->setPitch(0.0f); - if (assetManager->hasModel("pawn")) { + const auto gameboardModel = assetManager->getModel("gameboard"); + const auto gameboardEntity = entityRegistry.createEntity(); - const auto gameboardModel = assetManager->getModel("gameboard"); - const auto gameboardEntity = entityRegistry.createEntity(); + const auto transform = engine::TransformComponent({0,0,0}, {0,0,0}, {1,1,1}); + const auto mesh = engine::MeshComponent(gameboardModel); + entityRegistry.addComponent(gameboardEntity, transform); + entityRegistry.addComponent(gameboardEntity, mesh); - const auto transform = engine::TransformComponent({0,0,0}, {0,0,0}, {1,1,1}); - const auto mesh = engine::MeshComponent(gameboardModel); - entityRegistry.addComponent(gameboardEntity, transform); - entityRegistry.addComponent(gameboardEntity, mesh); - } + std::unordered_map> pieceModels; + pieceModels["Red"] = assetManager->getModel("pawn_red"); + pieceModels["Blue"] = assetManager->getModel("pawn_blue"); + pieceModels["Green"] = assetManager->getModel("pawn_green"); + pieceModels["Yellow"] = assetManager->getModel("pawn_yellow"); - addLayer(std::make_unique(entityRegistry, m_camera, m_pointLight, m_keyboard, m_mouse, assetManager->getModel("gameboard"))); + addLayer(std::make_unique(m_gameMode, entityRegistry, m_camera, m_pointLight, m_keyboard, m_mouse, assetManager->getModel("gameboard"), pieceModels)); } void GameScene::onExit() { @@ -40,7 +45,10 @@ void GameScene::onExit() { std::vector GameScene::getRequiredAssets() const { std::vector requests; - requests.emplace_back(engine::ModelRequest{"pawn", "assets/models/chess.obj", engine::ModelSplitPolicy::Combined}); + requests.emplace_back(engine::ModelRequest{"pawn_red", "assets/models/chess_red.obj", engine::ModelSplitPolicy::Combined}); + requests.emplace_back(engine::ModelRequest{"pawn_blue", "assets/models/chess_blue.obj", engine::ModelSplitPolicy::Combined}); + requests.emplace_back(engine::ModelRequest{"pawn_green", "assets/models/chess_green.obj", engine::ModelSplitPolicy::Combined}); + requests.emplace_back(engine::ModelRequest{"pawn_yellow", "assets/models/chess_yellow.obj", engine::ModelSplitPolicy::Combined}); requests.emplace_back(engine::ModelRequest{ "gameboard", "assets/models/GameBoard.obj", engine::ModelSplitPolicy::Combined}); return requests; } \ No newline at end of file diff --git a/game/src/GameScene.h b/game/src/GameScene.h index da2f7dd..e6cdf8d 100644 --- a/game/src/GameScene.h +++ b/game/src/GameScene.h @@ -5,6 +5,7 @@ #ifndef COLORRACE_GAMESCENE_H #define COLORRACE_GAMESCENE_H #include "layer/Scene.h" +#include "ludo/LudoGameMode.h" #include "renderer/entities/PointLight.h" @@ -17,6 +18,7 @@ public: private: std::shared_ptr m_camera; std::shared_ptr m_pointLight; + std::shared_ptr m_gameMode; }; diff --git a/game/src/ludo/LudoGameMode.cpp b/game/src/ludo/LudoGameMode.cpp index 7b56807..1e35406 100644 --- a/game/src/ludo/LudoGameMode.cpp +++ b/game/src/ludo/LudoGameMode.cpp @@ -81,7 +81,7 @@ void ludo::LudoGameMode::handle(const RollDiceCommand &command) { } } -ludo::PlayerColor ludo::LudoGameMode::getColorOf(engine::PlayerID player) { +ludo::PlayerColor ludo::LudoGameMode::getColorOf(engine::PlayerID player) const { const auto& order = getPlayerOrder(); auto it = std::find(order.begin(), order.end(), player); assert(it != order.end() && "getColorOf mit unregistrierter PlayerID aufgerufen"); diff --git a/game/src/ludo/LudoGameMode.h b/game/src/ludo/LudoGameMode.h index 0626387..6373dae 100644 --- a/game/src/ludo/LudoGameMode.h +++ b/game/src/ludo/LudoGameMode.h @@ -14,7 +14,8 @@ namespace ludo { class LudoGameMode: public engine::TurnBasedGameMode { public: - explicit LudoGameMode(std::vector players, std::optional seed = std::nullopt) : TurnBasedGameMode(std::move(players)), m_rng(seed.value_or(std::random_device{}())) {} + explicit LudoGameMode(std::vector players, std::optional seed = std::nullopt) : TurnBasedGameMode(std::move(players)), + m_rng(seed.value_or(std::random_device{}())), m_gameState(getPlayerOrder()) {} void sendCommand(const std::variant &command) override; std::vector pollEvents() override; @@ -32,7 +33,7 @@ namespace ludo { void handle(const RollDiceCommand &command); - PlayerColor getColorOf(engine::PlayerID playerID); + [[nodiscard]] PlayerColor getColorOf(engine::PlayerID playerID) const; void handle(const MovePieceCommand &command); void finishTurn(); diff --git a/game/src/ludo/LudoGameState.h b/game/src/ludo/LudoGameState.h index feeff86..37663ec 100644 --- a/game/src/ludo/LudoGameState.h +++ b/game/src/ludo/LudoGameState.h @@ -13,6 +13,15 @@ namespace ludo { class LudoGameState: public engine::GameState { public: + explicit LudoGameState(const std::vector& playerIDs) { + for (const auto& playerID : playerIDs) { + PlayerColor playerColor = getColorOf(playerID, playerIDs); + for (int i = 0; i < 4; i++) { + auto piece = Piece(playerColor, PieceState::AtHome, i); + m_pieces.push_back(piece); + } + } + }; [[nodiscard]] const std::vector &getPieces() const; [[nodiscard]] int getDiceValue() const; [[nodiscard]] TurnPhase getPhase() const; diff --git a/game/src/ludo/LudoTypes.h b/game/src/ludo/LudoTypes.h index ee5f77c..3a44cff 100644 --- a/game/src/ludo/LudoTypes.h +++ b/game/src/ludo/LudoTypes.h @@ -4,6 +4,9 @@ #ifndef COLORRACE_LUDOTYPES_H #define COLORRACE_LUDOTYPES_H +#include +#include +#include #include #include @@ -36,6 +39,18 @@ namespace ludo { return "Unknown"; } + constexpr std::array(PlayerColor::Count)> PlayerColors = { + PlayerColor::Red, + PlayerColor::Blue, + PlayerColor::Yellow, + PlayerColor::Green + }; + static PlayerColor getColorOf(engine::PlayerID player, std::vector playerOrder) { + auto it = std::find(playerOrder.begin(), playerOrder.end(), player); + assert(it != playerOrder.end() && "getColorOf mit unregistrierter PlayerID aufgerufen"); + return static_cast(std::distance(playerOrder.begin(), it)); + } + enum class PieceState : uint8_t { AtHome, OnTrack, InHomeStretch, Finished }; enum class TurnPhase : uint8_t { AwaitingRoll, AwaitingPieceSelection }; diff --git a/game/src/ludo/PiecePresenter.cpp b/game/src/ludo/PiecePresenter.cpp new file mode 100644 index 0000000..c1936b1 --- /dev/null +++ b/game/src/ludo/PiecePresenter.cpp @@ -0,0 +1,28 @@ +// +// Created by sebastian on 02.08.26. +// + +#include "PiecePresenter.h" + +#include "ecs/standardComponents/MeshComponent.h" +#include "ecs/standardComponents/TransformComponent.h" + +void ludo::PiecePresenter::spawnPieces(const ludo::LudoGameState &state) { + for (const auto& piece : state.getPieces()) { + auto entity = m_registry.createEntity(); + const auto transform = engine::TransformComponent( + m_layout.getWorldPosition(piece.color, piece.state, piece.position), + {0,0,0}, + {0.334296f, 0.334296f, 0.334296f}); + m_registry.addComponent(entity, transform); + m_registry.addComponent(entity, engine::MeshComponent(m_pieceModels[std::string(ludo::toString(piece.color))])); + } +} + +void ludo::PiecePresenter::onPieceMoved(const ludo::PieceMovedEvent &event, const ludo::LudoGameState &state) { + const auto& piece = state.getPieces()[event.pieceIndex]; + auto target = m_layout.getWorldPosition(piece.color, piece.state, piece.position); + auto entity = m_pieceEntities[event.pieceIndex]; + auto& transform = m_registry.getComponent(entity); + transform.m_position = target; +} diff --git a/game/src/ludo/PiecePresenter.h b/game/src/ludo/PiecePresenter.h new file mode 100644 index 0000000..d023ae6 --- /dev/null +++ b/game/src/ludo/PiecePresenter.h @@ -0,0 +1,31 @@ +// +// Created by sebastian on 02.08.26. +// + +#ifndef COLORRACE_PIECEPRESENTER_H +#define COLORRACE_PIECEPRESENTER_H +#include "BoardLayout.h" +#include "LudoGameState.h" +#include "ecs/EntityRegistry.h" +#include "loader/assets/AssetManager.h" +#include "loader/models/Model.h" + + +namespace ludo { + class PiecePresenter { + public: + PiecePresenter(engine::EntityRegistry& registry, std::unordered_map> pieceModels, BoardLayout& layout) + : m_registry(registry), m_pieceModels(std::move(pieceModels)), m_layout(layout) {} + + void spawnPieces(const ludo::LudoGameState& state); + void onPieceMoved(const ludo::PieceMovedEvent& event, const ludo::LudoGameState& state); + private: + engine::EntityRegistry& m_registry; + std::unordered_map> m_pieceModels; + BoardLayout& m_layout; + std::vector m_pieceEntities; + }; +} + + +#endif //COLORRACE_PIECEPRESENTER_H