-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSFXSmokeTrail.cpp
356 lines (256 loc) · 8.57 KB
/
SFXSmokeTrail.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// SFXSmokeTrail.cpp
//
// Smoke Trail SFX
#include "PreComp.h"
#define PARTICLE_EFFECT_TAG CONSTLIT("ParticleEffect")
#define LIFETIME_ATTRIB CONSTLIT("lifetime")
#define SPREAD_ATTRIB CONSTLIT("spread")
#define EMIT_LIFETIME_ATTRIB CONSTLIT("emitDuration")
#define PARTICLE_LIFETIME_ATTRIB CONSTLIT("particleLifetime")
#define NEW_PARTICLES_ATTRIB CONSTLIT("emitRate")
#define EMIT_SPEED_ATTRIB CONSTLIT("emitSpeed")
#define ROTATION_ATTRIB CONSTLIT("rotation")
const int DEFAULT_PARTICLE_LIFETIME = 10;
const int DEFAULT_NEW_PARTICLES_PER_TICK = 20;
const int DEFAULT_MAX_WIDTH = 8;
const int DEFAULT_EMIT_SPEED = 20;
class CSmokeTrailPainter : public IEffectPainter
{
public:
CSmokeTrailPainter (CCreatePainterCtx &Ctx, CSmokeTrailEffectCreator *pCreator);
// IEffectPainter virtuals
virtual ~CSmokeTrailPainter (void) override;
virtual CEffectCreator *GetCreator (void) override { return m_pCreator; }
virtual int GetFadeLifetime (bool bHit) const override { return m_pCreator->GetParticleLifetimeMax(); }
virtual void GetRect (RECT *retRect) const override;
virtual void OnBeginFade (void) override { m_iEmitLifetime = 0; }
virtual void OnUpdate (SEffectUpdateCtx &Ctx) override;
virtual void Paint (CG32bitImage &Dest, int x, int y, SViewportPaintCtx &Ctx) override;
virtual void PaintFade (CG32bitImage &Dest, int x, int y, SViewportPaintCtx &Ctx) override { Paint(Dest, x, y, Ctx); }
virtual void PaintHit (CG32bitImage &Dest, int x, int y, const CVector &vHitPos, SViewportPaintCtx &Ctx) override { Paint(Dest, x, y, Ctx); }
protected:
virtual void OnReadFromStream (SLoadCtx &Ctx) override;
virtual void OnWriteToStream (IWriteStream *pStream) override;
private:
void CreateNewParticles (int iCount, int iDirection);
CSmokeTrailEffectCreator *m_pCreator;
CParticleArray m_Particles;
int m_iLastDirection;
int m_iEmitLifetime;
int m_iTick;
IEffectPainter *m_pParticlePainter;
};
// CSmokeTrailEffectCreator object
CSmokeTrailEffectCreator::~CSmokeTrailEffectCreator (void)
// CSmokeTrailEffectCreator destructor
{
if (m_pParticleEffect)
delete m_pParticleEffect;
}
IEffectPainter *CSmokeTrailEffectCreator::OnCreatePainter (CCreatePainterCtx &Ctx)
// CreatePainter
//
// Creates a new painter
{
return new CSmokeTrailPainter(Ctx, this);
}
Metric CSmokeTrailEffectCreator::GetEmitSpeed (void) const
// GetEmitSpeed
//
// Returns the emit speed of a particle
{
return (m_InitSpeed.Roll() * LIGHT_SPEED / 100.0f);
}
ALERROR CSmokeTrailEffectCreator::OnEffectCreateFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc, const CString &sUNID)
// OnEffectCreateFromXML
//
// Initializes from XML
{
ALERROR error;
// In previous versions there was a bug in emitSpeed. It was interpreted
// as half its actual value. If this is loading from a previous version,
// we divide by two.
bool bEmitSpeedFix = (Ctx.GetAPIVersion() < 3);
// Load some stuff
m_iEmitLifetime = pDesc->GetAttributeIntegerBounded(EMIT_LIFETIME_ATTRIB, 0, -1, 100);
m_iLifetime = pDesc->GetAttributeIntegerBounded(LIFETIME_ATTRIB, 0, -1, -1);
if (!pDesc->FindAttributeInteger(ROTATION_ATTRIB, &m_iRotation))
m_iRotation = 180;
if (error = m_Spread.LoadFromXML(pDesc->GetAttribute(SPREAD_ATTRIB)))
return error;
CString sRange;
if (pDesc->FindAttribute(PARTICLE_LIFETIME_ATTRIB, &sRange))
{
if (error = m_ParticleLifetime.LoadFromXML(sRange))
return error;
}
else
m_ParticleLifetime = DiceRange(0, 0, DEFAULT_PARTICLE_LIFETIME);
if (pDesc->FindAttribute(NEW_PARTICLES_ATTRIB, &sRange))
{
if (error = m_NewParticles.LoadFromXML(sRange))
return error;
}
else
m_NewParticles = DiceRange(0, 0, DEFAULT_NEW_PARTICLES_PER_TICK);
if (pDesc->FindAttribute(EMIT_SPEED_ATTRIB, &sRange))
{
if (error = m_InitSpeed.LoadFromXML(sRange))
return error;
if (bEmitSpeedFix)
m_InitSpeed.Scale(0.5);
}
else
m_InitSpeed = DiceRange(0, 0, DEFAULT_EMIT_SPEED);
// Load the effect to use for particles
CXMLElement *pEffect = pDesc->GetContentElementByTag(PARTICLE_EFFECT_TAG);
if (pEffect)
{
if (error = CEffectCreator::CreateFromXML(Ctx, pEffect, NULL_STR, &m_pParticleEffect))
return error;
}
else
m_pParticleEffect = NULL;
return NOERROR;
}
ALERROR CSmokeTrailEffectCreator::OnEffectBindDesign (SDesignLoadCtx &Ctx)
// OnEffectBindDesign
//
// Resolve loading
{
ALERROR error;
if (m_pParticleEffect)
{
if (error = m_pParticleEffect->BindDesign(Ctx))
return error;
}
return NOERROR;
}
// CSmokeTrailPainter object
CSmokeTrailPainter::CSmokeTrailPainter (CCreatePainterCtx &Ctx, CSmokeTrailEffectCreator *pCreator) :
m_pCreator(pCreator),
m_iLastDirection(-1),
m_iTick(0)
// CSmokeTrailPainter constructor
{
int iMaxParticleCount = m_pCreator->GetParticleLifetimeMax() * m_pCreator->GetNewParticleMax();
m_Particles.Init(iMaxParticleCount);
int iTotalLifetime = m_pCreator->GetLifetime();
m_iEmitLifetime = (iTotalLifetime == -1 ? -1 : m_pCreator->GetEmitLifetime() * iTotalLifetime / 100);
CEffectCreator *pEffect = pCreator->GetParticleEffect();
if (pEffect)
m_pParticlePainter = pEffect->CreatePainter(Ctx);
else
m_pParticlePainter = NULL;
}
CSmokeTrailPainter::~CSmokeTrailPainter (void)
// CSmokeTrailPainter destructor
{
if (m_pParticlePainter)
m_pParticlePainter->Delete();
}
void CSmokeTrailPainter::CreateNewParticles (int iCount, int iDirection)
// CreateNewParticles
//
// Create the new particles for a tick
{
int i;
int iAngleRandomness = m_pCreator->GetSpread() / 7;
for (i = 0; i < iCount; i++)
{
// Generate a random velocity for the particle in a cone around the direction
Metric rInitialSpeed = m_pCreator->GetEmitSpeed();
CVector vVel = PolarToVector(
(iDirection + 360 + mathRandom(-iAngleRandomness, iAngleRandomness)),
rInitialSpeed);
// Add a random little kick
vVel = vVel + PolarToVector(mathRandom(0, 359), rInitialSpeed * m_pCreator->GetSpread() / 100.0);
// Position
CVector vPos = vVel * (mathRandom(0, 100) / 100.0);
// Lifetime
int iLifeLeft = m_pCreator->GetParticleLifetime();
// Add the particle
m_Particles.AddParticle(vPos, vVel, iLifeLeft);
}
}
void CSmokeTrailPainter::GetRect (RECT *retRect) const
// GetRect
//
// Returns the RECT of the effect centered on 0,0
{
*retRect = m_Particles.GetBounds();
}
void CSmokeTrailPainter::OnReadFromStream (SLoadCtx &Ctx)
// OnReadFromStream
//
// Load from stream
{
m_Particles.ReadFromStream(Ctx);
Ctx.pStream->Read((char *)&m_iLastDirection, sizeof(DWORD));
Ctx.pStream->Read((char *)&m_iEmitLifetime, sizeof(DWORD));
Ctx.pStream->Read((char *)&m_iTick, sizeof(DWORD));
if (m_pParticlePainter)
m_pParticlePainter->Delete();
m_pParticlePainter = CEffectCreator::CreatePainterFromStreamAndCreator(Ctx, m_pCreator->GetParticleEffect());
}
void CSmokeTrailPainter::OnUpdate (SEffectUpdateCtx &Ctx)
// OnUpdate
//
// Update the painter
{
if (m_iLastDirection != -1)
{
// Update particle motion
m_Particles.UpdateMotionLinear();
// Create new particles (we create the particles after we update
// the motion because we start out with some particles)
if (m_iEmitLifetime == -1 || m_iTick < m_iEmitLifetime)
{
CreateNewParticles(m_pCreator->GetNewParticleCount(),
m_iLastDirection);
}
// Update
m_iTick++;
}
}
void CSmokeTrailPainter::OnWriteToStream (IWriteStream *pStream)
// OnWriteToStream
//
// Write to stream
{
m_Particles.WriteToStream(pStream);
pStream->Write((char *)&m_iLastDirection, sizeof(DWORD));
pStream->Write((char *)&m_iEmitLifetime, sizeof(DWORD));
pStream->Write((char *)&m_iTick, sizeof(DWORD));
CEffectCreator::WritePainterToStream(pStream, m_pParticlePainter);
}
void CSmokeTrailPainter::Paint (CG32bitImage &Dest, int x, int y, SViewportPaintCtx &Ctx)
// Paint
//
// Paint
{
int iParticleLifetime = m_pCreator->GetParticleLifetimeMax();
// Particles move the opposite direction from the shot
int iTrailDirection = (Ctx.iRotation + m_pCreator->GetRotation()) % 360;
// If we haven't created any particles yet, do it now
if (m_iLastDirection == -1)
{
CreateNewParticles(m_pCreator->GetNewParticleCount(), iTrailDirection);
}
// Paint with the painter
if (m_pParticlePainter)
{
// If we can get a paint descriptor, use that because it is faster
SParticlePaintDesc Desc;
if (m_pParticlePainter->GetParticlePaintDesc(&Desc))
{
Desc.iMaxLifetime = iParticleLifetime;
m_Particles.Paint(Dest, x, y, Ctx, Desc);
}
// Otherwise, we use the painter for each particle
else
m_Particles.Paint(Dest, x, y, Ctx, m_pParticlePainter);
}
// Update
m_iLastDirection = iTrailDirection;
}