aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-18 15:34:22 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-18 15:34:22 +0100
commitb92da3c729c8fc7c002cb3acbb75c56cb63bd89e (patch)
tree454b64d31b7e0c9151da88a0d49ee782565f49e9 /src/crepe/system
parent8b449e764b5c91cd3cb0c4fa404a290ab295a7ef (diff)
parent121b64b1cb6cfead5814070c8b0185d3d7308095 (diff)
Merge branch 'master' of https://github.com/lonkaars/crepe into wouter/events
Diffstat (limited to 'src/crepe/system')
-rw-r--r--src/crepe/system/AnimatorSystem.cpp12
-rw-r--r--src/crepe/system/AnimatorSystem.h17
-rw-r--r--src/crepe/system/CMakeLists.txt1
-rw-r--r--src/crepe/system/CollisionSystem.cpp2
-rw-r--r--src/crepe/system/CollisionSystem.h8
-rw-r--r--src/crepe/system/ParticleSystem.cpp2
-rw-r--r--src/crepe/system/ParticleSystem.h4
-rw-r--r--src/crepe/system/PhysicsSystem.cpp2
-rw-r--r--src/crepe/system/PhysicsSystem.h2
-rw-r--r--src/crepe/system/RenderSystem.cpp16
-rw-r--r--src/crepe/system/RenderSystem.h13
-rw-r--r--src/crepe/system/ScriptSystem.cpp22
-rw-r--r--src/crepe/system/ScriptSystem.h25
-rw-r--r--src/crepe/system/System.cpp7
-rw-r--r--src/crepe/system/System.h17
15 files changed, 80 insertions, 70 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 29204d3..56cc7b3 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.
*
@@ -35,11 +26,7 @@ public:
* 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 92e7d3f..7316309 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 843261e..c647284 100644
--- a/src/crepe/system/ParticleSystem.h
+++ b/src/crepe/system/ParticleSystem.h
@@ -15,6 +15,7 @@ class Transform;
*/
class ParticleSystem : public System {
public:
+ using System::System;
/**
* \brief Updates all particle emitters by emitting particles, updating particle states, and
* checking bounds.
@@ -68,7 +69,8 @@ private:
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).
diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp
index 402dfab..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
diff --git a/src/crepe/system/PhysicsSystem.h b/src/crepe/system/PhysicsSystem.h
index b635693..227ab69 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 a488370..fa3d0de 100644
--- a/src/crepe/system/RenderSystem.cpp
+++ b/src/crepe/system/RenderSystem.cpp
@@ -5,26 +5,17 @@
#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(); }
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>();
@@ -34,8 +25,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 b9bf94b..87ec494 100644
--- a/src/crepe/system/RenderSystem.h
+++ b/src/crepe/system/RenderSystem.h
@@ -15,14 +15,8 @@ namespace crepe {
* 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 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