-
Notifications
You must be signed in to change notification settings - Fork 4
/
partsys.h
127 lines (102 loc) · 2.42 KB
/
partsys.h
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
#pragma once
#include <vector>
#include "vec.h"
#include "SDL_gpu.h"
struct PartSys {
GPU_Image* texture;
struct Particle {
Particle() {}
int sprite;
vec pos;
vec vel;
float ttl;
float scale;
float rotation;
float rotation_vel;
float alpha;
inline bool Update(float dt, const PartSys& system) {
ttl -= dt;
if (ttl < 0) return true;
vel += system.acc * dt;
pos += vel * dt;
scale += system.scale_vel * dt;
if (scale < 0.f) {
if (system.scale_vel < 0.f) {
return true;
}
else {
scale = 0.0001f;
}
}
rotation += rotation_vel * dt;
alpha += system.alpha_vel * dt;
if (alpha < 0.f) {
if (system.alpha_vel < 0.f) {
return true;
} else {
alpha = 0.f;
}
}
else if (system.bounce_alpha > 0) {
if (alpha > 2 * system.bounce_alpha) {
return true;
}
} else if (alpha > 1.f) {
alpha = 1.f;
}
return false;
}
};
vec pos = vec();
//float spawn_radius = 0.f; // TODO: Implement
vec max_vel = vec();
vec min_vel = vec();
vec acc = vec();
float min_ttl = 1.f;
float max_ttl = 1.f;
float min_interval = 0.2f;
float max_interval = 0.2f;
float min_scale = 1.f;
float max_scale = 1.f;
float scale_vel = 0.f;
float min_rotation = 0.f;
float max_rotation = 0.f;
float min_rotation_vel = 0.f;
float max_rotation_vel = 0.f;
float alpha = 1.f;
float alpha_vel = 0.f;
float bounce_alpha = -1.f; //max alpha, at which it starts going back to 0
float time = 0.f;
PartSys(GPU_Image* t) {
SetTexture(t);
particles.reserve(80); //~3kb memory usage
}
void SetTexture(GPU_Image* t) { texture = t; }
void AddSprite(const GPU_Rect& rect) {
sprites.emplace_back(rect);
}
void Spawn(float dt) { SpawnWithExternalTimer(time, dt); }
void SpawnWithExternalTimer(float& timer, float dt);
void UpdateParticles(float dt); //Doesn't create new particles, use Spawn(), SpawnWithExternalTimer(), AddParticle() or AddParticles()
void Draw() const;
Particle& AddParticle();
inline void AddParticles(int n) {
while (n-- > 0) {
AddParticle();
}
}
void DrawImGUI(const char* title = "Particles");
void Clear() { particles.clear(); }
void FlipX() {
float aux = max_vel.x;
max_vel.x = -min_vel.x;
min_vel.x = -aux;
acc.x = -acc.x;
aux = max_rotation_vel;
max_rotation_vel = -min_rotation_vel;
min_rotation_vel = -aux;
}
std::vector<Particle> particles;
private:
mutable std::vector<GPU_Rect> sprites;
};