diff --git a/CMakeLists.txt b/CMakeLists.txt index 79111a2..4dfabde 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -276,6 +276,8 @@ add_library(GameLib STATIC game/src/AudioLayer.h game/src/ludo/DiceSource.cpp game/src/ludo/DiceSource.h + game/src/ludo/PresentationGameState.cpp + game/src/ludo/PresentationGameState.h ) target_include_directories(GameLib PUBLIC game/src) target_link_libraries(GameLib PUBLIC Engine) diff --git a/game/src/GameLayer.cpp b/game/src/GameLayer.cpp index a885b82..dc90f9b 100644 --- a/game/src/GameLayer.cpp +++ b/game/src/GameLayer.cpp @@ -29,6 +29,8 @@ void GameLayer::onAttachImpl() { 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() { @@ -37,20 +39,23 @@ void GameLayer::onDetachImpl() { void GameLayer::renderHud() { const auto& state = m_gameMode->getState(); - bool isMyTurn = m_gameMode->getCurrentPlayer() == m_localPlayer; + + ImGui::SetNextWindowPos(ImVec2(20, 20), ImGuiCond_FirstUseEver); ImGui::Begin("Ludo", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize); - ImGui::Text("Am Zug: %s", toString(m_gameMode->getColorOf(m_gameMode->getCurrentPlayer())).data()); - ImGui::Text("Wuerfel: %d", state.getDiceValue()); + 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(!isMyTurn || state.getPhase() != ludo::TurnPhase::AwaitingRoll); + 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()); + // + // if (!m_lastEventMessage.empty()) ImGui::Text("%s", m_lastEventMessage.c_str()); ImGui::EndDisabled(); @@ -71,7 +76,7 @@ void GameLayer::onUpdate(float 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); } @@ -82,7 +87,7 @@ void GameLayer::onUpdate(float deltaTime) { m_piecePresenter.onPieceMoved(e, m_gameMode->getState()); } if constexpr (std::is_same_v) { - m_lastEventMessage = std::format("Wuerfel geworfen: {}", e.diceValue); + // m_lastEventMessage = std::format("Wuerfel geworfen: {}", e.diceValue); } // DiceRolledEvent/TurnChangedEvent -> ImGui-State aktualisieren }, event); diff --git a/game/src/GameLayer.h b/game/src/GameLayer.h index c9b8ed3..e0ec6d6 100644 --- a/game/src/GameLayer.h +++ b/game/src/GameLayer.h @@ -20,15 +20,17 @@ #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::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_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); @@ -45,6 +47,7 @@ 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; @@ -54,9 +57,10 @@ private: 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::string m_lastEventMessage; + std::vector aiControllers; engine::animation::AnimationSystem m_animationSystem; diff --git a/game/src/GameScene.cpp b/game/src/GameScene.cpp index 463b399..dddf635 100644 --- a/game/src/GameScene.cpp +++ b/game/src/GameScene.cpp @@ -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)); + addLayer(std::make_unique(getAnimationEventBus(), m_gameMode, entityRegistry, m_camera, m_pointLight, m_keyboard, m_mouse, assetManager->getModel("gameboard"), pieceModels, pieceSounds, m_eventBus)); } void GameScene::onExit() { diff --git a/game/src/ludo/PresentationGameState.cpp b/game/src/ludo/PresentationGameState.cpp new file mode 100644 index 0000000..cf95451 --- /dev/null +++ b/game/src/ludo/PresentationGameState.cpp @@ -0,0 +1,17 @@ +// +// Created by sebastian on 05.08.26. +// + +#include "PresentationGameState.h" + +#include "LudoGameMode.h" + +ludo::PresentationGameState::PresentationGameState(const LudoGameMode& gameMode, const LudoGameState &originalGameState) : + m_currentPlayer(gameMode.getCurrentPlayer()), m_diceValue(originalGameState.getDiceValue()), m_enableDiceRoll(false) { +} + +void ludo::PresentationGameState::commitGameState(const engine::PlayerID currentPlayer, const int diceValue, const bool enableDiceRoll) { + m_currentPlayer = currentPlayer; + m_diceValue = diceValue; + m_enableDiceRoll = enableDiceRoll; +} diff --git a/game/src/ludo/PresentationGameState.h b/game/src/ludo/PresentationGameState.h new file mode 100644 index 0000000..92dac47 --- /dev/null +++ b/game/src/ludo/PresentationGameState.h @@ -0,0 +1,27 @@ +// +// Created by sebastian on 05.08.26. +// + +#ifndef COLORRACE_PRESENTATIONGAMESTATE_H +#define COLORRACE_PRESENTATIONGAMESTATE_H +#include "LudoGameState.h" +#include "LudoGameMode.h" + +namespace ludo { + class PresentationGameState { + public: + explicit PresentationGameState(const LudoGameMode& gameMode, const LudoGameState& originalGameState); + void commitGameState(engine::PlayerID currentPlayer, 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; } + private: + engine::PlayerID m_currentPlayer ; + int m_diceValue; + bool m_enableDiceRoll; + }; +} + + +#endif //COLORRACE_PRESENTATIONGAMESTATE_H