diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-05 15:34:31 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-11-05 15:34:31 +0100 |
commit | 5f75bdbf3d38c94baeae52f4c4889f147ec6885e (patch) | |
tree | 5fc76975e41ae106df61a4d59559f0ab37fb52ac /src/crepe/system | |
parent | 9572c5b35de2d13dbe7f942e3ecc50d28b36e9b8 (diff) |
move systems + update contributing.md
Diffstat (limited to 'src/crepe/system')
-rw-r--r-- | src/crepe/system/CMakeLists.txt | 15 | ||||
-rw-r--r-- | src/crepe/system/CollisionSystem.cpp | 7 | ||||
-rw-r--r-- | src/crepe/system/CollisionSystem.h | 11 | ||||
-rw-r--r-- | src/crepe/system/ParticleSystem.cpp | 62 | ||||
-rw-r--r-- | src/crepe/system/ParticleSystem.h | 18 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.cpp | 62 | ||||
-rw-r--r-- | src/crepe/system/PhysicsSystem.h | 11 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 42 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 17 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.cpp | 47 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.h | 26 | ||||
-rw-r--r-- | src/crepe/system/System.h | 22 |
12 files changed, 340 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..367c7d8 --- /dev/null +++ b/src/crepe/system/ParticleSystem.cpp @@ -0,0 +1,62 @@ +#include <cmath> +#include <ctime> + +#include "../api/ParticleEmitter.h" +#include "../ComponentManager.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..6da714f --- /dev/null +++ b/src/crepe/system/PhysicsSystem.cpp @@ -0,0 +1,62 @@ +#include <iostream> + +#include "../api/Force.h" +#include "../api/Rigidbody.h" +#include "../api/Transform.h" +#include "../ComponentManager.h" + +#include "PhysicsSystem.h" + +using namespace crepe; +using namespace crepe::api; + +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..599087a --- /dev/null +++ b/src/crepe/system/RenderSystem.cpp @@ -0,0 +1,42 @@ +#include <functional> +#include <vector> + +#include "../api/Sprite.h" +#include "../api/Transform.h" +#include "../util/log.h" +#include "../SDLContext.h" +#include "../ComponentManager.h" + +#include "RenderSystem.h" + +using namespace crepe; +using namespace crepe::api; + +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..277b29d --- /dev/null +++ b/src/crepe/system/ScriptSystem.cpp @@ -0,0 +1,47 @@ +#include <forward_list> +#include <functional> +#include <vector> + +#include "../api/BehaviorScript.h" +#include "../api/Script.h" +#include "../util/log.h" +#include "../ComponentManager.h" + +#include "ScriptSystem.h" + +using namespace std; +using namespace crepe; +using namespace crepe::api; + +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..1f472a0 --- /dev/null +++ b/src/crepe/system/ScriptSystem.h @@ -0,0 +1,26 @@ +#pragma once + +#include <forward_list> + +#include "System.h" + +namespace crepe::api { +class Script; +} + +namespace crepe { + +class ScriptSystem : public System { +public: + static ScriptSystem & get_instance(); + void update(); + +private: + ScriptSystem(); + ~ScriptSystem(); + +private: + std::forward_list<api::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..ecbb7f5 --- /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 |