ADD: Synchronize Hud with Animations

This commit is contained in:
sebastian 2026-08-05 13:41:25 +02:00
parent 64671363dc
commit cdc6a1f92c
6 changed files with 67 additions and 12 deletions

View File

@ -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)

View File

@ -29,6 +29,8 @@ void GameLayer::onAttachImpl() {
m_cameraController = std::make_unique<engine::CameraController>(getInputStack(), getInputContext());
m_piecePresenter.spawnPieces(m_gameMode->getState());
this->m_presentationGameState = std::make_unique<ludo::PresentationGameState>(*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<T, ludo::DiceRolledEvent>) {
m_lastEventMessage = std::format("Wuerfel geworfen: {}", e.diceValue);
// m_lastEventMessage = std::format("Wuerfel geworfen: {}", e.diceValue);
}
// DiceRolledEvent/TurnChangedEvent -> ImGui-State aktualisieren
}, event);

View File

@ -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<engine::animation::AnimationMarkerEvent>& animationEventbus, std::shared_ptr<ludo::LudoGameMode> gameMode, engine::EntityRegistry& entityRegistry, std::shared_ptr<engine::Camera> cam, std::shared_ptr<engine::PointLight> light,
engine::Keyboard& keyboard, engine::Mouse& mouse, std::shared_ptr<engine::Model> boardModel, std::unordered_map<std::string, std::shared_ptr<engine::Model>> pieceModdels, std::unordered_map<std::string, std::shared_ptr<engine::audio::SoundBuffer>> pieceSounds)
engine::Keyboard& keyboard, engine::Mouse& mouse, std::shared_ptr<engine::Model> boardModel, std::unordered_map<std::string, std::shared_ptr<engine::Model>> pieceModdels, std::unordered_map<std::string, std::shared_ptr<engine::audio::SoundBuffer>> pieceSounds,
engine::EventBus<ludo::LudoEvent>& eventBus)
: Layer(keyboard, mouse, animationEventbus), m_gameMode(std::move(gameMode)), m_entityRegistry(entityRegistry), m_camera(std::move(cam)), m_renderer(std::make_unique<engine::MeshRenderer>()),
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<ludo::LudoEvent>& m_eventBus;
std::shared_ptr<engine::PointLight> m_pointLight;
std::shared_ptr<engine::Camera> m_camera;
std::unique_ptr<engine::MeshRenderer> m_renderer;
@ -54,9 +57,10 @@ private:
ludo::PiecePresenter m_piecePresenter;
engine::PlayerID m_localPlayer = 0;
std::shared_ptr<ludo::LudoGameMode> m_gameMode;
std::unique_ptr<ludo::PresentationGameState> m_presentationGameState;
engine::PickingSystem m_pickingSystem;
ludo::HighlightSystem m_highlightSystem;
std::string m_lastEventMessage;
std::vector<ludo::LudoAiController> aiControllers;
engine::animation::AnimationSystem m_animationSystem;

View File

@ -44,7 +44,7 @@ void GameScene::onEnter() {
addLayer(std::make_unique<ludo::AudioLayer>(*assetManager, m_keyboard, m_mouse, m_gameMode, entityRegistry, m_eventBus, getAnimationEventBus()));
addLayer(std::make_unique<engine::DebugLayer>(getAnimationEventBus(), *m_camera, entityRegistry, m_keyboard, m_mouse, engine::SphereFactory::createUVSphere(1.0f)));
addLayer(std::make_unique<GameLayer>(getAnimationEventBus(), m_gameMode, entityRegistry, m_camera, m_pointLight, m_keyboard, m_mouse, assetManager->getModel("gameboard"), pieceModels, pieceSounds));
addLayer(std::make_unique<GameLayer>(getAnimationEventBus(), m_gameMode, entityRegistry, m_camera, m_pointLight, m_keyboard, m_mouse, assetManager->getModel("gameboard"), pieceModels, pieceSounds, m_eventBus));
}
void GameScene::onExit() {

View File

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

View File

@ -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