diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-11 21:19:57 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-12-11 21:19:57 +0100 |
commit | 000062b462a3af86db4dac4d8c9e5ef32feb2996 (patch) | |
tree | d766704f5862520ead6a03656103dd2fbcce99e9 /src/crepe/manager | |
parent | 359ad8db97305856f4cfdade1cd1dada78a7a635 (diff) |
split up loopmanager into SystemManager and Engine
Diffstat (limited to 'src/crepe/manager')
-rw-r--r-- | src/crepe/manager/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/crepe/manager/Mediator.h | 4 | ||||
-rw-r--r-- | src/crepe/manager/SystemManager.cpp | 45 | ||||
-rw-r--r-- | src/crepe/manager/SystemManager.h | 55 | ||||
-rw-r--r-- | src/crepe/manager/SystemManager.hpp | 40 |
5 files changed, 145 insertions, 2 deletions
diff --git a/src/crepe/manager/CMakeLists.txt b/src/crepe/manager/CMakeLists.txt index 459bbfa..aa19f3e 100644 --- a/src/crepe/manager/CMakeLists.txt +++ b/src/crepe/manager/CMakeLists.txt @@ -6,6 +6,7 @@ target_sources(crepe PUBLIC SceneManager.cpp ResourceManager.cpp ReplayManager.cpp + SystemManager.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES @@ -21,5 +22,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES ResourceManager.h ResourceManager.hpp ReplayManager.h + SystemManager.h + SystemManager.hpp ) diff --git a/src/crepe/manager/Mediator.h b/src/crepe/manager/Mediator.h index f5864e7..95ca4b5 100644 --- a/src/crepe/manager/Mediator.h +++ b/src/crepe/manager/Mediator.h @@ -14,7 +14,7 @@ class ResourceManager; class SDLContext; class LoopTimer; class ReplayManager; -class LoopManager; +class SystemManager; /** * Struct to pass references to classes that would otherwise need to be singletons down to @@ -37,7 +37,7 @@ struct Mediator { OptionalRef<ResourceManager> resource_manager; OptionalRef<LoopTimer> timer; OptionalRef<ReplayManager> replay_manager; - OptionalRef<LoopManager> loop_manager; + OptionalRef<SystemManager> system_manager; }; } // namespace crepe diff --git a/src/crepe/manager/SystemManager.cpp b/src/crepe/manager/SystemManager.cpp new file mode 100644 index 0000000..8fd80a7 --- /dev/null +++ b/src/crepe/manager/SystemManager.cpp @@ -0,0 +1,45 @@ +#include "../system/AISystem.h" +#include "../system/AnimatorSystem.h" +#include "../system/AudioSystem.h" +#include "../system/CollisionSystem.h" +#include "../system/InputSystem.h" +#include "../system/ParticleSystem.h" +#include "../system/PhysicsSystem.h" +#include "../system/RenderSystem.h" +#include "../system/ScriptSystem.h" +#include "../system/EventSystem.h" + +#include "SystemManager.h" + +using namespace crepe; +using namespace std; + +SystemManager::SystemManager(Mediator & mediator) : Manager(mediator) { + this->load_system<ScriptSystem>(); + this->load_system<AISystem>(); + this->load_system<PhysicsSystem>(); + this->load_system<CollisionSystem>(); + this->load_system<AnimatorSystem>(); + this->load_system<ParticleSystem>(); + this->load_system<RenderSystem>(); + this->load_system<InputSystem>(); + this->load_system<EventSystem>(); + this->load_system<AudioSystem>(); + + this->mediator.system_manager = *this; +} + +void SystemManager::fixed_update() { + for (auto & [type, system] : this->systems) { + if (!system->active) continue; + system->fixed_update(); + } +} + +void SystemManager::frame_update() { + for (auto & [type, system] : this->systems) { + if (!system->active) continue; + system->frame_update(); + } +} + diff --git a/src/crepe/manager/SystemManager.h b/src/crepe/manager/SystemManager.h new file mode 100644 index 0000000..5726a5c --- /dev/null +++ b/src/crepe/manager/SystemManager.h @@ -0,0 +1,55 @@ +#pragma once + +#include "../system/System.h" + +#include "Manager.h" + +namespace crepe { + +class SystemManager : public Manager { +public: + SystemManager(Mediator &); + + /** + * \brief Per-frame update. + * + * Updates the game state based on the elapsed time since the last frame. + */ + void frame_update(); + + /** + * \brief Fixed update executed at a fixed rate. + * + * This function updates physics and game logic based on LoopTimer's fixed_delta_time. + */ + void fixed_update(); + +private: + /** + * \brief Collection of System instances + * + * This map holds System instances indexed by the system's class typeid. It is filled in the + * constructor of \c SystemManager using SystemManager::load_system. + */ + std::unordered_map<std::type_index, std::unique_ptr<System>> systems; + /** + * \brief Initialize a system + * \tparam T System type (must be derivative of \c System) + */ + template <class T> + void load_system(); + +public: + /** + * \brief Retrieve a reference to ECS system + * \tparam T System type + * \returns Reference to system instance + * \throws std::runtime_error if the System is not initialized + */ + template <class T> + T & get_system(); +}; + +} // namespace crepe + +#include "SystemManager.hpp" diff --git a/src/crepe/manager/SystemManager.hpp b/src/crepe/manager/SystemManager.hpp new file mode 100644 index 0000000..46ada5f --- /dev/null +++ b/src/crepe/manager/SystemManager.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include <memory> +#include <cassert> +#include <format> + +#include "SystemManager.h" + +namespace crepe { + +template <class T> +T & SystemManager::get_system() { + using namespace std; + static_assert(is_base_of<System, T>::value, "get_system must recieve a derivative class of System"); + + const type_info & type = typeid(T); + if (!this->systems.contains(type)) + throw runtime_error(format("SystemManager: {} is not initialized", type.name())); + + System * system = this->systems.at(type).get(); + T * concrete_system = dynamic_cast<T *>(system); + assert(concrete_system != nullptr); + + return *concrete_system; +} + +template <class T> +void SystemManager::load_system() { + using namespace std; + static_assert(is_base_of<System, T>::value, + "load_system must recieve a derivative class of System"); + + const type_info & type = typeid(T); + if (this->systems.contains(type)) + throw runtime_error(format("SystemManager: {} is already initialized", type.name())); + System * system = new T(this->mediator); + this->systems[type] = unique_ptr<System>(system); +} + +} // namespace crepe |