-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParticleEngine.cpp
169 lines (147 loc) · 3.37 KB
/
ParticleEngine.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
#include "ParticleEngine.h"
ParticleEngine::ParticleEngine(sf::Vector2f pLocation, int pCount, sf::Color pColor) :
mEmitterLocation(pLocation),
mParticles(pCount),
mColor(pColor)
{
srand ((unsigned int) time(NULL));
Refill(pCount);
}
ParticleEngine::~ParticleEngine(void)
{
}
void ParticleEngine::Refill(int pCount)
{
mParticles.resize(pCount);
for(int i = 0; i < mParticles.size(); i++)
{
mParticles[i] = GenerateNewParticle();
}
}
void ParticleEngine::Clear()
{
mParticles.clear();
}
Particle ParticleEngine::GenerateNewParticle(float xMin, float xMax,
float yMin, float yMax,
float TTLmin, float TTLmax,
int sizeMin, int sizeMax, float pAngle,
float pAngularvelocity, DrawType pDrawType)
{
sf::Vector2f position = mEmitterLocation;
sf::Vector2f velocity(RandomFloat(xMin,xMax),RandomFloat(yMin,yMax));
float angle = pAngle;
float angularVelocity;
if (pAngularvelocity = -99) angularVelocity = RandomFloat(-1,1);
else angularVelocity = pAngularvelocity;
sf::Color tmpColor;
if(mColor == sf::Color::Black)
{
tmpColor = sf::Color(RandomInt(0,255),
RandomInt(0,255),
RandomInt(0,255));
}
else tmpColor = mColor;
int size = RandomInt(sizeMin,sizeMax);
int ttl = RandomInt(TTLmin,TTLmax);
return Particle(position, velocity, angle, angularVelocity, ttl, size, tmpColor, pDrawType);
}
void ParticleEngine::Update(float gravity, bool kill)
{
std::vector<Particle>::iterator it = mParticles.begin();
for (int i = 0; i < mParticles.size(); i++)
{
mParticles[i].Update();
mParticles[i].changeVelocity(sf::Vector2f(0,gravity));
if (mParticles[i].getTTL() <= 0)
{
if (kill)
{
mParticles.erase(mParticles.begin() + i);
}
else
{
mParticles[i] = GenerateNewParticle();
}
}
}
}
void ParticleEngine::Draw(sf::RenderWindow* window, float pInterpolation)
{
for (int i = 0; i < mParticles.size(); i++)
{
mParticles[i].Draw(window, pInterpolation);
}
}
void ParticleEngine::setColor(sf::Color pColor)
{
mColor = pColor;
}
sf::Color ParticleEngine::getColor()
{
return mColor;
}
void ParticleEngine::setEmitterLocation(sf::Vector2f pPosition)
{
mEmitterLocation = pPosition;
}
sf::Vector2f ParticleEngine::getEmitterLocation()
{
return mEmitterLocation;
}
void ParticleEngine::setPosition(sf::Vector2f pPosition)
{
for(int i = 0; i < mParticles.size(); i++)
{
mParticles[i].setPosition(pPosition);
}
}
void ParticleEngine::setVelocity(sf::Vector2f pVelocity)
{
for(int i = 0; i < mParticles.size(); i++)
{
mParticles[i].setVelocity(pVelocity);
}
}
void ParticleEngine::changeVelocity(sf::Vector2f pVelocity)
{
for(int i = 0; i < mParticles.size(); i++)
{
mParticles[i].changeVelocity(pVelocity);
}
}
void ParticleEngine::setAngle( float pAngle)
{
for(int i = 0; i < mParticles.size(); i++)
{
mParticles[i].setAngle(pAngle);
}
}
void ParticleEngine::setAnglularVelocity(float pAngVel)
{
for(int i = 0; i < mParticles.size(); i++)
{
mParticles[i].setAnglularVelocity(pAngVel);
}
}
void ParticleEngine::setSize(float pSize)
{
for(int i = 0; i < mParticles.size(); i++)
{
mParticles[i].setSize(pSize);
}
}
void ParticleEngine::setTTL(int pTTL)
{
for(int i = 0; i < mParticles.size(); i++)
{
mParticles[i].setTTL(pTTL);
}
}
void ParticleEngine::setDrawType(DrawType pDrawType)
{
for(int i = 0; i < mParticles.size(); i++)
{
mParticles[i].setDrawType(pDrawType);
}
}