diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-14 11:26:12 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-14 11:26:12 +0100 |
commit | 01c09a196c3f3e5cefaa4119a95a1cdeb7b9c263 (patch) | |
tree | 5667369a85bd06b683c67de42bf0311c2647912b /src/crepe/system | |
parent | 6e13510f3c6d4155707f748d237bb1fa05243450 (diff) | |
parent | 8600b8a29351aae26ec7b22f84aeeef92d8cb421 (diff) |
merge `loek/cleanup` into `loek/audio`
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/AudioSystem.cpp | 5 | ||||
-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 | 2 | ||||
-rw-r--r-- | src/crepe/system/ParticleSystem.h | 82 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.cpp | 2 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.h | 2 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 16 | ||||
-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 | 25 | ||||
-rw-r--r-- | src/crepe/system/System.cpp | 6 | ||||
-rw-r--r-- | src/crepe/system/System.h | 21 |
15 files changed, 124 insertions, 113 deletions
diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index bf45362..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/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp index 9fdd7eb..67967ef 100644 --- a/src/crepe/system/AudioSystem.cpp +++ b/src/crepe/system/AudioSystem.cpp @@ -6,10 +6,9 @@ using namespace crepe; using namespace std; -AudioSystem::AudioSystem(SoundContext & ctx) : ctx(ctx) {} - void AudioSystem::update() { - vector<reference_wrapper<AudioSource>> components = this->compmgr.get_components_by_type<AudioSource>(); + ComponentManager & mgr = this->component_manager; + vector<reference_wrapper<AudioSource>> components = mgr.get_components_by_type<AudioSource>(); for (auto component_ref : components) { AudioSource & component = component_ref.get(); 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 e7a3bec..33db52e 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -13,7 +13,7 @@ using namespace crepe; void ParticleSystem::update() { // Get all emitters - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; std::vector<std::reference_wrapper<ParticleEmitter>> emitters = mgr.get_components_by_type<ParticleEmitter>(); diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index d7ca148..0acc2b9 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -4,67 +4,83 @@ #include "System.h" +#include "System.h" + namespace crepe { + class ParticleEmitter; class Transform; + /** - * \brief ParticleSystem class responsible for managing particle emission, updates, and bounds checking. + * \brief ParticleSystem class responsible for managing particle emission, + * updates, and bounds checking. */ class ParticleSystem : public System { public: + using System::System; /** - * \brief Updates all particle emitters by emitting particles, updating particle states, and checking bounds. - */ + * \brief Updates all particle emitters by emitting particles, updating + * particle states, and checking bounds. + */ void update() override; private: /** - * \brief Emits a particle from the specified emitter based on its emission properties. - * - * \param emitter Reference to the ParticleEmitter. - * \param transform Const reference to the Transform component associated with the emitter. - */ + * \brief Emits a particle from the specified emitter based on its emission + * properties. + * + * \param emitter Reference to the ParticleEmitter. + * \param transform Const reference to the Transform component associated + * with the emitter. + */ void emit_particle(ParticleEmitter & emitter, const Transform & transform); /** - * \brief Calculates the number of times particles should be emitted based on emission rate and update count. - * - * \param count Current update count. - * \param emission Emission rate. - * \return The number of particles to emit. - */ + * \brief Calculates the number of times particles should be emitted based on + * emission rate and update count. + * + * \param count Current update count. + * \param emission Emission rate. + * \return The number of particles to emit. + */ int calculate_update(int count, double emission) const; /** - * \brief Checks whether particles are within the emitter’s boundary, resets or stops particles if they exit. - * - * \param emitter Reference to the ParticleEmitter. - * \param transform Const reference to the Transform component associated with the emitter. - */ + * \brief Checks whether particles are within the emitter’s boundary, resets + * or stops particles if they exit. + * + * \param emitter Reference to the ParticleEmitter. + * \param transform Const reference to the Transform component associated + * with the emitter. + */ void check_bounds(ParticleEmitter & emitter, const Transform & transform); /** - * \brief Generates a random angle for particle emission within the specified range. - * - * \param min_angle Minimum emission angle in degrees. - * \param max_angle Maximum emission angle in degrees. - * \return Random angle in degrees. - */ + * \brief Generates a random angle for particle emission within the specified + * range. + * + * \param min_angle Minimum emission angle in degrees. + * \param max_angle Maximum emission angle in degrees. + * \return Random angle in degrees. + */ double generate_random_angle(double min_angle, double max_angle) const; /** - * \brief Generates a random speed for particle emission within the specified range. - * - * \param min_speed Minimum emission speed. - * \param max_speed Maximum emission speed. - * \return Random speed. - */ + * \brief Generates a random speed for particle emission within the specified + * range. + * + * \param min_speed Minimum emission speed. + * \param max_speed Maximum emission speed. + * \return Random speed. + */ double generate_random_speed(double min_speed, double max_speed) const; private: - //! Counter to count updates to determine how many times emit_particle is called. + //! Counter to count updates to determine how many times emit_particle is + // called. unsigned int update_count = 0; - //! Determines the lowest amount of emission rate (1000 = 0.001 = 1 particle per 1000 updates). + //! Determines the lowest amount of emission rate (1000 = 0.001 = 1 particle + // per 1000 updates). static constexpr unsigned int MAX_UPDATE_COUNT = 100; }; 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 038c120..5433a0f 100644 --- a/src/crepe/system/PhysicsSystem.h +++ b/src/crepe/system/PhysicsSystem.h @@ -3,6 +3,7 @@ #include "System.h" namespace crepe { + /** * \brief System that controls all physics * @@ -11,6 +12,7 @@ namespace crepe { */ class PhysicsSystem : public System { public: + using System::System; /** * \brief updates the physics system. * diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 10211a3..0d37808 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -5,21 +5,12 @@ #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" -#include "../util/log.h" +#include "../util/Log.h" #include "RenderSystem.h" 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 68fbb02..f4a826b 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,24 +12,33 @@ 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() { - forward_list<Script *> scripts = {}; +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 - = this->compmgr.get_components_by_type<BehaviorScript>(); + = mgr.get_components_by_type<BehaviorScript>(); for (auto behavior_script_ref : behavior_scripts) { BehaviorScript & behavior_script = behavior_script_ref.get(); 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 4fa6141..deb89cb 100644 --- a/src/crepe/system/ScriptSystem.h +++ b/src/crepe/system/ScriptSystem.h @@ -8,13 +8,32 @@ namespace crepe { class Script; +/** + * \brief Script system + * + * The script system is responsible for all \c BehaviorScript components, and + * calls the methods on classes derived from \c Script. + */ class ScriptSystem : public System { public: - void update(); + using System::System; + /** + * \brief Call Script::update() on all active \c BehaviorScript instances + * + * This routine updates all scripts sequentially using the Script::update() + * method. It also calls Script::init() if this has not been done before on + * the \c BehaviorScript instance. + */ + void update() override; private: - // TODO: to forward_list<reference_wrapper> - std::forward_list<Script *> get_scripts(); + /** + * \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<std::reference_wrapper<Script>> get_scripts() const; }; } // namespace crepe diff --git a/src/crepe/system/System.cpp b/src/crepe/system/System.cpp index fa51d2f..937a423 100644 --- a/src/crepe/system/System.cpp +++ b/src/crepe/system/System.cpp @@ -1,7 +1,7 @@ +#include "../util/Log.h" + #include "System.h" using namespace crepe; -// TODO: ComponentManager shouldn't be a singleton -System::System() : compmgr(ComponentManager::get_instance()) {} - +System::System(ComponentManager & mgr) : component_manager(mgr) { dbg_trace(); } diff --git a/src/crepe/system/System.h b/src/crepe/system/System.h index 5091977..36f7edc 100644 --- a/src/crepe/system/System.h +++ b/src/crepe/system/System.h @@ -4,19 +4,28 @@ namespace crepe { -//! ECS system base class +class ComponentManager; + +/** + * \brief Base ECS system class + * + * This class is used as the base for all system classes. Classes derived from + * System must implement the System::update() method and copy Script::Script + * with the `using`-syntax. + */ class System { public: - //! Process components belonging to this system + /** + * \brief Process all components this system is responsible for. + */ virtual void update() = 0; public: - System(); + System(ComponentManager &); virtual ~System() = default; -public: - //! Reference to component manager - ComponentManager & compmgr; +protected: + ComponentManager & component_manager; }; } // namespace crepe |