Repository navigation
Expand file tree
/
Copy pathParticles.cpp
More file actions
239 lines (199 loc) · 5.57 KB
/
Copy pathParticles.cpp
File metadata and controls
239 lines (199 loc) · 5.57 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
#include "Particles.h"
#include "Game.h"
#include "Player.h"
#include <iostream>
#include <tuple>
#include <utility>
// larger ast explosion (directional and speed based)
Particles::Particles(const int x, const int y, float radians, const float speed, const float offset, const int amount, const float s_r)
{
this->amount = amount;
SPEED = speed;// *s_r;
angle_modifier = radians;
float vx = sin(radians) * SPEED;
float vy = cos(radians) * SPEED;
float orig_offset = offset;
pixels.reserve(amount*3);
for (int i = 0; i < amount * 3; i++)
{
pixels.push_back(std::make_unique<Pixel>());
pixels.back()->rect = SDL_Rect{ x, y, 2, 2 };
pixels.back()->pos_x = x;
pixels.back()->pos_y = y;
float new_offset = orig_offset * ((float)rand() / (float)RAND_MAX) - (offset / 2.0);
float new_angle = angle_modifier + new_offset;
float new_speed = SPEED/4.0f + (((float)rand() / (float)RAND_MAX) * (SPEED/2.0f));
pixels.back()->vel_x = new_speed * sinf(new_angle);
pixels.back()->vel_y = new_speed * cosf(new_angle);
}
}
// Player thrust
Particles::Particles(Entity* player, const int amount, const float s_r)
{
float radians = player->angle * PI / 180.0f;
float speed;
pixels.reserve(amount*3);
int x = -sin((float)player->angle * PI / 180.0) * player->height / 4.0 + player->rect->x + player->width / 2.0;
int y = cos((float)player->angle * PI / 180.0) * player->height / 4.0 + player->rect->y + player->height / 2.0;
for (int i = 0; i < amount * 3; i++)
{
life_cycle.SetNewDelay(400.0f);
pixels.push_back(std::make_unique<Pixel>());
pixels.back()->rect = SDL_Rect{ x, y, 2, 2 };
pixels.back()->pos_x = x;
pixels.back()->pos_y = y;
// calculate an offset
float offset = (((float)rand() / (float)RAND_MAX) * (PI)) - (PI/2.0f);
float new_angle = radians + offset;
speed = 75.0f + ((float)rand() / (float)RAND_MAX) * 25.0f;
pixels.back()->vel_x = speed * -sinf(new_angle);
pixels.back()->vel_y = speed * cosf(new_angle);
}
}
// small ast explosion
Particles::Particles(const int x, const int y, const float amount, const float s_r)
{
SPEED *= s_r;
this->amount = amount;
angle_modifier /= amount;
float iteration;
pixels.reserve(amount*3);
for (int i = 0; i < amount*3; i++)
{
iteration = (float)(i + 1);
pixels.push_back(std::make_unique<Pixel>());
pixels.back()->rect = SDL_Rect{ x, y, 2, 2 };
pixels.back()->pos_x = x;
pixels.back()->pos_y = y;
pixels.back()->vel_x = ((float)rand() / (float)RAND_MAX)*(SPEED*s_r) * cosf(angle_modifier * iteration);
pixels.back()->vel_y = ((float)rand() / (float)RAND_MAX)*(SPEED*s_r) * sinf(angle_modifier * iteration);
}
}
// Player Hit
Particles::Particles(Entity* entity, const float s_r)
{
float speed;
int pixel_size = 0;
float life_time = 300.0f;
int rAngle = 0;
if (dynamic_cast<Player*>(entity) != nullptr)
{
ent_type = 'p';
pixel_size = 5;
rAngle = entity->angle;
}
else if (dynamic_cast<PowerUp*>(entity) != nullptr)
{
ent_type = 'c';
pixel_size = 3;
life_time /= 3.0;
rAngle = entity->angle;
}
else
{
std::cout << "UNKOWN ENTITY" << std::endl;
}
amount = 40;
float radians = rAngle * PI / 180.0f;
float offset = 0;
pixels.reserve(amount * 4);
for (int i = 0; i < 4; i++)
{
int x = entity->rect->x + entity->width / 2.0;
int y = entity->rect->y + entity->height / 2.0;
rAngle += i * 90.0;
rAngle = rAngle % 360;
float radians = rAngle * PI / 180.0f;
for (int j = 0; j < amount; j++)
{
life_cycle.SetNewDelay(life_time);
pixels.push_back(std::make_unique<Pixel>());
pixels.back()->rect = SDL_Rect{ x, y, pixel_size, pixel_size };
pixels.back()->pos_x = x;
pixels.back()->pos_y = y;
// calculate an offset
offset = (((float)rand() / (float)RAND_MAX) * (PI / 2.0));
float new_angle = radians +offset;
if (j < amount / 2.0)
{
speed = 400.0f * s_r;
}
else
{
speed = (400.0f - ((float)rand() / (float)RAND_MAX) * 100.0f) * s_r;
}
pixels.back()->vel_x = speed * -sinf(new_angle);
pixels.back()->vel_y = speed * cosf(new_angle);
}
}
}
void Particles::Update(float const dt)
{
delta_time = dt;
for (int i = 0; i < pixels.size(); i++)
{
pixels[i]->pos_x += pixels[i]->vel_x * dt;
pixels[i]->pos_y += pixels[i]->vel_y * dt;
pixels[i]->rect.x = pixels[i]->pos_x;
pixels[i]->rect.y = pixels[i]->pos_y;
}
if (life_cycle.DelayComplete(true))
{
is_complete = true;
}
}
void Particles::Draw() const
{
SDL_SetRenderDrawBlendMode(Game::gRenderer, SDL_BLENDMODE_BLEND);
time_elapsed -= (delta_time * 1000) / life_cycle.delay;
opacity = 255.0 * (time_elapsed);
if (opacity < 0)
{
opacity = 0;
}
if (ent_type == NULL)
{
for (int i = 0, n = pixels.size(); i < n; i++)
{
if (i < n / 3)
{
// yellow
SDL_SetRenderDrawColor(Game::gRenderer, 255, 255, 0, opacity);
}
else if (i > n / 3 && i < (n / 3) * 2)
{
// orange
SDL_SetRenderDrawColor(Game::gRenderer, 255, 165, 0, opacity);
}
else if (i > (n / 3) * 2)
{
// red
SDL_SetRenderDrawColor(Game::gRenderer, 255, 0, 0, opacity);
}
else
{
// white
SDL_SetRenderDrawColor(Game::gRenderer, 255, 255, 255, opacity);
}
SDL_RenderFillRect(Game::gRenderer, &(pixels[i]->rect));
}
}
else if (ent_type == 'p')
{
// white
for (int i = 0, n = pixels.size(); i < n; i++)
{
SDL_SetRenderDrawColor(Game::gRenderer, 255, 255, 255, opacity);
SDL_RenderFillRect(Game::gRenderer, &(pixels[i]->rect));
}
}
else
{
// green
for (int i = 0, n = pixels.size(); i < n; i++)
{
SDL_SetRenderDrawColor(Game::gRenderer, 152, 251, 152, opacity);
SDL_RenderFillRect(Game::gRenderer, &(pixels[i]->rect));
}
}
}