forked from raydelto/GeometryWars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParticleManager.cpp
98 lines (78 loc) · 2.72 KB
/
ParticleManager.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
#include "Utility/package.hpp"
#include "package.h"
//---------------------------------------------------------------------------------
// Written by Terence J. Grant - tjgrant [at] tatewake [dot] com
// Find the full tutorial at: http://gamedev.tutsplus.com/series/
//----------------------------------------------------------------------------------
void ParticleManager::Swap(typename ParticleManager::CircularParticleArray& list, size_t index1, size_t index2) const
{
Particle temp = list[index1];
list[index1] = list[index2];
list[index2] = temp;
}
ParticleManager::ParticleManager(int capacity)
: mParticleList(capacity)
{
}
void ParticleManager::update()
{
size_t removalCount = 0;
for (size_t i = 0; i < mParticleList.getCount(); i++)
{
Particle& ref = mParticleList[i];
ref.mState.updateParticle(ref);
ref.mPercentLife -= 1.0f / ref.mDuration;
Swap(mParticleList, i - removalCount, i);
if (ref.mPercentLife < 0)
{
removalCount++;
}
}
mParticleList.setCount(mParticleList.getCount() - removalCount);
}
void ParticleManager::draw(tSpriteBatch* spriteBatch)
{
for (size_t i = 0; i < mParticleList.getCount(); i++)
{
Particle particle = mParticleList[(size_t)i];
tPoint2f origin = particle.mTexture->getSurfaceSize() / 2;
spriteBatch->draw(2, particle.mTexture, tPoint2f((int)particle.mPosition.x, (int)particle.mPosition.y), tOptional<tRectf>(),
particle.mColor,
particle.mOrientation, origin, particle.mScale);
}
}
void ParticleManager::createParticle(tTexture* texture, const tVector2f& position, const tColor4f& tint, float duration, float scale, const ParticleState& state, float theta)
{
createParticle(texture, position, tint, duration, tVector2f(scale), state, theta);
}
void ParticleManager::createParticle(tTexture* texture, const tVector2f& position, const tColor4f& tint, float duration, const tVector2f& scale, const ParticleState& state, float theta)
{
size_t index;
if (mParticleList.getCount() == mParticleList.getCapacity())
{
index = 0;
mParticleList.setStart(mParticleList.getStart() + 1);
}
else
{
index = mParticleList.getCount();
mParticleList.setCount(mParticleList.getCount() + 1);
}
Particle& ref = mParticleList[index];
ref.mTexture = texture;
ref.mPosition = position;
ref.mColor = tint;
ref.mDuration = duration;
ref.mPercentLife = 1.0f;
ref.mScale = scale;
ref.mOrientation = theta;
ref.mState = state;
}
void ParticleManager::clear()
{
mParticleList.setCount(0);
}
size_t ParticleManager::getParticleCount()
{
return mParticleList.getCount();
}