aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system
diff options
context:
space:
mode:
authormax-001 <maxsmits21@kpnmail.nl>2024-11-05 16:34:00 +0100
committermax-001 <maxsmits21@kpnmail.nl>2024-11-05 16:34:00 +0100
commit3d27e696185d6f4dfc9e978517ed17844044c5bc (patch)
treeaf65fdcdb44a1f9e6a84a294f267e6f743fea599 /src/crepe/system
parent6071387c2bbe6d36a95c113ad137e8e2ce80be27 (diff)
parentae6a103946e437ca85cc69c5fc2cbf68d35ffeae (diff)
Merge remote-tracking branch 'origin/max/gameobject' into max/scenes
Diffstat (limited to 'src/crepe/system')
-rw-r--r--src/crepe/system/CMakeLists.txt15
-rw-r--r--src/crepe/system/CollisionSystem.cpp7
-rw-r--r--src/crepe/system/CollisionSystem.h11
-rw-r--r--src/crepe/system/ParticleSystem.cpp62
-rw-r--r--src/crepe/system/ParticleSystem.h18
-rw-r--r--src/crepe/system/PhysicsSystem.cpp61
-rw-r--r--src/crepe/system/PhysicsSystem.h11
-rw-r--r--src/crepe/system/RenderSystem.cpp41
-rw-r--r--src/crepe/system/RenderSystem.h17
-rw-r--r--src/crepe/system/ScriptSystem.cpp46
-rw-r--r--src/crepe/system/ScriptSystem.h25
-rw-r--r--src/crepe/system/System.h22
12 files changed, 336 insertions, 0 deletions
diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt
new file mode 100644
index 0000000..ff6f66f
--- /dev/null
+++ b/src/crepe/system/CMakeLists.txt
@@ -0,0 +1,15 @@
+target_sources(crepe PUBLIC
+ ParticleSystem.cpp
+ ScriptSystem.cpp
+ PhysicsSystem.cpp
+ CollisionSystem.cpp
+ RenderSystem.cpp
+)
+
+target_sources(crepe PUBLIC FILE_SET HEADERS FILES
+ System.h
+ ScriptSystem.h
+ PhysicsSystem.h
+ CollisionSystem.h
+ RenderSystem.h
+)
diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp
new file mode 100644
index 0000000..55e0fdc
--- /dev/null
+++ b/src/crepe/system/CollisionSystem.cpp
@@ -0,0 +1,7 @@
+#include "CollisionSystem.h"
+
+using namespace crepe;
+
+CollisionSystem::CollisionSystem() {}
+
+void CollisionSystem::update() {}
diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h
new file mode 100644
index 0000000..1e9f1aa
--- /dev/null
+++ b/src/crepe/system/CollisionSystem.h
@@ -0,0 +1,11 @@
+#pragma once
+
+namespace crepe {
+
+class CollisionSystem {
+public:
+ CollisionSystem();
+ void update();
+};
+
+} // namespace crepe
diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp
new file mode 100644
index 0000000..397b586
--- /dev/null
+++ b/src/crepe/system/ParticleSystem.cpp
@@ -0,0 +1,62 @@
+#include <cmath>
+#include <ctime>
+
+#include "../ComponentManager.h"
+#include "../api/ParticleEmitter.h"
+
+#include "ParticleSystem.h"
+
+using namespace crepe;
+
+ParticleSystem::ParticleSystem() : elapsed_time(0.0f) {}
+
+void ParticleSystem::update() {
+ ComponentManager & mgr = ComponentManager::get_instance();
+ std::vector<std::reference_wrapper<ParticleEmitter>> emitters
+ = mgr.get_components_by_type<ParticleEmitter>();
+ float delta_time = 0.10;
+ for (ParticleEmitter & emitter : emitters) {
+ float update_amount = 1 / static_cast<float>(emitter.emission_rate);
+ for (float i = 0; i < delta_time; i += update_amount) {
+ emit_particle(emitter);
+ }
+ for (size_t j = 0; j < emitter.particles.size(); j++) {
+ if (emitter.particles[j].active) {
+ emitter.particles[j].update(delta_time);
+ }
+ }
+ }
+}
+
+void ParticleSystem::emit_particle(ParticleEmitter & emitter) {
+ Position initial_position = {emitter.position.x, emitter.position.y};
+ float random_angle = 0.0f;
+ if (emitter.max_angle < emitter.min_angle) {
+ random_angle = ((emitter.min_angle
+ + (std::rand()
+ % (static_cast<uint32_t>(emitter.max_angle + 360
+ - emitter.min_angle + 1))))
+ % 360);
+ } else {
+ random_angle = emitter.min_angle
+ + (std::rand()
+ % (static_cast<uint32_t>(emitter.max_angle
+ - emitter.min_angle + 1)));
+ }
+ float angle_in_radians = random_angle * (M_PI / 180.0f);
+ float random_speed_offset = (static_cast<float>(std::rand()) / RAND_MAX)
+ * (2 * emitter.speed_offset)
+ - emitter.speed_offset;
+ float velocity_x
+ = (emitter.speed + random_speed_offset) * std::cos(angle_in_radians);
+ float velocity_y
+ = (emitter.speed + random_speed_offset) * std::sin(angle_in_radians);
+ Position initial_velocity = {velocity_x, velocity_y};
+ for (size_t i = 0; i < emitter.particles.size(); i++) {
+ if (!emitter.particles[i].active) {
+ emitter.particles[i].reset(emitter.end_lifespan, initial_position,
+ initial_velocity);
+ break;
+ }
+ }
+}
diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h
new file mode 100644
index 0000000..3ac1d3f
--- /dev/null
+++ b/src/crepe/system/ParticleSystem.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include "../api/ParticleEmitter.h"
+
+namespace crepe {
+
+class ParticleSystem {
+public:
+ ParticleSystem();
+ void update();
+
+private:
+ void emit_particle(ParticleEmitter & emitter); //emits a new particle
+
+ float elapsed_time; //elapsed time since the last emission
+};
+
+} // namespace crepe
diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp
new file mode 100644
index 0000000..cea8062
--- /dev/null
+++ b/src/crepe/system/PhysicsSystem.cpp
@@ -0,0 +1,61 @@
+#include <iostream>
+
+#include "../ComponentManager.h"
+#include "../api/Force.h"
+#include "../api/Rigidbody.h"
+#include "../api/Transform.h"
+
+#include "PhysicsSystem.h"
+
+using namespace crepe;
+
+PhysicsSystem::PhysicsSystem() {}
+
+void PhysicsSystem::update() {
+ ComponentManager & mgr = ComponentManager::get_instance();
+ std::vector<std::reference_wrapper<Rigidbody>> rigidbodies
+ = mgr.get_components_by_type<Rigidbody>();
+ std::vector<std::reference_wrapper<Transform>> transforms
+ = mgr.get_components_by_type<Transform>();
+
+ for (Rigidbody & rigidbody : rigidbodies) {
+
+ switch (rigidbody.body_type) {
+ case BodyType::DYNAMIC:
+ for (Transform & transform : transforms) {
+ if (transform.game_object_id == rigidbody.game_object_id) {
+ rigidbody.velocity_x = 0;
+ rigidbody.velocity_y = 0;
+ std::vector<std::reference_wrapper<Force>> forces
+ = mgr.get_components_by_id<Force>(
+ rigidbody.game_object_id);
+ rigidbody.velocity_y
+ += rigidbody.gravity_scale * 1 * rigidbody.mass;
+
+ for (Force & force : forces) {
+ rigidbody.velocity_x += force.force_x;
+ rigidbody.velocity_y += force.force_y;
+ }
+
+ std::cout << "before transform.postion.x "
+ << transform.position.x << std::endl;
+ std::cout << "before transform.postion.y "
+ << transform.position.y << std::endl;
+ transform.position.x += rigidbody.velocity_x;
+ transform.position.y += rigidbody.velocity_y;
+ std::cout << "after transform.postion.x "
+ << transform.position.x << std::endl;
+ std::cout << "after transform.postion.y "
+ << transform.position.y << std::endl;
+ }
+ }
+ break;
+ case BodyType::KINEMATIC:
+ break; //(scripts)
+ case BodyType::STATIC:
+ break; //(unmoveable objects)
+ default:
+ break;
+ }
+ }
+}
diff --git a/src/crepe/system/PhysicsSystem.h b/src/crepe/system/PhysicsSystem.h
new file mode 100644
index 0000000..33b4072
--- /dev/null
+++ b/src/crepe/system/PhysicsSystem.h
@@ -0,0 +1,11 @@
+#pragma once
+
+namespace crepe {
+
+class PhysicsSystem {
+public:
+ PhysicsSystem();
+ void update();
+};
+
+} // namespace crepe
diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp
new file mode 100644
index 0000000..5a07cc2
--- /dev/null
+++ b/src/crepe/system/RenderSystem.cpp
@@ -0,0 +1,41 @@
+#include <functional>
+#include <vector>
+
+#include "../ComponentManager.h"
+#include "../api/Sprite.h"
+#include "../api/Transform.h"
+#include "../facade/SDLContext.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::update() {
+
+ ComponentManager & mgr = ComponentManager::get_instance();
+
+ std::vector<std::reference_wrapper<Sprite>> sprites
+ = mgr.get_components_by_type<Sprite>();
+
+ SDLContext & render = SDLContext::get_instance();
+ render.clear_screen();
+
+ for (const Sprite & sprite : sprites) {
+ std::vector<std::reference_wrapper<Transform>> transforms
+ = mgr.get_components_by_id<Transform>(sprite.game_object_id);
+ for (const Transform & transform : transforms) {
+ render.draw(sprite, transform);
+ }
+ }
+ render.present_screen();
+}
diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h
new file mode 100644
index 0000000..4b910a4
--- /dev/null
+++ b/src/crepe/system/RenderSystem.h
@@ -0,0 +1,17 @@
+#pragma once
+
+#include "System.h"
+
+namespace crepe {
+
+class RenderSystem : public System {
+
+public:
+ static RenderSystem & get_instance();
+ void update();
+
+private:
+ RenderSystem();
+ ~RenderSystem();
+};
+} // namespace crepe
diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp
new file mode 100644
index 0000000..f1fae4d
--- /dev/null
+++ b/src/crepe/system/ScriptSystem.cpp
@@ -0,0 +1,46 @@
+#include <forward_list>
+#include <functional>
+#include <vector>
+
+#include "../ComponentManager.h"
+#include "../api/BehaviorScript.h"
+#include "../api/Script.h"
+#include "../util/log.h"
+
+#include "ScriptSystem.h"
+
+using namespace std;
+using namespace crepe;
+
+ScriptSystem::ScriptSystem() { dbg_trace(); }
+ScriptSystem::~ScriptSystem() { dbg_trace(); }
+
+ScriptSystem & ScriptSystem::get_instance() {
+ static ScriptSystem instance;
+ return instance;
+}
+
+void ScriptSystem::update() {
+ using namespace std;
+ dbg_trace();
+
+ forward_list<Script *> scripts = this->get_scripts();
+ for (Script * script : scripts) script->update();
+}
+
+forward_list<Script *> ScriptSystem::get_scripts() {
+ forward_list<Script *> scripts = {};
+ ComponentManager & mgr = ComponentManager::get_instance();
+ vector<reference_wrapper<BehaviorScript>> behavior_scripts
+ = 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);
+ }
+
+ return scripts;
+}
diff --git a/src/crepe/system/ScriptSystem.h b/src/crepe/system/ScriptSystem.h
new file mode 100644
index 0000000..32e793c
--- /dev/null
+++ b/src/crepe/system/ScriptSystem.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <forward_list>
+
+#include "System.h"
+
+namespace crepe {
+
+class Script;
+
+class ScriptSystem : public System {
+public:
+ static ScriptSystem & get_instance();
+ void update();
+
+private:
+ ScriptSystem();
+ ~ScriptSystem();
+
+private:
+ // TODO: to forward_list<reference_wrapper>
+ std::forward_list<Script *> get_scripts();
+};
+
+} // namespace crepe
diff --git a/src/crepe/system/System.h b/src/crepe/system/System.h
new file mode 100644
index 0000000..8744920
--- /dev/null
+++ b/src/crepe/system/System.h
@@ -0,0 +1,22 @@
+#pragma once
+
+namespace crepe {
+
+class System {
+public:
+ static System & get_instance();
+ virtual void update() = 0;
+
+protected:
+ System(){};
+ virtual ~System(){};
+
+private:
+ // singleton
+ System(const System &) = delete;
+ System(System &&) = delete;
+ System & operator=(const System &) = delete;
+ System & operator=(System &&) = delete;
+};
+
+} // namespace crepe