aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/ParticleSystem.cpp
diff options
context:
space:
mode:
authormax-001 <maxsmits21@kpnmail.nl>2024-11-05 16:29:18 +0100
committermax-001 <maxsmits21@kpnmail.nl>2024-11-05 16:29:18 +0100
commitae6a103946e437ca85cc69c5fc2cbf68d35ffeae (patch)
treea0bd09748c68950353f05d245bed4de470548fc6 /src/crepe/system/ParticleSystem.cpp
parenta5d3564f6d051986376c98abb9c098a8a7183fe0 (diff)
parent5b248d068a94902be9ca4d00fe07d551f64c49b9 (diff)
Merge remote-tracking branch 'origin/master' into max/gameobject
Diffstat (limited to 'src/crepe/system/ParticleSystem.cpp')
-rw-r--r--src/crepe/system/ParticleSystem.cpp62
1 files changed, 62 insertions, 0 deletions
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;
+ }
+ }
+}