aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/ParticleEmitter.cpp
diff options
context:
space:
mode:
authorjaroWMR <jarorutjes07@gmail.com>2024-10-01 19:10:24 +0200
committerjaroWMR <jarorutjes07@gmail.com>2024-10-01 19:10:24 +0200
commitbd24f87e18bef29679023ee9a60028f2afdbb470 (patch)
treeaa17d05db8ba6d5f959e676aa2c51c67743108b3 /src/crepe/ParticleEmitter.cpp
parenta78f7bbfcdabad9550afe22f615b973b92cb074f (diff)
particle POC
Diffstat (limited to 'src/crepe/ParticleEmitter.cpp')
-rw-r--r--src/crepe/ParticleEmitter.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/crepe/ParticleEmitter.cpp b/src/crepe/ParticleEmitter.cpp
new file mode 100644
index 0000000..13aaeae
--- /dev/null
+++ b/src/crepe/ParticleEmitter.cpp
@@ -0,0 +1,40 @@
+#include "ParticleEmitter.hpp"
+
+ParticleEmitter::ParticleEmitter(int maxParticles, float emissionRate)
+ : maxParticles(maxParticles), emissionRate(emissionRate), elapsedTime(0.0f) {
+ position = { 0, 0 };
+ std::srand(static_cast<unsigned int>(std::time(nullptr))); /
+}
+
+void ParticleEmitter::update(float deltaTime) {
+ elapsedTime += deltaTime;
+
+ while (elapsedTime >= (1.0f / emissionRate) && particles.size() < maxParticles) {
+ elapsedTime -= (1.0f / emissionRate);
+ emitParticle();
+ }
+
+ for (auto it = particles.begin(); it != particles.end();) {
+ it->update(deltaTime);
+ if (!it->isAlive()) {
+ it = particles.erase(it);
+ } else {
+ ++it;
+ }
+ }
+}
+
+void ParticleEmitter::emitParticle() {
+ Particle::Position initialPosition = { position.x, position.y };
+ Particle::Position initialVelocity = {
+ static_cast<int>(randFloat(-50.0f, 50.0f, 10.0f, 100.0f)),
+ static_cast<int>(randFloat(-50.0f, 50.0f, 10.0f, 100.0f))
+ };
+ particles.emplace_back(2.0f, initialPosition, initialVelocity);
+}
+
+float ParticleEmitter::randFloat(float minangle, float maxangle, float minVel, float maxVel) {
+ float angle = static_cast<float>(rand()) / RAND_MAX * (maxangle - minangle) + minangle;
+ float velocity = static_cast<float>(rand()) / RAND_MAX * (maxVel - minVel) + minVel;
+ return velocity * std::cos(angle);
+}