From 5f75bdbf3d38c94baeae52f4c4889f147ec6885e Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 5 Nov 2024 15:34:31 +0100 Subject: move systems + update contributing.md --- src/crepe/system/CMakeLists.txt | 15 +++++++++ src/crepe/system/CollisionSystem.cpp | 7 ++++ src/crepe/system/CollisionSystem.h | 11 +++++++ src/crepe/system/ParticleSystem.cpp | 62 ++++++++++++++++++++++++++++++++++++ src/crepe/system/ParticleSystem.h | 18 +++++++++++ src/crepe/system/PhysicsSystem.cpp | 62 ++++++++++++++++++++++++++++++++++++ src/crepe/system/PhysicsSystem.h | 11 +++++++ src/crepe/system/RenderSystem.cpp | 42 ++++++++++++++++++++++++ src/crepe/system/RenderSystem.h | 17 ++++++++++ src/crepe/system/ScriptSystem.cpp | 47 +++++++++++++++++++++++++++ src/crepe/system/ScriptSystem.h | 26 +++++++++++++++ src/crepe/system/System.h | 22 +++++++++++++ 12 files changed, 340 insertions(+) create mode 100644 src/crepe/system/CMakeLists.txt create mode 100644 src/crepe/system/CollisionSystem.cpp create mode 100644 src/crepe/system/CollisionSystem.h create mode 100644 src/crepe/system/ParticleSystem.cpp create mode 100644 src/crepe/system/ParticleSystem.h create mode 100644 src/crepe/system/PhysicsSystem.cpp create mode 100644 src/crepe/system/PhysicsSystem.h create mode 100644 src/crepe/system/RenderSystem.cpp create mode 100644 src/crepe/system/RenderSystem.h create mode 100644 src/crepe/system/ScriptSystem.cpp create mode 100644 src/crepe/system/ScriptSystem.h create mode 100644 src/crepe/system/System.h (limited to 'src/crepe/system') 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 +#include + +#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> emitters + = mgr.get_components_by_type(); + float delta_time = 0.10; + for (ParticleEmitter & emitter : emitters) { + float update_amount = 1 / static_cast(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(emitter.max_angle + 360 + - emitter.min_angle + 1)))) + % 360); + } else { + random_angle = emitter.min_angle + + (std::rand() + % (static_cast(emitter.max_angle + - emitter.min_angle + 1))); + } + float angle_in_radians = random_angle * (M_PI / 180.0f); + float random_speed_offset = (static_cast(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 + +#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> rigidbodies + = mgr.get_components_by_type(); + std::vector> transforms + = mgr.get_components_by_type(); + + 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> forces + = mgr.get_components_by_id( + 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 +#include + +#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> sprites + = mgr.get_components_by_type(); + + SDLContext & render = SDLContext::get_instance(); + render.clear_screen(); + + for (const Sprite & sprite : sprites) { + std::vector> transforms + = mgr.get_components_by_id(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 +#include +#include + +#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