ADD: Generic GameMode and GameState

This commit is contained in:
sebastian 2026-08-02 14:45:39 +02:00
parent 92e3092d3b
commit 7d1774e222
7 changed files with 97 additions and 0 deletions

View File

@ -147,6 +147,12 @@ add_library(Engine STATIC
engine/src/core/inputsOutputs/context/InputContextStack.h
engine/src/core/inputsOutputs/controller/CameraController.cpp
engine/src/core/inputsOutputs/controller/CameraController.h
engine/src/game/GameState.cpp
engine/src/game/GameState.h
engine/src/game/GameMode.cpp
engine/src/game/GameMode.h
engine/src/game/TurnBasedGameMode.cpp
engine/src/game/TurnBasedGameMode.h
)
target_include_directories(Engine PUBLIC engine/src)
target_link_libraries(Engine PUBLIC OpenGL::GL glfw glad glm::glm imgui spdlog::spdlog tinyobjloader stb_image)

View File

@ -0,0 +1,5 @@
//
// Created by sebastian on 02.08.26.
//
#include "GameMode.h"

View File

@ -0,0 +1,28 @@
//
// Created by sebastian on 02.08.26.
//
#ifndef COLORRACE_GAMEMODE_H
#define COLORRACE_GAMEMODE_H
#include <type_traits>
#include <vector>
#include "GameState.h"
namespace engine {
template <typename Command, typename Event, typename State>
class GameMode {
public:
static_assert(std::is_base_of_v<GameState, State>, "State must be derived from GameState");
virtual ~GameMode() = default;
virtual void sendCommand(const Command& command) = 0;
virtual std::vector<Event> pollEvents() = 0;
[[nodiscard]] virtual const State& getState() const = 0;
};
}
#endif //COLORRACE_GAMEMODE_H

View File

@ -0,0 +1,5 @@
//
// Created by sebastian on 02.08.26.
//
#include "GameState.h"

View File

@ -0,0 +1,17 @@
//
// Created by sebastian on 02.08.26.
//
#ifndef COLORRACE_GAMESTATE_H
#define COLORRACE_GAMESTATE_H
namespace engine {
class GameState {
public:
virtual ~GameState() = default;
};
}
#endif //COLORRACE_GAMESTATE_H

View File

@ -0,0 +1,5 @@
//
// Created by sebastian on 02.08.26.
//
#include "TurnBasedGameMode.h"

View File

@ -0,0 +1,31 @@
//
// Created by sebastian on 02.08.26.
//
#ifndef COLORRACE_TURNBASEDGAMEMODE_H
#define COLORRACE_TURNBASEDGAMEMODE_H
#include <cstdint>
#include "GameMode.h"
namespace engine {
using PlayerID = uint32_t;
template<typename Command, typename Event, typename State>
class TurnBasedGameMode : public GameMode<Command, Event, State> {
public:
explicit TurnBasedGameMode(std::vector<PlayerID> playerOrder) : m_playerOrder(std::move(playerOrder)) {}
[[nodiscard]] PlayerID getCurrentPlayer() const { return m_playerOrder[m_currentPlayerIndex]; }
[[nodiscard]] bool isCurrentPlayer(PlayerID playerID) const { return getCurrentPlayer() == playerID; }
protected:
void endTurn(bool extraTurn = false) {
if (!extraTurn) m_currentPlayerIndex = (m_currentPlayerIndex + 1) % m_playerOrder.size();
}
private:
std::vector<PlayerID> m_playerOrder;
size_t m_currentPlayerIndex = 0;
};
}
#endif //COLORRACE_TURNBASEDGAMEMODE_H