aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/ParticleSystem.cpp
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-10-23 15:21:56 +0200
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-10-23 15:21:56 +0200
commitadb7dfabec4811566308cd072e0542cd7eae8cc1 (patch)
tree669c19049121d7fa841e4e9287eceb2c71860ebb /src/crepe/ParticleSystem.cpp
parent194c8c4af559f9ea35b2fc53103415ea1e47fb61 (diff)
parent5445331293854aac26af2d5c6a20cedeaa819383 (diff)
Merge branch 'jaro/poc-physics' into niels/rendering
Diffstat (limited to 'src/crepe/ParticleSystem.cpp')
-rw-r--r--src/crepe/ParticleSystem.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/crepe/ParticleSystem.cpp b/src/crepe/ParticleSystem.cpp
new file mode 100644
index 0000000..0dad4cd
--- /dev/null
+++ b/src/crepe/ParticleSystem.cpp
@@ -0,0 +1,59 @@
+#include "ParticleSystem.h"
+#include <cmath>
+#include <ctime>
+#include "ParticleEmitter.h"
+#include "Particle.h"
+#include "ComponentManager.h"
+
+using namespace crepe;
+
+ParticleSystem::ParticleSystem() : m_elapsedTime(0.0f) {} // Initialize m_elapsedTime to 0
+
+void ParticleSystem::update() {
+ ComponentManager& mgr = ComponentManager::get_instance();
+ std::vector<std::reference_wrapper<ParticleEmitter>> emitters = mgr.get_components_by_type<ParticleEmitter>();
+ float deltaTime = 0.10;
+ for (ParticleEmitter& emitter : emitters) {
+ float updateAmount = 1/static_cast<float>(emitter.m_emissionRate);
+ for (float i = 0; i < deltaTime; i += updateAmount)
+ {
+ emitParticle(emitter);
+ }
+ for (size_t j = 0; j < emitter.particles.size(); j++)
+ {
+ if(emitter.particles[j].active)
+ {
+ emitter.particles[j].update(deltaTime);
+ }
+ }
+ }
+}
+
+void ParticleSystem::emitParticle(ParticleEmitter& emitter) {
+ Position initialPosition = { emitter.m_position.x, emitter.m_position.y};
+ float randomAngle = 0.0f;
+ if(emitter.m_maxAngle < emitter.m_minAngle)
+ {
+ randomAngle = ((emitter.m_minAngle + (std::rand() % (static_cast<uint32_t>(emitter.m_maxAngle + 360 - emitter.m_minAngle + 1))))%360);
+ }
+ else
+ {
+ randomAngle = emitter.m_minAngle + (std::rand() % (static_cast<uint32_t>(emitter.m_maxAngle - emitter.m_minAngle + 1)));
+ }
+ float angleInRadians = randomAngle * (M_PI / 180.0f);
+ float randomSpeedOffset = (static_cast<float>(std::rand()) / RAND_MAX) * (2 * emitter.m_speedOffset) - emitter.m_speedOffset;
+ float velocityX = (emitter.m_speed + randomSpeedOffset) * std::cos(angleInRadians);
+ float velocityY = (emitter.m_speed + randomSpeedOffset) * std::sin(angleInRadians);
+ Position initialVelocity = {
+ velocityX,
+ velocityY
+ };
+ for (size_t i = 0; i < emitter.particles.size(); i++)
+ {
+ if(!emitter.particles[i].active)
+ {
+ emitter.particles[i].reset(emitter.m_endLifespan, initialPosition, initialVelocity);
+ break;
+ }
+ }
+}