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/ParticleSystem.cpp | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/crepe/system/ParticleSystem.cpp (limited to 'src/crepe/system/ParticleSystem.cpp') 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; + } + } +} -- cgit v1.2.3 From 74533e9fbcd0360950daec4dc297e48e30a8a2d0 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 5 Nov 2024 15:51:01 +0100 Subject: `make format` --- src/crepe/api/Sprite.cpp | 2 +- src/crepe/api/Texture.cpp | 2 +- src/crepe/system/ParticleSystem.cpp | 2 +- src/crepe/system/PhysicsSystem.cpp | 2 +- src/crepe/system/RenderSystem.cpp | 4 ++-- src/crepe/system/ScriptSystem.cpp | 2 +- src/crepe/util/LogColor.h | 2 +- src/crepe/util/log.h | 3 +-- src/example/physics.cpp | 2 +- src/example/rendering.cpp | 2 +- 10 files changed, 11 insertions(+), 12 deletions(-) (limited to 'src/crepe/system/ParticleSystem.cpp') diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 3fd6018..3dd44f2 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -3,9 +3,9 @@ #include "../util/log.h" -#include "Texture.h" #include "Component.h" #include "Sprite.h" +#include "Texture.h" using namespace std; using namespace crepe; diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index b84a3c6..b5001a6 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -1,7 +1,7 @@ #include -#include "../util/log.h" #include "../SDLContext.h" +#include "../util/log.h" #include "Asset.h" #include "Texture.h" diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 367c7d8..397b586 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -1,8 +1,8 @@ #include #include -#include "../api/ParticleEmitter.h" #include "../ComponentManager.h" +#include "../api/ParticleEmitter.h" #include "ParticleSystem.h" diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index efec750..cea8062 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -1,9 +1,9 @@ #include +#include "../ComponentManager.h" #include "../api/Force.h" #include "../api/Rigidbody.h" #include "../api/Transform.h" -#include "../ComponentManager.h" #include "PhysicsSystem.h" diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index b3c53db..ee3cdf2 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -1,11 +1,11 @@ #include #include +#include "../ComponentManager.h" +#include "../SDLContext.h" #include "../api/Sprite.h" #include "../api/Transform.h" #include "../util/log.h" -#include "../SDLContext.h" -#include "../ComponentManager.h" #include "RenderSystem.h" diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index 731adae..f1fae4d 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -2,10 +2,10 @@ #include #include +#include "../ComponentManager.h" #include "../api/BehaviorScript.h" #include "../api/Script.h" #include "../util/log.h" -#include "../ComponentManager.h" #include "ScriptSystem.h" diff --git a/src/crepe/util/LogColor.h b/src/crepe/util/LogColor.h index 7e60ba2..c1170cb 100644 --- a/src/crepe/util/LogColor.h +++ b/src/crepe/util/LogColor.h @@ -48,4 +48,4 @@ private: std::string final = ""; }; -} // namespace crepe::util +} // namespace crepe diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h index b13b9cc..5a1cf00 100644 --- a/src/crepe/util/log.h +++ b/src/crepe/util/log.h @@ -17,8 +17,7 @@ // NOLINTBEGIN #define dbg_logf(fmt, ...) \ _crepe_logf_here(crepe::LogLevel::DEBUG, ": " fmt, __VA_ARGS__) -#define dbg_log(str) \ - _crepe_logf_here(crepe::LogLevel::DEBUG, "%s: " str, "") +#define dbg_log(str) _crepe_logf_here(crepe::LogLevel::DEBUG, "%s: " str, "") #define dbg_trace() _crepe_logf_here(crepe::LogLevel::TRACE, "%s", "") // NOLINTEND diff --git a/src/example/physics.cpp b/src/example/physics.cpp index d3a0f32..2dbddc2 100644 --- a/src/example/physics.cpp +++ b/src/example/physics.cpp @@ -2,13 +2,13 @@ #include #include -#include #include #include #include #include #include #include +#include using namespace crepe; using namespace std; diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 9c8d60c..b0ab60a 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -1,6 +1,6 @@ #include -#include #include +#include #include #include -- cgit v1.2.3