-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticleSystem.cpp
73 lines (58 loc) · 2.04 KB
/
ParticleSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "ParticleSystem.h"
#include <iostream>
using namespace std;
#include <cmath>
#include <unordered_map>
ParticleSystem::ParticleSystem() : isRunning(false) {}
std::unordered_map<float, int> positionXCounts;
std::vector<Particle> particles;
void ParticleSystem::start() {
isRunning = true;
}
void ParticleSystem::stop() {
isRunning = false;
}
void ParticleSystem::update(float deltaTime, std::string type) {
if (!isRunning) return;
windowWidth = 1920;
windowHeight = 1080;
float positionX;
std::vector<Particle> newParticles; // Temporary vector to hold new particles
// Update dynamic particles
for (auto& particle : particles) {
if (particle.isStatic()) {
continue; // Skip updating static particles
}
positionX = particle.update(deltaTime, windowWidth, windowHeight, type);
if (positionX > 1.0f && positionX < 1920.0f) {
if (positionXCounts.find(positionX) != positionXCounts.end()) {
float newPositionY = 4.0f * positionXCounts[positionX];
Particle particleNew = Particle(positionX, (1070.0f - newPositionY), 0.0f, 0.0f);
particleNew.setStatic();
newParticles.push_back(particleNew);
positionXCounts[positionX] += 1;
} else {
Particle particleNew = Particle(positionX, 1070.0f, 0.0f, 0.0f);
particleNew.setStatic();
newParticles.push_back(particleNew);
positionXCounts[positionX] += 1;
}
}
}
particles.insert(particles.end(), newParticles.begin(), newParticles.end());
}
void ParticleSystem::render() {
for (const auto& particle : particles) {
particle.render();
}
}
void ParticleSystem::addParticle(const Particle& particle) {
particles.push_back(particle);
}
const std::vector<Particle>& ParticleSystem::getParticles() const {
return particles;
}
void ParticleSystem::removeParticle(const Particle& particle) {
std::cout << "pop";
particles.pop_back();
}