-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParticleEmitter.cpp
More file actions
258 lines (234 loc) · 6 KB
/
ParticleEmitter.cpp
File metadata and controls
258 lines (234 loc) · 6 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
Greyout - a colourful platformer about love
Greyout is Copyright (c)2011-2014 Janek Schäfer
This file is part of Greyout.
Greyout is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Greyout is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Please direct any feedback, questions or comments to
Janek Schäfer (foxblock), foxblock_at_gmail_dot_com
*/
#include "ParticleEmitter.h"
#include "NumberUtility.h"
#include "Random.h"
#include "Level.h"
#include "MyGame.h"
ParticleEmitter::ParticleEmitter( Level *newParent ) :
BaseUnit(newParent),
particleTimer(0),
emitDir(0,0),
angleScatter(0),
emitPower(0,0),
particleLifetime(0,0),
nextParticleTime(0,0),
size(16,16),
centred(true),
enabled(true)
{
flags.addFlag(ufNoMapCollision);
flags.addFlag(ufNoGravity);
flags.addFlag(ufInvincible);
#ifndef _DEBUG
flags.addFlag(ufNoRender);
#endif // _DEBUG
unitCollisionMode = 0;
stringToProp["direction"] = epDirection;
stringToProp["power"] = epPower;
stringToProp["lifetime"] = epLifetime;
stringToProp["delay"] = epDelay;
stringToProp["directionscatter"] = epDirectionScatter;
stringToProp["powerscatter"] = epPowerScatter;
stringToProp["lifetimescatter"] = epLifetimeScatter;
stringToProp["delayscatter"] = epDelayScatter;
stringToProp["enabled"] = epEnabled;
stringToProp["multiplier"] = epMultiplier;
stringToProp["centred"] = epCentred;
Random::randSeed();
}
ParticleEmitter::~ParticleEmitter()
{
//
}
///--- PUBLIC ------------------------------------------------------------------
void ParticleEmitter::reset()
{
enabled = true;
BaseUnit::reset();
}
void ParticleEmitter::update()
{
BaseUnit::update();
if (enabled)
{
if (particleTimer == 0)
{
int multi = multiplier.empty() ? 1 : multiplier[ENGINE->settings->getParticleDensity()];
for (int I = 0; I < multi; ++I)
{
Vector2df tempDir = emitDir;
Vector2df pos = position;
if (angleScatter != 0)
{
float tempAngle = Random::nextFloat(-angleScatter, angleScatter);
tempDir.x = emitDir.x * cos(tempAngle) - emitDir.y * sin(tempAngle);
tempDir.y = emitDir.x * sin(tempAngle) + emitDir.y * cos(tempAngle);
}
tempDir *= Random::nextFloat(emitPower.x,emitPower.y);
if (!centred)
{
pos.x = Random::nextFloat(pos.x, pos.x + size.x);
pos.y = Random::nextFloat(pos.y, pos.y + size.y);
}
parent->addParticle(this,col,pos,tempDir,Random::nextInt(particleLifetime.x,particleLifetime.y));
}
particleTimer = max(Random::nextInt(nextParticleTime.x,nextParticleTime.y),1);
}
--particleTimer;
}
}
void ParticleEmitter::render(SDL_Surface* surf)
{
#ifdef _DEBUG
SDL_Rect temp;
if (centred)
{
temp.x = position.x;
temp.y = position.y - size.y / 2.0f;
temp.w = 1;
temp.h = size.y;
SDL_FillRect(surf,&temp,col.getSDL_Uint32Colour(surf));
temp.x = position.x - size.x / 2.0f;
temp.y = position.y;
temp.w = size.x;
temp.h = 1;
SDL_FillRect(surf,&temp,col.getSDL_Uint32Colour(surf));
}
else
{
temp.x = position.x;
temp.y = position.y;
temp.w = size.x;
temp.h = 1;
SDL_FillRect(surf,&temp,col.getSDL_Uint32Colour(surf));
temp.y += size.y;
SDL_FillRect(surf,&temp,col.getSDL_Uint32Colour(surf));
temp.w = 1;
temp.h = size.y;
temp.y -= size.y;
SDL_FillRect(surf,&temp,col.getSDL_Uint32Colour(surf));
temp.x += size.x;
SDL_FillRect(surf,&temp,col.getSDL_Uint32Colour(surf));
}
#else
// Don't render anything
#endif
}
bool ParticleEmitter::processParameter(const PARAMETER_TYPE& value)
{
bool parsed = true;
switch (stringToProp[value.first])
{
case BaseUnit::upSize:
{
size = StringUtility::stringToVec<Vector2di>(value.second);
break;
}
case epEnabled:
{
enabled = StringUtility::stringToBool(value.second);
break;
}
case epDirection:
{
emitDir = StringUtility::stringToVec<Vector2df>(value.second);
emitDir.normalise();
break;
}
case epPower:
{
float temp = StringUtility::stringToFloat(value.second);
emitPower.x = temp;
emitPower.y = temp;
break;
}
case epLifetime:
{
int temp = 0;
pLoadTime( value.second, temp );
particleLifetime.x = temp;
particleLifetime.y = temp;
break;
}
case epDelay:
{
int temp = 0;
pLoadTime( value.second, temp );
nextParticleTime.x = temp;
nextParticleTime.y = temp;
break;
}
case epDirectionScatter:
{
angleScatter = NumberUtility::degToRad(StringUtility::stringToFloat(value.second));
break;
}
case epPowerScatter:
{
float temp = StringUtility::stringToFloat(value.second);
emitPower.x -= temp;
emitPower.y += temp;
break;
}
case epLifetimeScatter:
{
int temp = 0;
pLoadTime( value.second, temp );
particleLifetime.x -= temp;
particleLifetime.y += temp;
break;
}
case epDelayScatter:
{
int temp = 0;
pLoadTime( value.second, temp );
nextParticleTime.x -= temp;
nextParticleTime.y += temp;
break;
}
case epMultiplier:
{
multiplier.clear();
vector<string> tokens;
StringUtility::tokenize( value.second, tokens, DELIMIT_STRING );
for (vector<string>::const_iterator I = tokens.begin(); I != tokens.end(); ++I)
{
multiplier.push_back(StringUtility::stringToInt(*I));
}
int temp = multiplier.size();
for (int I = temp; I < Settings::pdEOL; ++I)
{
multiplier.push_back(multiplier[temp-1]);
}
break;
}
case epCentred:
{
centred = StringUtility::stringToBool(value.second);
break;
}
default:
parsed = false;
}
if (not parsed)
return BaseUnit::processParameter(value);
return parsed;
}
///--- PROTECTED ---------------------------------------------------------------
///--- PRIVATE -----------------------------------------------------------------