diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-13 16:26:38 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-13 16:26:38 +0100 |
commit | b63f4700f7bda696afb14cc3111be0f8b0eed458 (patch) | |
tree | 08c4b00b249a4fa672d97a5bc79927adac0a0257 /src/crepe/system | |
parent | 2933655dea64f11f200f42fe51e58dacc5f160eb (diff) | |
parent | 9e87a556a5f68c5f9bb04bef9a66880536ccd6e8 (diff) |
merge `loek/tests` into `loek/cleanup` (close #32)
Diffstat (limited to 'src/crepe/system')
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 12 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.h | 17 | ||||
-rw-r--r-- | src/crepe/system/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/crepe/system/CollisionSystem.cpp | 2 | ||||
-rw-r--r-- | src/crepe/system/CollisionSystem.h | 8 | ||||
-rw-r--r-- | src/crepe/system/ParticleSystem.cpp | 4 | ||||
-rw-r--r-- | src/crepe/system/ParticleSystem.h | 15 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.cpp | 2 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.h | 11 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 14 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 13 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.cpp | 24 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.h | 6 | ||||
-rw-r--r-- | src/crepe/system/System.cpp | 7 | ||||
-rw-r--r-- | src/crepe/system/System.h | 7 |
15 files changed, 62 insertions, 81 deletions
diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 1c101fa..9d18873 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,27 +1,17 @@ - #include <cstdint> #include <functional> #include <vector> #include "api/Animator.h" #include "facade/SDLContext.h" -#include "util/Log.h" #include "AnimatorSystem.h" #include "ComponentManager.h" using namespace crepe; -AnimatorSystem::AnimatorSystem() { dbg_trace(); } -AnimatorSystem::~AnimatorSystem() { dbg_trace(); } - -AnimatorSystem & AnimatorSystem::get_instance() { - static AnimatorSystem instance; - return instance; -} - void AnimatorSystem::update() { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; std::vector<std::reference_wrapper<Animator>> animations = mgr.get_components_by_type<Animator>(); diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index 969e9d1..aa97084 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -17,16 +17,7 @@ namespace crepe { class AnimatorSystem : public System { public: - /** - * \brief Retrieves the singleton instance of the AnimatorSystem. - * - * \return A reference to the single instance of the AnimatorSystem. - * - * This method ensures that there is only one instance of the AnimatorSystem, following the - * singleton design pattern. It can be used to access the system globally. - */ - static AnimatorSystem & get_instance(); - + using System::System; /** * \brief Updates the Animator components. * @@ -34,11 +25,7 @@ public: * Animator components, moving the animations forward and managing their behavior (e.g., looping). */ void update() override; - -private: - // private because singleton - AnimatorSystem(); // dbg_trace - ~AnimatorSystem(); // dbg_trace + // FIXME: never say "likely" in the documentation lmao }; } // namespace crepe diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt index 4c18b87..d658b25 100644 --- a/src/crepe/system/CMakeLists.txt +++ b/src/crepe/system/CMakeLists.txt @@ -1,4 +1,5 @@ target_sources(crepe PUBLIC + System.cpp ParticleSystem.cpp ScriptSystem.cpp PhysicsSystem.cpp diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 55e0fdc..c74ca1d 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -2,6 +2,4 @@ using namespace crepe; -CollisionSystem::CollisionSystem() {} - void CollisionSystem::update() {} diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index 1e9f1aa..c1a70d8 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -1,11 +1,13 @@ #pragma once +#include "System.h" + namespace crepe { -class CollisionSystem { +class CollisionSystem : public System { public: - CollisionSystem(); - void update(); + using System::System; + void update() override; }; } // namespace crepe diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 397b586..fe02682 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -8,10 +8,8 @@ using namespace crepe; -ParticleSystem::ParticleSystem() : elapsed_time(0.0f) {} - void ParticleSystem::update() { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; std::vector<std::reference_wrapper<ParticleEmitter>> emitters = mgr.get_components_by_type<ParticleEmitter>(); float delta_time = 0.10; diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index 3ac1d3f..f171c62 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -2,17 +2,22 @@ #include "../api/ParticleEmitter.h" +#include "System.h" + namespace crepe { -class ParticleSystem { +class ParticleSystem : public System { public: - ParticleSystem(); - void update(); + using System::System; + void update() override; private: - void emit_particle(ParticleEmitter & emitter); //emits a new particle + //! Emits a new particle + void emit_particle(ParticleEmitter & emitter); - float elapsed_time; //elapsed time since the last emission + //! Elapsed time since the last emission + float elapsed_time = 0.0; + // TODO: to std::chrono::duration }; } // namespace crepe diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index eb54ad3..da79707 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -11,7 +11,7 @@ using namespace crepe; void PhysicsSystem::update() { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; std::vector<std::reference_wrapper<Rigidbody>> rigidbodies = mgr.get_components_by_type<Rigidbody>(); std::vector<std::reference_wrapper<Transform>> transforms diff --git a/src/crepe/system/PhysicsSystem.h b/src/crepe/system/PhysicsSystem.h index cb6160e..5433a0f 100644 --- a/src/crepe/system/PhysicsSystem.h +++ b/src/crepe/system/PhysicsSystem.h @@ -1,5 +1,7 @@ #pragma once +#include "System.h" + namespace crepe { /** @@ -8,18 +10,15 @@ namespace crepe { * This class is a physics system that uses a rigidbody and transform * to add physics to a game object. */ -class PhysicsSystem { +class PhysicsSystem : public System { public: - /** - * Constructor is default - */ - PhysicsSystem() = default; + using System::System; /** * \brief updates the physics system. * * It calculates new velocties and changes the postion in the transform. */ - void update(); + void update() override; }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 3ff5b4f..0d37808 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -11,15 +11,6 @@ using namespace crepe; -RenderSystem::RenderSystem() { dbg_trace(); } - -RenderSystem::~RenderSystem() { dbg_trace(); } - -RenderSystem & RenderSystem::get_instance() { - static RenderSystem instance; - return instance; -} - void RenderSystem::clear_screen() const { SDLContext::get_instance().clear_screen(); } @@ -28,7 +19,7 @@ void RenderSystem::present_screen() const { SDLContext::get_instance().present_screen(); } void RenderSystem::update_camera() { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; std::vector<std::reference_wrapper<Camera>> cameras = mgr.get_components_by_type<Camera>(); @@ -39,8 +30,7 @@ void RenderSystem::update_camera() { } } void RenderSystem::render_sprites() const { - - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; std::vector<std::reference_wrapper<Sprite>> sprites = mgr.get_components_by_type<Sprite>(); diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 70db21a..da4e910 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -15,14 +15,8 @@ namespace crepe { * rendering services for the application. */ class RenderSystem : public System { - public: - /** - * \brief Gets the singleton instance of RenderSystem. - * \return Reference to the RenderSystem instance. - */ - static RenderSystem & get_instance(); - + using System::System; /** * \brief Updates the RenderSystem for the current frame. * This method is called to perform all rendering operations for the current game frame. @@ -30,10 +24,6 @@ public: void update() override; private: - // Private constructor to enforce singleton pattern. - RenderSystem(); - ~RenderSystem(); - //! Clears the screen in preparation for rendering. void clear_screen() const; @@ -61,4 +51,5 @@ private: Camera * curr_cam = nullptr; // TODO: needs a better solution }; + } // namespace crepe diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index 807ad7f..7b22533 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -5,7 +5,6 @@ #include "../ComponentManager.h" #include "../api/BehaviorScript.h" #include "../api/Script.h" -#include "../util/Log.h" #include "ScriptSystem.h" @@ -13,16 +12,24 @@ using namespace std; using namespace crepe; void ScriptSystem::update() { - using namespace std; dbg_trace(); - forward_list<Script *> scripts = this->get_scripts(); - for (Script * script : scripts) script->update(); + forward_list<reference_wrapper<Script>> scripts = this->get_scripts(); + + for (auto & script_ref : scripts) { + Script & script = script_ref.get(); + BehaviorScript & component = *script.parent_ref; + if (!component.initialized) { + script.init(); + component.initialized = true; + } + script.update(); + } } -forward_list<Script *> ScriptSystem::get_scripts() const { - forward_list<Script *> scripts = {}; - ComponentManager & mgr = ComponentManager::get_instance(); +forward_list<reference_wrapper<Script>> ScriptSystem::get_scripts() const { + forward_list<reference_wrapper<Script>> scripts = {}; + ComponentManager & mgr = this->component_manager; vector<reference_wrapper<BehaviorScript>> behavior_scripts = mgr.get_components_by_type<BehaviorScript>(); @@ -31,8 +38,9 @@ forward_list<Script *> ScriptSystem::get_scripts() const { if (!behavior_script.active) continue; Script * script = behavior_script.script.get(); if (script == nullptr) continue; - scripts.push_front(script); + scripts.push_front(*script); } return scripts; } + diff --git a/src/crepe/system/ScriptSystem.h b/src/crepe/system/ScriptSystem.h index 9d57640..deb89cb 100644 --- a/src/crepe/system/ScriptSystem.h +++ b/src/crepe/system/ScriptSystem.h @@ -16,6 +16,7 @@ class Script; */ class ScriptSystem : public System { public: + using System::System; /** * \brief Call Script::update() on all active \c BehaviorScript instances * @@ -23,17 +24,16 @@ public: * method. It also calls Script::init() if this has not been done before on * the \c BehaviorScript instance. */ - void update(); + void update() override; private: - // TODO: to forward_list<reference_wrapper> /** * \brief Aggregate all active \c BehaviorScript components and return a list * of references to their \c Script instances (utility) * * \returns List of active \c Script instances */ - std::forward_list<Script *> get_scripts() const; + std::forward_list<std::reference_wrapper<Script>> get_scripts() const; }; } // namespace crepe diff --git a/src/crepe/system/System.cpp b/src/crepe/system/System.cpp new file mode 100644 index 0000000..937a423 --- /dev/null +++ b/src/crepe/system/System.cpp @@ -0,0 +1,7 @@ +#include "../util/Log.h" + +#include "System.h" + +using namespace crepe; + +System::System(ComponentManager & mgr) : component_manager(mgr) { dbg_trace(); } diff --git a/src/crepe/system/System.h b/src/crepe/system/System.h index ec4507f..28ea20e 100644 --- a/src/crepe/system/System.h +++ b/src/crepe/system/System.h @@ -2,6 +2,8 @@ namespace crepe { +class ComponentManager; + /** * \brief Base ECS system class * @@ -17,8 +19,11 @@ public: virtual void update() = 0; public: - System() = default; + System(ComponentManager &); virtual ~System() = default; + +protected: + ComponentManager & component_manager; }; } // namespace crepe |