diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-18 14:27:58 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-18 14:27:58 +0100 |
commit | 03e493012edc106b00bc5676c1830ce2085a7ff9 (patch) | |
tree | ff06d4314552e82f98d08c52d7a15edb3a74436e /src/crepe/system | |
parent | 21f5b66d0bcc13d903800adf3bb614e380ff8591 (diff) | |
parent | 121b64b1cb6cfead5814070c8b0185d3d7308095 (diff) |
Merge remote-tracking branch 'origin/master' into niels/sorting_rendering
Diffstat (limited to 'src/crepe/system')
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 12 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.h | 20 | ||||
-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 | 47 | ||||
-rw-r--r-- | src/crepe/system/ParticleSystem.h | 77 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.cpp | 24 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.h | 6 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 30 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 19 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.cpp | 22 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.h | 25 | ||||
-rw-r--r-- | src/crepe/system/System.cpp | 7 | ||||
-rw-r--r-- | src/crepe/system/System.h | 17 |
15 files changed, 157 insertions, 160 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..56cc7b3 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -17,28 +17,16 @@ 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. * * This method is called periodically (likely every frame) to update the state of all - * Animator components, moving the animations forward and managing their behavior (e.g., looping). + * 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 e7a3bec..7316309 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -13,20 +13,17 @@ 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>(); for (ParticleEmitter & emitter : emitters) { // Get transform linked to emitter const Transform & transform - = mgr.get_components_by_id<Transform>(emitter.game_object_id) - .front() - .get(); + = mgr.get_components_by_id<Transform>(emitter.game_object_id).front().get(); // Emit particles based on emission_rate - int updates - = calculate_update(this->update_count, emitter.data.emission_rate); + int updates = calculate_update(this->update_count, emitter.data.emission_rate); for (size_t i = 0; i < updates; i++) { emit_particle(emitter, transform); } @@ -45,8 +42,7 @@ void ParticleSystem::update() { this->update_count = (this->update_count + 1) % this->MAX_UPDATE_COUNT; } -void ParticleSystem::emit_particle(ParticleEmitter & emitter, - const Transform & transform) { +void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & transform) { constexpr double DEG_TO_RAD = M_PI / 180.0; Vector2 initial_position = emitter.data.position + transform.position; @@ -57,13 +53,13 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, = generate_random_speed(emitter.data.min_speed, emitter.data.max_speed); double angle_radians = random_angle * DEG_TO_RAD; - Vector2 velocity = {random_speed * std::cos(angle_radians), - random_speed * std::sin(angle_radians)}; + Vector2 velocity + = {random_speed * std::cos(angle_radians), random_speed * std::sin(angle_radians)}; for (Particle & particle : emitter.data.particles) { if (!particle.active) { - particle.reset(emitter.data.end_lifespan, initial_position, - velocity, random_angle); + particle.reset(emitter.data.end_lifespan, initial_position, velocity, + random_angle); break; } } @@ -81,10 +77,8 @@ int ParticleSystem::calculate_update(int count, double emission) const { return static_cast<int>(emission); } -void ParticleSystem::check_bounds(ParticleEmitter & emitter, - const Transform & transform) { - Vector2 offset = emitter.data.boundary.offset + transform.position - + emitter.data.position; +void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & transform) { + Vector2 offset = emitter.data.boundary.offset + transform.position + emitter.data.position; double half_width = emitter.data.boundary.width / 2.0; double half_height = emitter.data.boundary.height / 2.0; @@ -95,8 +89,8 @@ void ParticleSystem::check_bounds(ParticleEmitter & emitter, for (Particle & particle : emitter.data.particles) { const Vector2 & position = particle.position; - bool within_bounds = (position.x >= LEFT && position.x <= RIGHT - && position.y >= TOP && position.y <= BOTTOM); + bool within_bounds = (position.x >= LEFT && position.x <= RIGHT && position.y >= TOP + && position.y <= BOTTOM); if (!within_bounds) { if (emitter.data.boundary.reset_on_exit) { @@ -112,30 +106,25 @@ void ParticleSystem::check_bounds(ParticleEmitter & emitter, } } -double ParticleSystem::generate_random_angle(double min_angle, - double max_angle) const { +double ParticleSystem::generate_random_angle(double min_angle, double max_angle) const { if (min_angle == max_angle) { return min_angle; } else if (min_angle < max_angle) { return min_angle - + static_cast<double>(std::rand() - % static_cast<int>(max_angle - min_angle)); + + static_cast<double>(std::rand() % static_cast<int>(max_angle - min_angle)); } else { double angle_offset = (360 - min_angle) + max_angle; - double random_angle = min_angle - + static_cast<double>( - std::rand() % static_cast<int>(angle_offset)); + double random_angle + = min_angle + static_cast<double>(std::rand() % static_cast<int>(angle_offset)); return (random_angle >= 360) ? random_angle - 360 : random_angle; } } -double ParticleSystem::generate_random_speed(double min_speed, - double max_speed) const { +double ParticleSystem::generate_random_speed(double min_speed, double max_speed) const { if (min_speed == max_speed) { return min_speed; } else { return min_speed - + static_cast<double>(std::rand() - % static_cast<int>(max_speed - min_speed)); + + static_cast<double>(std::rand() % static_cast<int>(max_speed - min_speed)); } } diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index d7ca148..c647284 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -5,66 +5,75 @@ #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..4a7dbfb 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 @@ -29,17 +29,15 @@ void PhysicsSystem::update() { // Add gravity if (rigidbody.data.use_gravity) { rigidbody.data.linear_velocity.y - += (rigidbody.data.mass - * rigidbody.data.gravity_scale * gravity); + += (rigidbody.data.mass * rigidbody.data.gravity_scale + * gravity); } // Add damping if (rigidbody.data.angular_damping != 0) { - rigidbody.data.angular_velocity - *= rigidbody.data.angular_damping; + rigidbody.data.angular_velocity *= rigidbody.data.angular_damping; } if (rigidbody.data.linear_damping != Vector2{0, 0}) { - rigidbody.data.linear_velocity - *= rigidbody.data.linear_damping; + rigidbody.data.linear_velocity *= rigidbody.data.linear_damping; } // Max velocity check @@ -75,21 +73,17 @@ void PhysicsSystem::update() { // Move object if (!rigidbody.data.constraints.rotation) { - transform.rotation - += rigidbody.data.angular_velocity; - transform.rotation - = std::fmod(transform.rotation, 360.0); + transform.rotation += rigidbody.data.angular_velocity; + transform.rotation = std::fmod(transform.rotation, 360.0); if (transform.rotation < 0) { transform.rotation += 360.0; } } if (!rigidbody.data.constraints.x) { - transform.position.x - += rigidbody.data.linear_velocity.x; + transform.position.x += rigidbody.data.linear_velocity.x; } if (!rigidbody.data.constraints.y) { - transform.position.y - += rigidbody.data.linear_velocity.y; + transform.position.y += rigidbody.data.linear_velocity.y; } } } diff --git a/src/crepe/system/PhysicsSystem.h b/src/crepe/system/PhysicsSystem.h index 038c120..227ab69 100644 --- a/src/crepe/system/PhysicsSystem.h +++ b/src/crepe/system/PhysicsSystem.h @@ -3,14 +3,16 @@ #include "System.h" namespace crepe { + /** * \brief System that controls all physics * - * This class is a physics system that uses a rigidbody and transform - * to add physics to a game object. + * This class is a physics system that uses a rigidbody and transform to add physics to a game + * object. */ 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 718036c..aa9910b 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -7,33 +7,19 @@ #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" -#include "../util/log.h" -#include "Asset.h" +#include "../util/Log.h" #include "RenderSystem.h" using namespace crepe; -RenderSystem::RenderSystem() { dbg_trace(); } +void RenderSystem::clear_screen() const { SDLContext::get_instance().clear_screen(); } -RenderSystem::~RenderSystem() { dbg_trace(); } - -RenderSystem & RenderSystem::get_instance() { - static RenderSystem instance; - return instance; -} - -void RenderSystem::clear_screen() const { - SDLContext::get_instance().clear_screen(); -} - -void RenderSystem::present_screen() const { - SDLContext::get_instance().present_screen(); -} +void RenderSystem::present_screen() const { SDLContext::get_instance().present_screen(); } void RenderSystem::update_camera() { - ComponentManager & mgr = ComponentManager::get_instance(); - std::vector<std::reference_wrapper<Camera>> cameras - = mgr.get_components_by_type<Camera>(); + ComponentManager & mgr = this->component_manager; + + std::vector<std::reference_wrapper<Camera>> cameras = mgr.get_components_by_type<Camera>(); for (Camera & cam : cameras) { SDLContext::get_instance().camera(cam); @@ -63,6 +49,7 @@ RenderSystem::sort(std::vector<std::reference_wrapper<Sprite>> & objs) { } void RenderSystem::render_sprites() { + ComponentManager & mgr = this->component_manager; ComponentManager & mgr = ComponentManager::get_instance(); @@ -72,8 +59,7 @@ void RenderSystem::render_sprites() { SDLContext & render = SDLContext::get_instance(); for (const Sprite & sprite : sorted_sprites) { - auto transforms - = mgr.get_components_by_id<Transform>(sprite.game_object_id); + auto transforms = mgr.get_components_by_id<Transform>(sprite.game_object_id); render.draw(sprite, transforms[0], *curr_cam); } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index a83633a..7661cbd 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -13,19 +13,13 @@ namespace crepe { * \class RenderSystem * \brief Manages rendering operations for all game objects. * - * RenderSystem is responsible for rendering sprites, clearing and presenting the screen, - * and managing the active camera. It functions as a singleton, providing centralized - * rendering services for the application. + * RenderSystem is responsible for rendering sprites, clearing and presenting the screen, and + * managing the active camera. It functions as a singleton, providing centralized 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. @@ -33,10 +27,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; @@ -67,4 +57,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 f2673e7..c4d724c 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,23 @@ 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(); + if (!script.initialized) { + script.init(); + script.initialized = true; + } + script.update(); + } } -forward_list<Script *> ScriptSystem::get_scripts() { - 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,7 +37,7 @@ forward_list<Script *> ScriptSystem::get_scripts() { 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 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 3b81bef..28ea20e 100644 --- a/src/crepe/system/System.h +++ b/src/crepe/system/System.h @@ -2,13 +2,28 @@ namespace crepe { +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: + /** + * \brief Process all components this system is responsible for. + */ virtual void update() = 0; public: - System() = default; + System(ComponentManager &); virtual ~System() = default; + +protected: + ComponentManager & component_manager; }; } // namespace crepe |