9#include "sceneConcept.hpp"
28template <SceneIdentifier TSceneId,
typename TDependency,
typename TInput,
30 requires((Scene<TScenes, TSceneId, TInput> && ...) &&
31 (std::constructible_from<TScenes, TDependency &> && ...))
42 template <
typename TInitialScene>
43 SceneManager(TDependency &dependency, std::in_place_type_t<TInitialScene>)
44 : _dependency(dependency),
45 _currentScene(std::in_place_type<TInitialScene>, dependency) {}
52 std::visit([&](
auto &scene) { scene.onEnter(context); }, _currentScene);
63 const auto requestedScene = std::visit(
64 [&](
auto &scene) {
return scene.onUpdate(context, input, dt); },
67 if (requestedScene.has_value())
68 switchScene(context, *requestedScene);
76 std::visit([&](
auto &scene) { scene.onExit(context); }, _currentScene);
84 template <
typename TScene>
85 requires(std::same_as<TScene, TScenes> || ...)
86 [[nodiscard]] TScene *
get() noexcept {
87 return std::get_if<TScene>(&_currentScene);
95 template <
typename TScene>
96 requires(std::same_as<TScene, TScenes> || ...)
97 [[nodiscard]]
const TScene *
get() const noexcept {
98 return std::get_if<TScene>(&_currentScene);
102 template <
typename TScene>
bool trySwitchScene(TSceneId nextScene) {
103 if (nextScene != TScene::Type)
106 _currentScene.template emplace<TScene>(_dependency);
110 void switchScene(EngineContext &context, TSceneId nextScene) {
113 const bool found = (trySwitchScene<TScenes>(nextScene) || ...);
116 throw std::runtime_error(
"Unknown scene");
121 TDependency &_dependency;
122 SceneVariant _currentScene;
Holds the active scene and switches between a fixed set of scenes.
Definition sceneManager.hpp:32
std::variant< TScenes... > SceneVariant
Variant holding the active scene.
Definition sceneManager.hpp:35
void start(EngineContext &context)
Calls onEnter() on the active scene.
Definition sceneManager.hpp:51
TScene * get() noexcept
Returns the active scene if it has the given type.
Definition sceneManager.hpp:86
void update(EngineContext &context, const TInput &input, float dt)
Updates the active scene and switches if it asks to.
Definition sceneManager.hpp:62
void stop(EngineContext &context)
Calls onExit() on the active scene.
Definition sceneManager.hpp:75
SceneManager(TDependency &dependency, std::in_place_type_t< TInitialScene >)
Constructs the initial scene without entering it.
Definition sceneManager.hpp:43
const TScene * get() const noexcept
Returns the active scene if it has the given type.
Definition sceneManager.hpp:97
Engine services handed to the game and its scenes.
Definition EngineContext.hpp:20