-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticles.cpp
More file actions
117 lines (100 loc) · 3.45 KB
/
particles.cpp
File metadata and controls
117 lines (100 loc) · 3.45 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
#include "particles.h"
/*
Rectng r;
Point vel;
double vel_mult_second; // vel
SDL_Colour colour;
*/
void Particle::init(Point spwnPnt, double size, Point initial_vel, double vel_mult_per_second, double rem_life_seconds, unsigned char red, unsigned char grn, unsigned char blu) {
r.a = spwnPnt;
r.dimensions.x = size;
r.dimensions.y = size;
vel = initial_vel;
vel_mult_second = vel_mult_per_second;
rem_life = rem_life_seconds;
init_life = rem_life_seconds;
colour = { red, grn, blu, 255 };
}
bool Particle::update(double dt) {
r.a += vel * dt;
vel *= 1 - ((1 - vel_mult_second) * dt);
rem_life -= dt;
return rem_life <= 0;
}
void Particle::render(Camera *cam) {
SDL_SetRenderDrawColor(cam->r, colour.r, colour.g, colour.b, 255 * (rem_life / init_life));
r.render(cam);
}
// ------------------------ PS -------------------------
ParticleS::ParticleS() {
spwnTimer = 0;
spwnRate = 1;
randDir = 0;
randSpeed = 0;
randLife = 0;
ps.reserve_n_spots(256);
}
void ParticleS::create(Point spwnPnt, double size, double speed, double dir, double vel_mult_per_second, double rem_life_seconds, unsigned char red, unsigned char grn, unsigned char blu) {
__spwnPnt = spwnPnt;
__spwnPnt.x -= __size / 2;
__spwnPnt.y += __size / 2;
__size = size;
__speed = speed;
__dir = dir;
__vel_mult_per_second = vel_mult_per_second;
__rem_life_seconds = rem_life_seconds;
__red = red;
__grn = grn;
__blu = blu;
}
void ParticleS::setSpawnInterval(double spawn_rate) {
spwnRate = spawn_rate;
}
void ParticleS::setRandomises(double _randDir = 0, double _randSpeed = 0, double _randLife = 0) { // TODO+ random colour
randDir = _randDir;
randSpeed = _randSpeed;
randLife = _randLife;
}
void ParticleS::moveSpawner(Point new_spwnPoint, double new_dir) {
__spwnPnt = new_spwnPoint;
// __spwnPnt.x -= __size / 2;
// __spwnPnt.y += __size / 2;
__dir = new_dir;
}
void ParticleS::update(double dt, double addMult = 1.0, Point relvel = { 0, 0 }) {
spwnTimer += dt * addMult;
while (spwnTimer >= spwnRate) {
spwnTimer -= spwnRate;
Particle tmpP;
int id = ps.push_back(tmpP);
// TODO randK je neki sussy
double randK = ((rand() % 2001) * 0.001) - 1.0; // [-1, 1]
randK += -sin(randK * PI) * .3; // more like x^3 graph; this *4 must be <.5
double tmpDir = __dir + (randDir * randK);
randK = ((rand() % 2001) * 0.001) - 1.0;
randK += -sin(randK * PI) * .4;
double tmpSpd = __speed + (randSpeed * __speed * randK);
randK = ((rand() % 2001) * 0.001) - 1.0;
randK += -sin(randK * PI) * .4;
ps.at_id(id)->init(__spwnPnt, __size, { ((tmpSpd * cos(tmpDir)) + relvel.x) * addMult, ((tmpSpd * sin(tmpDir)) + relvel.y) * addMult }, __vel_mult_per_second, __rem_life_seconds + (randLife * __rem_life_seconds * randK), __red, __grn, __blu);
// ps.at_id(id)->vel = {spd * cos(dir), spd * sin(dir)};
}
for (int i = 0; i < ps.size(); ++i) {
if (ps.at_index(i)->update(dt)) { // zelja po izbrisu
ps.remove_index(i);
--i;
}
}
}
void ParticleS::render(Camera *cam) {
/*
Rectng r;
r.a = __spwnPnt;
r.dimensions = {__size, __size};
SDL_SetRenderDrawColor(cam->r, 255, 255, 255, 255);
r.render(cam);
*/
for (int i = 0; i < ps.size(); ++i) {
ps.at_index(i)->render(cam);
}
}