Skip to content

Commit 50b8c91

Browse files
xezonChapter5780
authored andcommitted
refactor(draw): Simplify particle effects code in W3DTankDraw, W3DTankTruckDraw, W3DTruckDraw (TheSuperHackers#2235)
1 parent 7336394 commit 50b8c91

File tree

6 files changed

+186
-257
lines changed

6 files changed

+186
-257
lines changed

Core/GameEngineDevice/Include/W3DDevice/GameClient/Module/W3DTankDraw.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@ class W3DTankDraw : public W3DModelDraw
7474

7575
protected:
7676

77-
/// debris emitters for when tank is moving
78-
ParticleSystem* m_treadDebrisLeft;
79-
ParticleSystem* m_treadDebrisRight;
77+
/// left and right debris emitters for when tank is moving
78+
ParticleSystem* m_treadDebris[2];
8079

8180
RenderObjClass *m_prevRenderObj;
8281

Core/GameEngineDevice/Include/W3DDevice/GameClient/Module/W3DTankTruckDraw.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ class W3DTankTruckDraw : public W3DModelDraw
9696
Bool m_effectsInitialized;
9797
Bool m_wasAirborne;
9898
Bool m_isPowersliding;
99+
99100
/// debris emitters for when tank is moving
100-
ParticleSystem* m_dustEffect;
101-
ParticleSystem* m_dirtEffect;
102-
ParticleSystem* m_powerslideEffect;
101+
enum { DustEffect, DirtEffect, PowerslideEffect };
102+
ParticleSystem* m_truckEffects[3];
103103

104104
Real m_frontWheelRotation;
105105
Real m_rearWheelRotation;
@@ -120,9 +120,9 @@ class W3DTankTruckDraw : public W3DModelDraw
120120
AudioEventRTS m_landingSound;
121121

122122
//Tank Data
123-
/// debris emitters for when tank is moving
124-
ParticleSystem* m_treadDebrisLeft;
125-
ParticleSystem* m_treadDebrisRight;
123+
124+
/// left and right debris emitters for when tank is moving
125+
ParticleSystem* m_treadDebris[2];
126126

127127
enum TreadType { TREAD_LEFT, TREAD_RIGHT, TREAD_MIDDLE }; //types of treads for different vehicles
128128
enum {MAX_TREADS_PER_TANK=4};

Core/GameEngineDevice/Include/W3DDevice/GameClient/Module/W3DTruckDraw.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ class W3DTruckDraw : public W3DModelDraw
9999
Bool m_effectsInitialized;
100100
Bool m_wasAirborne;
101101
Bool m_isPowersliding;
102+
102103
/// debris emitters for when tank is moving
103-
ParticleSystem* m_dustEffect;
104-
ParticleSystem* m_dirtEffect;
105-
ParticleSystem* m_powerslideEffect;
104+
enum { DustEffect, DirtEffect, PowerslideEffect };
105+
ParticleSystem* m_truckEffects[3];
106106

107107
Real m_frontWheelRotation;
108108
Real m_rearWheelRotation;

Core/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DTankDraw.cpp

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ void W3DTankDrawModuleData::buildFieldParse(MultiIniFieldParse& p)
8686
//-------------------------------------------------------------------------------------------------
8787
//-------------------------------------------------------------------------------------------------
8888
W3DTankDraw::W3DTankDraw( Thing *thing, const ModuleData* moduleData )
89-
: W3DModelDraw( thing, moduleData ),m_prevRenderObj(nullptr), m_treadDebrisLeft(nullptr), m_treadDebrisRight(nullptr)
89+
: W3DModelDraw( thing, moduleData )
90+
, m_prevRenderObj(nullptr)
9091
{
91-
m_treadDebrisLeft = nullptr;
92-
m_treadDebrisRight = nullptr;
92+
std::fill(m_treadDebris, m_treadDebris + ARRAY_SIZE(m_treadDebris), nullptr);
9393

9494
for (Int i=0; i<MAX_TREADS_PER_TANK; i++)
9595
m_treads[i].m_robj = nullptr;
@@ -107,50 +107,39 @@ W3DTankDraw::W3DTankDraw( Thing *thing, const ModuleData* moduleData )
107107
//-------------------------------------------------------------------------------------------------
108108
void W3DTankDraw::tossEmitters( void )
109109
{
110-
if (m_treadDebrisLeft)
110+
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebris); ++i)
111111
{
112-
m_treadDebrisLeft->attachToObject(nullptr);
113-
m_treadDebrisLeft->destroy();
114-
m_treadDebrisLeft = nullptr;
115-
}
116-
if (m_treadDebrisRight)
117-
{
118-
m_treadDebrisRight->attachToObject(nullptr);
119-
m_treadDebrisRight->destroy();
120-
m_treadDebrisRight = nullptr;
112+
if (m_treadDebris[i] != nullptr)
113+
{
114+
m_treadDebris[i]->attachToObject(nullptr);
115+
m_treadDebris[i]->destroy();
116+
m_treadDebris[i] = nullptr;
117+
}
121118
}
122119
}
123120

124121
//-------------------------------------------------------------------------------------------------
125122
//-------------------------------------------------------------------------------------------------
126123
void W3DTankDraw::createEmitters( void )
127124
{
128-
if (!m_treadDebrisLeft)
129-
{
130-
const ParticleSystemTemplate *sysTemplate;
131-
sysTemplate = TheParticleSystemManager->findTemplate(getW3DTankDrawModuleData()->m_treadDebrisNameLeft);
132-
if (sysTemplate)
133-
{
134-
m_treadDebrisLeft = TheParticleSystemManager->createParticleSystem( sysTemplate );
135-
m_treadDebrisLeft->attachToDrawable(getDrawable());
136-
// important: mark it as do-not-save, since we'll just re-create it when we reload.
137-
m_treadDebrisLeft->setSaveable(FALSE);
138-
// they come into being stopped.
139-
m_treadDebrisLeft->stop();
140-
}
141-
}
142-
if (!m_treadDebrisRight)
125+
const AsciiString *treadDebrisNames[2];
126+
static_assert(ARRAY_SIZE(treadDebrisNames) == ARRAY_SIZE(m_treadDebris), "Array size must match");
127+
treadDebrisNames[0] = &getW3DTankDrawModuleData()->m_treadDebrisNameLeft;
128+
treadDebrisNames[1] = &getW3DTankDrawModuleData()->m_treadDebrisNameRight;
129+
130+
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebris); ++i)
143131
{
144-
const ParticleSystemTemplate *sysTemplate;
145-
sysTemplate = TheParticleSystemManager->findTemplate(getW3DTankDrawModuleData()->m_treadDebrisNameRight);
146-
if (sysTemplate)
132+
if (m_treadDebris[i] == nullptr)
147133
{
148-
m_treadDebrisRight = TheParticleSystemManager->createParticleSystem( sysTemplate );
149-
m_treadDebrisRight->attachToDrawable(getDrawable());
150-
// important: mark it as do-not-save, since we'll just re-create it when we reload.
151-
m_treadDebrisRight->setSaveable(FALSE);
152-
// they come into being stopped.
153-
m_treadDebrisRight->stop();
134+
if (const ParticleSystemTemplate *sysTemplate = TheParticleSystemManager->findTemplate(*treadDebrisNames[i]))
135+
{
136+
m_treadDebris[i] = TheParticleSystemManager->createParticleSystem( sysTemplate );
137+
m_treadDebris[i]->attachToDrawable(getDrawable());
138+
// important: mark it as do-not-save, since we'll just re-create it when we reload.
139+
m_treadDebris[i]->setSaveable(FALSE);
140+
// they come into being stopped.
141+
m_treadDebris[i]->stop();
142+
}
154143
}
155144
}
156145
}
@@ -170,17 +159,17 @@ W3DTankDraw::~W3DTankDraw()
170159
//-------------------------------------------------------------------------------------------------
171160
//-------------------------------------------------------------------------------------------------
172161
/**
173-
174162
* Start creating debris from the tank treads
175163
*/
176164
void W3DTankDraw::startMoveDebris( void )
177165
{
178166
if (getDrawable()->isDrawableEffectivelyHidden())
179167
return;
180-
if (m_treadDebrisLeft)
181-
m_treadDebrisLeft->start();
182-
if (m_treadDebrisRight)
183-
m_treadDebrisRight->start();
168+
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebris); ++i)
169+
{
170+
if (m_treadDebris[i] != nullptr)
171+
m_treadDebris[i]->start();
172+
}
184173
}
185174

186175
//-------------------------------------------------------------------------------------------------
@@ -190,10 +179,11 @@ void W3DTankDraw::startMoveDebris( void )
190179
*/
191180
void W3DTankDraw::stopMoveDebris( void )
192181
{
193-
if (m_treadDebrisLeft)
194-
m_treadDebrisLeft->stop();
195-
if (m_treadDebrisRight)
196-
m_treadDebrisRight->stop();
182+
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebris); ++i)
183+
{
184+
if (m_treadDebris[i] != nullptr)
185+
m_treadDebris[i]->stop();
186+
}
197187
}
198188

199189
//-------------------------------------------------------------------------------------------------
@@ -359,11 +349,11 @@ void W3DTankDraw::doDrawModule(const Matrix3D* transformMtx)
359349
if (velMult.z > 1.0f)
360350
velMult.z = 1.0f;
361351

362-
m_treadDebrisLeft->setVelocityMultiplier( &velMult );
363-
m_treadDebrisRight->setVelocityMultiplier( &velMult );
364-
365-
m_treadDebrisLeft->setBurstCountMultiplier( velMult.z );
366-
m_treadDebrisRight->setBurstCountMultiplier( velMult.z );
352+
for (size_t i = 0; i < ARRAY_SIZE(m_treadDebris); ++i)
353+
{
354+
m_treadDebris[i]->setVelocityMultiplier( &velMult );
355+
m_treadDebris[i]->setBurstCountMultiplier( velMult.z );
356+
}
367357

368358
//Update movement of treads
369359
if (m_treadCount)

0 commit comments

Comments
 (0)