From 5e580af16fe9a7471d41dedf8e3a5e00e2467852 Mon Sep 17 00:00:00 2001 From: sebastian Date: Wed, 5 Aug 2026 16:00:32 +0200 Subject: [PATCH] WIP: GameController --- CMakeLists.txt | 13 +- game/src/ColorRaceApp.h | 2 +- game/src/GameLayer.cpp | 116 ------------------ game/src/GameLayer.h | 78 ------------ game/src/GameScene.cpp | 4 +- game/src/gameLayer/GameInput.cpp | 5 + game/src/gameLayer/GameInput.h | 13 ++ game/src/gameLayer/GameLayer.cpp | 101 +++++++++++++++ game/src/gameLayer/GameLayer.h | 73 +++++++++++ game/src/gameLayer/GameRenderer.cpp | 5 + game/src/gameLayer/GameRenderer.h | 13 ++ game/src/gameLayer/LudoHud.cpp | 5 + game/src/gameLayer/LudoHud.h | 13 ++ .../gameController/LudoGameController.cpp | 96 +++++++++++++++ .../gameController/LudoGameController.h | 49 ++++++++ .../LudoGameControllerContext.h | 35 ++++++ game/src/ludo/PresentationGameState.cpp | 5 +- game/src/ludo/PresentationGameState.h | 6 +- 18 files changed, 429 insertions(+), 203 deletions(-) delete mode 100644 game/src/GameLayer.cpp delete mode 100644 game/src/GameLayer.h create mode 100644 game/src/gameLayer/GameInput.cpp create mode 100644 game/src/gameLayer/GameInput.h create mode 100644 game/src/gameLayer/GameLayer.cpp create mode 100644 game/src/gameLayer/GameLayer.h create mode 100644 game/src/gameLayer/GameRenderer.cpp create mode 100644 game/src/gameLayer/GameRenderer.h create mode 100644 game/src/gameLayer/LudoHud.cpp create mode 100644 game/src/gameLayer/LudoHud.h create mode 100644 game/src/gameLayer/gameController/LudoGameController.cpp create mode 100644 game/src/gameLayer/gameController/LudoGameController.h create mode 100644 game/src/gameLayer/gameController/LudoGameControllerContext.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4dfabde..34427ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -255,8 +255,8 @@ target_link_libraries(Engine PUBLIC OpenGL::GL glfw glad glm::glm imgui spdlog:: add_library(GameLib STATIC game/src/ColorRaceApp.cpp game/src/ColorRaceApp.h - game/src/GameLayer.cpp - game/src/GameLayer.h + game/src/gameLayer/GameLayer.cpp + game/src/gameLayer/GameLayer.h game/src/GameScene.cpp game/src/GameScene.h game/src/ludo/LudoGameState.cpp @@ -278,6 +278,15 @@ add_library(GameLib STATIC game/src/ludo/DiceSource.h game/src/ludo/PresentationGameState.cpp game/src/ludo/PresentationGameState.h + game/src/gameLayer/GameRenderer.cpp + game/src/gameLayer/GameRenderer.h + game/src/gameLayer/gameController/LudoGameController.cpp + game/src/gameLayer/gameController/LudoGameController.h + game/src/gameLayer/LudoHud.cpp + game/src/gameLayer/LudoHud.h + game/src/gameLayer/GameInput.cpp + game/src/gameLayer/GameInput.h + game/src/gameLayer/gameController/LudoGameControllerContext.h ) target_include_directories(GameLib PUBLIC game/src) target_link_libraries(GameLib PUBLIC Engine) diff --git a/game/src/ColorRaceApp.h b/game/src/ColorRaceApp.h index 1563e7e..2959d18 100644 --- a/game/src/ColorRaceApp.h +++ b/game/src/ColorRaceApp.h @@ -4,7 +4,7 @@ #ifndef COLORRACE_COLORRACEAPP_H #define COLORRACE_COLORRACEAPP_H -#include "GameLayer.h" +#include "gameLayer/GameLayer.h" #include "GameScene.h" #include "core/Application.h" #include "ecs/EntityRegistry.h" diff --git a/game/src/GameLayer.cpp b/game/src/GameLayer.cpp deleted file mode 100644 index dc90f9b..0000000 --- a/game/src/GameLayer.cpp +++ /dev/null @@ -1,116 +0,0 @@ -// -// Created by sebastian on 29.07.26. -// - -#include "GameLayer.h" - -#include -#include -#include - -#include "ecs/standardComponents/MeshComponent.h" -#include "ecs/standardComponents/TransformComponent.h" -#include "renderer/MeshRenderer.h" -#include "renderer/MeshRenderSystem.h" -#define GLM_ENABLE_EXPERIMENTAL -#include - -#include "core/animation/components/BlockingAnimationComponent.h" -#include "core/audio/SoundSource.h" -#include "core/audio/ecs/BlockingSoundComponent.h" -#include "core/audio/ecs/PlaySoundComponent.h" -#include "GLFW/glfw3.h" -using namespace engine; -void GameLayer::onAttachImpl() { - m_renderState.depthTesting = false; - m_renderState.backfaceCulling = false; - - m_renderer = std::make_unique(); - - m_cameraController = std::make_unique(getInputStack(), getInputContext()); - m_piecePresenter.spawnPieces(m_gameMode->getState()); - - this->m_presentationGameState = std::make_unique(*m_gameMode, m_gameMode->getState()); -} - -void GameLayer::onDetachImpl() { - -} - -void GameLayer::renderHud() { - const auto& state = m_gameMode->getState(); - - - - ImGui::SetNextWindowPos(ImVec2(20, 20), ImGuiCond_FirstUseEver); - ImGui::Begin("Ludo", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize); - - auto currentPlayerColor = m_gameMode->getColorOf(m_presentationGameState->getCurrentPlayer()); - int currentDiceValue = m_presentationGameState->getDiceValue(); - ImGui::Text("Am Zug: %s", toString(currentPlayerColor).data()); - ImGui::Text("Wuerfel: %d", currentDiceValue); - - ImGui::BeginDisabled(m_presentationGameState->isDiceRollEnabled() == false && m_gameMode->getState().getPhase() == ludo::TurnPhase::AwaitingRoll); - if (ImGui::Button("Wuerfeln")) { - m_gameMode->sendCommand(ludo::RollDiceCommand{m_localPlayer}); - } - // - // if (!m_lastEventMessage.empty()) ImGui::Text("%s", m_lastEventMessage.c_str()); - - ImGui::EndDisabled(); - - ImGui::End(); - -} - -bool GameLayer::isAnimationBlocking() const { - return m_entityRegistry.getPool().size() > 0; -} - -bool GameLayer::isSoundBlocking() const { - return m_entityRegistry.getPool().size() > 0; -} - -void GameLayer::onUpdate(float deltaTime) { - Layer::onUpdate(deltaTime); - m_cameraController->update(getKeyboard(), *m_camera, deltaTime); - m_animationSystem.update(deltaTime); - if (isAnimationBlocking() || isSoundBlocking()) return; - m_presentationGameState->commitGameState(m_gameMode->getCurrentPlayer(), m_gameMode->getState().getDiceValue(), m_gameMode->getState().getPhase() == ludo::TurnPhase::AwaitingRoll && m_localPlayer == m_gameMode->getCurrentPlayer()); - for (auto& ai : aiControllers) { - ai.update(deltaTime); - } - - for (const auto& event : m_gameMode->pollEvents()) { - std::visit([this](const T& e) { - if constexpr (std::is_same_v) { - m_piecePresenter.onPieceMoved(e, m_gameMode->getState()); - } - if constexpr (std::is_same_v) { - // m_lastEventMessage = std::format("Wuerfel geworfen: {}", e.diceValue); - } - // DiceRolledEvent/TurnChangedEvent -> ImGui-State aktualisieren - }, event); - } - - m_highlightSystem.update(getMouse(), m_entityRegistry); - - if (auto entity = m_pickingSystem.update(getMouse(), m_entityRegistry)) { - if (auto pieceIndex = m_piecePresenter.getPieceIndex(*entity)) { - spdlog::info("Sende MovePieceCommand fuer pieceIndex={}", *pieceIndex); - m_gameMode->sendCommand(ludo::MovePieceCommand{m_localPlayer, *pieceIndex}); - } else { - spdlog::warn("Entity {} getroffen, aber keine bekannte Spielfigur (getPieceIndex = nullopt)", entity->value()); - } - } -} - -void GameLayer::onRender() { - m_renderQueue.clear(); - - MeshRenderSystem meshRenderSystem; - meshRenderSystem.update(m_entityRegistry, m_renderQueue); - m_renderer->render(m_renderQueue, *m_camera, *m_pointLight); - - renderHud(); -} diff --git a/game/src/GameLayer.h b/game/src/GameLayer.h deleted file mode 100644 index e0ec6d6..0000000 --- a/game/src/GameLayer.h +++ /dev/null @@ -1,78 +0,0 @@ -// -// Created by sebastian on 29.07.26. -// - -#ifndef COLORRACE_GAMELAYER_H -#define COLORRACE_GAMELAYER_H -#include - -#include "core/animation/AnimationSystem.h" -#include "core/audio/AudioDevice.h" -#include "core/audio/SoundBuffer.h" -#include "core/inputsOutputs/controller/CameraController.h" -#include "ecs/EntityRegistry.h" -#include "ecs/systems/PickingSystem.h" -#include "layer/Layer.h" -#include "ludo/BoardLayout.h" -#include "ludo/LudoAiController.h" -#include "ludo/PiecePresenter.h" -#include "renderer/MeshRenderer.h" -#include "renderer/RenderQueue.h" -#include "utils/openglWrapper/GLRenderState.h" -#include "ludo/LudoGameMode.h" -#include "ludo/PresentationGameState.h" -#include "ludo/ecs/highlight/HighlightSystem.h" - -class GameLayer: public engine::Layer { -public: - GameLayer(engine::EventBus& animationEventbus, 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, std::unordered_map> pieceSounds, - engine::EventBus& eventBus) - : Layer(keyboard, mouse, animationEventbus), 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(animationEventbus, m_entityRegistry, std::move(pieceModdels), std::move(pieceSounds), m_boardLayout), - m_pickingSystem(*m_camera), m_highlightSystem(*m_camera), m_animationSystem(m_entityRegistry), m_eventBus(eventBus) { - for (auto playerID : m_gameMode->getPlayerOrder()) { - if (playerID != m_localPlayer) { - aiControllers.emplace_back(m_gameMode, playerID); - } - } - } - - void onUpdate(float deltaTime) override; - void onRender() override; -protected: - void onAttachImpl() override; - void onDetachImpl() override; -private: - engine::GLRenderState m_renderState; - engine::RenderQueue m_renderQueue; - engine::EntityRegistry& m_entityRegistry; - engine::EventBus& m_eventBus; - std::shared_ptr m_pointLight; - std::shared_ptr m_camera; - std::unique_ptr m_renderer; - std::unique_ptr m_cameraController; - - ludo::BoardLayout m_boardLayout; - ludo::PiecePresenter m_piecePresenter; - engine::PlayerID m_localPlayer = 0; - std::shared_ptr m_gameMode; - std::unique_ptr m_presentationGameState; - engine::PickingSystem m_pickingSystem; - ludo::HighlightSystem m_highlightSystem; - - - std::vector aiControllers; - engine::animation::AnimationSystem m_animationSystem; - - - std::unique_ptr m_soundBuffer; - - - void renderHud(); - bool isAnimationBlocking() const; - - bool isSoundBlocking() const; -}; - -#endif //COLORRACE_GAMELAYER_H diff --git a/game/src/GameScene.cpp b/game/src/GameScene.cpp index dddf635..75061f5 100644 --- a/game/src/GameScene.cpp +++ b/game/src/GameScene.cpp @@ -5,7 +5,7 @@ #include "GameScene.h" #include "AudioLayer.h" -#include "GameLayer.h" +#include "gameLayer/GameLayer.h" #include "core/inputsOutputs/inputs/Mouse.h" #include "ecs/standardComponents/MeshComponent.h" #include "ecs/standardComponents/TransformComponent.h" @@ -44,7 +44,7 @@ void GameScene::onEnter() { addLayer(std::make_unique(*assetManager, m_keyboard, m_mouse, m_gameMode, entityRegistry, m_eventBus, getAnimationEventBus())); addLayer(std::make_unique(getAnimationEventBus(), *m_camera, entityRegistry, m_keyboard, m_mouse, engine::SphereFactory::createUVSphere(1.0f))); - addLayer(std::make_unique(getAnimationEventBus(), m_gameMode, entityRegistry, m_camera, m_pointLight, m_keyboard, m_mouse, assetManager->getModel("gameboard"), pieceModels, pieceSounds, m_eventBus)); + addLayer(std::make_unique(getAnimationEventBus(), m_gameMode, entityRegistry, m_camera, m_pointLight, m_keyboard, m_mouse, m_eventBus, *assetManager)); } void GameScene::onExit() { diff --git a/game/src/gameLayer/GameInput.cpp b/game/src/gameLayer/GameInput.cpp new file mode 100644 index 0000000..3acc2a8 --- /dev/null +++ b/game/src/gameLayer/GameInput.cpp @@ -0,0 +1,5 @@ +// +// Created by sebastian on 05.08.26. +// + +#include "GameInput.h" diff --git a/game/src/gameLayer/GameInput.h b/game/src/gameLayer/GameInput.h new file mode 100644 index 0000000..e68108a --- /dev/null +++ b/game/src/gameLayer/GameInput.h @@ -0,0 +1,13 @@ +// +// Created by sebastian on 05.08.26. +// + +#ifndef COLORRACE_GAMEINPUT_H +#define COLORRACE_GAMEINPUT_H + + +class GameInput { +}; + + +#endif //COLORRACE_GAMEINPUT_H diff --git a/game/src/gameLayer/GameLayer.cpp b/game/src/gameLayer/GameLayer.cpp new file mode 100644 index 0000000..2d14a28 --- /dev/null +++ b/game/src/gameLayer/GameLayer.cpp @@ -0,0 +1,101 @@ +// +// Created by sebastian on 29.07.26. +// + +#include "GameLayer.h" + +#include +#include +#include + +#include "ecs/standardComponents/MeshComponent.h" +#include "ecs/standardComponents/TransformComponent.h" +#include "renderer/MeshRenderer.h" +#include "renderer/MeshRenderSystem.h" +#define GLM_ENABLE_EXPERIMENTAL +#include + +#include "core/animation/components/BlockingAnimationComponent.h" +#include "core/audio/SoundSource.h" +#include "core/audio/ecs/BlockingSoundComponent.h" +#include "core/audio/ecs/PlaySoundComponent.h" +#include "GLFW/glfw3.h" +using namespace engine; +void GameLayer::onAttachImpl() { + m_renderState.depthTesting = false; + m_renderState.backfaceCulling = false; + + m_renderer = std::make_unique(); + + m_cameraController = std::make_unique(getInputStack(), getInputContext()); + + + std::unordered_map> pieceModels = std::unordered_map>(); + pieceModels["Red"] = m_assetManager.getModel("pawn_red"); + pieceModels["Blue"] = m_assetManager.getModel("pawn_blue"); + pieceModels["Green"] = m_assetManager.getModel("pawn_green"); + pieceModels["Yellow"] = m_assetManager.getModel("pawn_yellow"); + + std::unordered_map> pieceSounds; + pieceSounds["step"] = m_assetManager.getSound("pawn_step"); + + auto boardLayout = ludo::BoardLayout(m_assetManager.getModel("gameboard")); + + ludo::GameController::LudoGameControllerContext context = { + .gameMode = m_gameMode, + .registry = m_entityRegistry, + .camera = *m_camera, + .boardLayout = boardLayout, + .eventBus = m_eventBus, + .animationEventBus = getAnimationEventBus(), + .pieceModels = pieceModels, + .pieceSounds = pieceSounds, + .localPlayer = 0 + }; + this->m_GameController = std::move( ludo::GameController::LudoGameController::create(context)); + +} + +void GameLayer::onDetachImpl() { + +} + +void GameLayer::renderHud() { + ImGui::SetNextWindowPos(ImVec2(20, 20), ImGuiCond_FirstUseEver); + ImGui::Begin("Ludo", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize); + + auto currentPlayerColor = m_GameController->getPresentationGameState().getCurrentPlayerColor(); + int currentDiceValue = m_GameController->getPresentationGameState().getDiceValue(); + ImGui::Text("Am Zug: %s", toString(currentPlayerColor).data()); + ImGui::Text("Wuerfel: %d", currentDiceValue); + + ImGui::BeginDisabled(m_GameController->isBlocking() || !m_GameController->getPresentationGameState().isDiceRollEnabled()); + if (ImGui::Button("Wuerfeln")) { + m_gameMode->sendCommand(ludo::RollDiceCommand{m_GameController->getLocalPlayer()}); + } + // + // if (!m_lastEventMessage.empty()) ImGui::Text("%s", m_lastEventMessage.c_str()); + + ImGui::EndDisabled(); + + ImGui::End(); + +} + + +void GameLayer::onUpdate(float deltaTime) { + Layer::onUpdate(deltaTime); + m_cameraController->update(getKeyboard(), *m_camera, deltaTime); + + m_GameController->update(deltaTime, getMouse(), getKeyboard()); +} + +void GameLayer::onRender() { + m_renderQueue.clear(); + + MeshRenderSystem meshRenderSystem; + meshRenderSystem.update(m_entityRegistry, m_renderQueue); + m_renderer->render(m_renderQueue, *m_camera, *m_pointLight); + + renderHud(); +} diff --git a/game/src/gameLayer/GameLayer.h b/game/src/gameLayer/GameLayer.h new file mode 100644 index 0000000..916d086 --- /dev/null +++ b/game/src/gameLayer/GameLayer.h @@ -0,0 +1,73 @@ +// +// Created by sebastian on 29.07.26. +// + +#ifndef COLORRACE_GAMELAYER_H +#define COLORRACE_GAMELAYER_H +#include + +#include "core/animation/AnimationSystem.h" +#include "core/audio/AudioDevice.h" +#include "core/audio/SoundBuffer.h" +#include "core/inputsOutputs/controller/CameraController.h" +#include "ecs/EntityRegistry.h" +#include "ecs/systems/PickingSystem.h" +#include "layer/Layer.h" +#include "../ludo/BoardLayout.h" +#include "../ludo/LudoAiController.h" +#include "../ludo/PiecePresenter.h" +#include "renderer/MeshRenderer.h" +#include "renderer/RenderQueue.h" +#include "utils/openglWrapper/GLRenderState.h" +#include "../ludo/LudoGameMode.h" +#include "../ludo/PresentationGameState.h" +#include "../ludo/ecs/highlight/HighlightSystem.h" +#include "gameController/LudoGameController.h" + +class GameLayer: public engine::Layer { +public: + GameLayer( + engine::EventBus& animationEventbus, + std::shared_ptr gameMode, + engine::EntityRegistry& entityRegistry, + std::shared_ptr cam, + std::shared_ptr light, + engine::Keyboard& keyboard, engine::Mouse& mouse, + engine::EventBus& eventBus, + engine::AssetManager& assetManager + ) + : + Layer(keyboard, mouse, animationEventbus), + m_gameMode(gameMode), + m_eventBus(eventBus), + m_entityRegistry(entityRegistry), + m_camera(std::move(cam)), + m_pointLight(std::move(light)), + m_assetManager(assetManager) + {} + + + void onUpdate(float deltaTime) override; + void onRender() override; +protected: + void onAttachImpl() override; + void onDetachImpl() override; +private: + engine::GLRenderState m_renderState; + engine::RenderQueue m_renderQueue; + engine::EntityRegistry& m_entityRegistry; + engine::EventBus& m_eventBus; + std::shared_ptr m_pointLight; + std::shared_ptr m_camera; + std::unique_ptr m_renderer; + std::unique_ptr m_cameraController; + std::shared_ptr m_gameMode; + std::unique_ptr m_soundBuffer; + + + void renderHud(); + std::unique_ptr m_GameController; + engine::AssetManager& m_assetManager; +}; + +#endif //COLORRACE_GAMELAYER_H diff --git a/game/src/gameLayer/GameRenderer.cpp b/game/src/gameLayer/GameRenderer.cpp new file mode 100644 index 0000000..7ec8ce3 --- /dev/null +++ b/game/src/gameLayer/GameRenderer.cpp @@ -0,0 +1,5 @@ +// +// Created by sebastian on 05.08.26. +// + +#include "GameRenderer.h" diff --git a/game/src/gameLayer/GameRenderer.h b/game/src/gameLayer/GameRenderer.h new file mode 100644 index 0000000..5a51dec --- /dev/null +++ b/game/src/gameLayer/GameRenderer.h @@ -0,0 +1,13 @@ +// +// Created by sebastian on 05.08.26. +// + +#ifndef COLORRACE_GAMERENDERER_H +#define COLORRACE_GAMERENDERER_H + + +class GameRenderer { +}; + + +#endif //COLORRACE_GAMERENDERER_H diff --git a/game/src/gameLayer/LudoHud.cpp b/game/src/gameLayer/LudoHud.cpp new file mode 100644 index 0000000..27e3d89 --- /dev/null +++ b/game/src/gameLayer/LudoHud.cpp @@ -0,0 +1,5 @@ +// +// Created by sebastian on 05.08.26. +// + +#include "LudoHud.h" diff --git a/game/src/gameLayer/LudoHud.h b/game/src/gameLayer/LudoHud.h new file mode 100644 index 0000000..9702625 --- /dev/null +++ b/game/src/gameLayer/LudoHud.h @@ -0,0 +1,13 @@ +// +// Created by sebastian on 05.08.26. +// + +#ifndef COLORRACE_LUDOHUD_H +#define COLORRACE_LUDOHUD_H + + +class LudoHud { +}; + + +#endif //COLORRACE_LUDOHUD_H diff --git a/game/src/gameLayer/gameController/LudoGameController.cpp b/game/src/gameLayer/gameController/LudoGameController.cpp new file mode 100644 index 0000000..9dc60ee --- /dev/null +++ b/game/src/gameLayer/gameController/LudoGameController.cpp @@ -0,0 +1,96 @@ +// +// Created by sebastian on 05.08.26. +// + +#include "LudoGameController.h" + +#include "core/animation/components/BlockingAnimationComponent.h" +#include "core/audio/ecs/BlockingSoundComponent.h" + +std::unique_ptr ludo::GameController::LudoGameController::create(LudoGameControllerContext& context) { + auto controller = std::make_unique(context); + controller->m_piecePresenter.spawnPieces(context.gameMode->getState()); + std::vector playerOrder = controller->m_gameMode->getPlayerOrder(); + for (engine::PlayerID player : playerOrder) { + if (player == context.localPlayer) continue; + + controller->m_aiControllers.emplace_back(LudoAiController(controller->m_gameMode, player)); + } + return controller; +} + +void ludo::GameController::LudoGameController::update(float deltaTime, const engine::Mouse &mouse, const engine::Keyboard &keyboard) { + m_animationSystem.update(deltaTime); + + if (isBlocking()) return; + + processEvents(); + + m_presentationGameState.commitGameState( + m_gameMode->getCurrentPlayer(), + m_gameMode->getColorOf(m_gameMode->getCurrentPlayer()), + m_gameMode->getState().getDiceValue(), + m_gameMode->getCurrentPlayer() == 0 //Todo: Replace through real m_localPlayer variable + ); + + for (auto& ai : m_aiControllers) { + ai.update(deltaTime); + } + + handleInput(mouse, keyboard); +} + +void ludo::GameController::LudoGameController::handleInput(const engine::Mouse &mouse, const engine::Keyboard &keyboard) { + m_highlightSystem.update(mouse, m_registry); + + auto entity = m_pickingSystem.update(mouse, m_registry); + if (!entity) return; + + auto pieceIndex = m_piecePresenter.getPieceIndex(*entity); + if (!pieceIndex) return; + + m_gameMode->sendCommand(MovePieceCommand(m_localPlayer, *pieceIndex)); +} + +void ludo::GameController::LudoGameController::processEvents() { + for (const auto& event : m_gameMode->pollEvents()) { + std::visit([this](const T& e) { + if constexpr (std::is_same_v) { + m_piecePresenter.onPieceMoved(e, m_gameMode->getState()); + } + if constexpr (std::is_same_v) { + // m_lastEventMessage = std::format("Wuerfel geworfen: {}", e.diceValue); + } + // DiceRolledEvent/TurnChangedEvent -> ImGui-State aktualisieren + }, event); + } +} + +bool ludo::GameController::LudoGameController::isBlocking() const { + const bool isSoundBlocking = m_registry.getPool().size() > 0; + const bool isAnimationBlocking = m_registry.getPool().size() > 0; + + return isSoundBlocking || isAnimationBlocking; +} + +ludo::GameController::LudoGameController::LudoGameController( + LudoGameControllerContext deps +) + : + m_gameMode(std::move(deps.gameMode)), + m_registry(deps.registry), + m_piecePresenter( + deps.animationEventBus, + m_registry, + std::move(deps.pieceModels), + std::move(deps.pieceSounds), + deps.boardLayout + ), + m_presentationGameState(*m_gameMode, m_gameMode->getState()), + m_animationSystem(m_registry), + m_highlightSystem(deps.camera), + m_pickingSystem(deps.camera), + m_localPlayer(deps.localPlayer) +{ +} + diff --git a/game/src/gameLayer/gameController/LudoGameController.h b/game/src/gameLayer/gameController/LudoGameController.h new file mode 100644 index 0000000..ee4a14a --- /dev/null +++ b/game/src/gameLayer/gameController/LudoGameController.h @@ -0,0 +1,49 @@ +// +// Created by sebastian on 05.08.26. +// + +#ifndef COLORRACE_LUDOGAMECONTROLLER_H +#define COLORRACE_LUDOGAMECONTROLLER_H +#include + +#include "LudoGameControllerContext.h" +#include "core/animation/AnimationSystem.h" +#include "core/inputsOutputs/inputs/Keyboard.h" +#include "ecs/systems/PickingSystem.h" +#include "ludo/LudoAiController.h" +#include "ludo/LudoGameMode.h" +#include "ludo/PiecePresenter.h" +#include "ludo/PresentationGameState.h" +#include "ludo/ecs/highlight/HighlightSystem.h" + + +namespace ludo::GameController { + class LudoGameController { + public: + explicit LudoGameController(LudoGameControllerContext context); + static std::unique_ptr create(LudoGameControllerContext& context); + void update(float deltaTime, const engine::Mouse& mouse, const engine::Keyboard& keyboard); + void processEvents(); + bool isBlocking() const; + const PresentationGameState& getPresentationGameState() const { return m_presentationGameState; } + engine::PlayerID getLocalPlayer() const { return m_localPlayer; } + private: + std::shared_ptr m_gameMode; + engine::EntityRegistry& m_registry; + + PiecePresenter m_piecePresenter; + PresentationGameState m_presentationGameState; + + engine::animation::AnimationSystem m_animationSystem; + HighlightSystem m_highlightSystem; + engine::PickingSystem m_pickingSystem; + + std::vector m_aiControllers; + engine::PlayerID m_localPlayer; + + void handleInput(const engine::Mouse& mouse, const engine::Keyboard& keyboard); + }; +} + + +#endif //COLORRACE_LUDOGAMECONTROLLER_H diff --git a/game/src/gameLayer/gameController/LudoGameControllerContext.h b/game/src/gameLayer/gameController/LudoGameControllerContext.h new file mode 100644 index 0000000..aa1f435 --- /dev/null +++ b/game/src/gameLayer/gameController/LudoGameControllerContext.h @@ -0,0 +1,35 @@ +// +// Created by sebastian on 05.08.26. +// + +#ifndef COLORRACE_LUDOGAMECONTROLLERCONTEXT_H +#define COLORRACE_LUDOGAMECONTROLLERCONTEXT_H +#include + +#include "core/animation/events/AnimationMarkerEvent.h" +#include "core/audio/SoundBuffer.h" +#include "ecs/EntityRegistry.h" +#include "ludo/BoardLayout.h" +#include "ludo/LudoGameMode.h" +#include "renderer/entities/Camera.h" + +namespace ludo::GameController { + struct LudoGameControllerContext { + std::shared_ptr gameMode; + + engine::EntityRegistry& registry; + engine::Camera& camera; + + ludo::BoardLayout& boardLayout; + + engine::EventBus& eventBus; + engine::EventBus& animationEventBus; + + std::unordered_map> pieceModels; + + std::unordered_map> pieceSounds; + int localPlayer; + }; +} + +#endif //COLORRACE_LUDOGAMECONTROLLERCONTEXT_H diff --git a/game/src/ludo/PresentationGameState.cpp b/game/src/ludo/PresentationGameState.cpp index cf95451..4b3fd8c 100644 --- a/game/src/ludo/PresentationGameState.cpp +++ b/game/src/ludo/PresentationGameState.cpp @@ -7,11 +7,12 @@ #include "LudoGameMode.h" ludo::PresentationGameState::PresentationGameState(const LudoGameMode& gameMode, const LudoGameState &originalGameState) : - m_currentPlayer(gameMode.getCurrentPlayer()), m_diceValue(originalGameState.getDiceValue()), m_enableDiceRoll(false) { + m_currentPlayer(gameMode.getCurrentPlayer()), m_diceValue(originalGameState.getDiceValue()), m_enableDiceRoll(false), m_currentPlayerColor(gameMode.getColorOf(gameMode.getCurrentPlayer())) { } -void ludo::PresentationGameState::commitGameState(const engine::PlayerID currentPlayer, const int diceValue, const bool enableDiceRoll) { +void ludo::PresentationGameState::commitGameState(const engine::PlayerID currentPlayer, const ludo::PlayerColor color, const int diceValue, const bool enableDiceRoll) { m_currentPlayer = currentPlayer; m_diceValue = diceValue; m_enableDiceRoll = enableDiceRoll; + m_currentPlayerColor = color; } diff --git a/game/src/ludo/PresentationGameState.h b/game/src/ludo/PresentationGameState.h index 92dac47..5047b1a 100644 --- a/game/src/ludo/PresentationGameState.h +++ b/game/src/ludo/PresentationGameState.h @@ -11,13 +11,15 @@ namespace ludo { class PresentationGameState { public: explicit PresentationGameState(const LudoGameMode& gameMode, const LudoGameState& originalGameState); - void commitGameState(engine::PlayerID currentPlayer, int diceValue, bool enableDiceRoll); + void commitGameState(engine::PlayerID currentPlayer, ludo::PlayerColor color, int diceValue, bool enableDiceRoll); [[nodiscard]] engine::PlayerID getCurrentPlayer() const { return m_currentPlayer; } [[nodiscard]] int getDiceValue() const { return m_diceValue; } [[nodiscard]] bool isDiceRollEnabled() const { return m_enableDiceRoll; } + [[nodiscard]] ludo::PlayerColor getCurrentPlayerColor() const { return m_currentPlayerColor; } private: - engine::PlayerID m_currentPlayer ; + engine::PlayerID m_currentPlayer; + ludo::PlayerColor m_currentPlayerColor; int m_diceValue; bool m_enableDiceRoll; };