-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParticleEmitter.cpp
146 lines (126 loc) · 2.46 KB
/
ParticleEmitter.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "ParticleEmitter.h"
int ParticleEmitter::inst_num = 0;
ParticleEmitter::ParticleEmitter()
{
//inst_num = Emitter::inst_num;
mInstNum = inst_num;
inst_num += 1;
mX = 0;
mY = 0;
mStartTime = SDL_GetTicks();
//mParticles = new Particle[MAX_PARTICLES];
mParticles = new Particle[MAX_PARTICLES];
//mParticles = nullptr;
mActive = false;
}
ParticleEmitter::~ParticleEmitter()
{
mX = 0;
mY = 0;
mStartTime = 0;
delete[] mParticles;
mParticles = nullptr;
mActive = false;
}
void ParticleEmitter::reset()
{
printf("reset particle emitter %d at pos x %d y %d\n", mInstNum, mX, mY);
for (int i = 0; i < MAX_PARTICLES; i++) {
randomizeParticle(i);
}
mActive = true;
mStartTime = SDL_GetTicks();
}
void ParticleEmitter::setPos(int x, int y)
{
mX = x;
mY = y;
}
Particle* ParticleEmitter::getParticles()
{
return mParticles;
}
Uint32 ParticleEmitter::getStartTime()
{
return mStartTime;
}
void ParticleEmitter::stop()
{
mActive = false;
//delete[] mParticles;
//mParticles = nullptr;
}
//void ParticleEmitter::pause()
//{
//mActive = false;
//}
bool ParticleEmitter::isActive()
{
return mActive;
}
void ParticleEmitter::tick()
{
bool particlesDone;
if (isActive()) {
//if (SDL_GetTicks() - getStartTime() > PARTICLE_LIFE_TIME) {
//stop();
//}
//particles = gParticleEmitters[i].getParticles();
particlesDone = true;
for (int p = 0; p < MAX_PARTICLES; p++) {
if (SDL_GetTicks() - mParticles[p].getStartTime() > PARTICLE_LIFE_TIME) {
//randomizeParticle(p);
}
if (SDL_GetTicks() - mParticles[p].getStartTime() < mParticles[p].getDelay()) {
//continue;
}
if (mParticles[p].getAlpha() == 0) {
randomizeParticle(p);
}
mParticles[p].move();
if (mParticles[p].getForce() > 1) {
particlesDone = false;
}
}
if (particlesDone) {
stop();
}
}
}
void ParticleEmitter::randomizeParticle(int i)
{
SDL_Rect r;
SDL_Color c;
r.x = mX;
r.y = mY;
r.w = (rand() % MAX_PARTICLE_SIZE) + 1;
r.h = r.w;
c.r = rand() % 0xFF;
c.g = rand() % 0xFF;
c.b = rand() % 0xFF;
c.a = 0xFF;
mParticles[i].setRect(r);
mParticles[i].setImpulse(rand() % 360, (rand() % PARTICLE_MAX_SPEED) + 1);
mParticles[i].setColor(c);
mParticles[i].resetTime();
}
int ParticleEmitter::getXPos()
{
return mX;
}
int ParticleEmitter::getYPos()
{
return mY;
}
void ParticleEmitter::setActive(bool b)
{
mActive = b;
}
void ParticleEmitter::resetStartTime()
{
mStartTime = SDL_GetTicks();
}
int ParticleEmitter::getInstNum()
{
return mInstNum;
}