-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParticleSystem.h
155 lines (139 loc) · 4.87 KB
/
ParticleSystem.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
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
#pragma once
#include "ScreenUtils.h"
#include "RC.h"
#include "Gradient.h"
#include <Core/Rand.h>
#include <Core/Math.h>
struct Particle
{
Particle() = default;
RC start_pos;
float time_stamp = 0.f;
float life_time = 2.f;
float vel_r = 0.f;
float vel_c = 0.f;
float pos_r = 0.f;
float pos_c = 0.f;
float g = -30.f;
bool dead = false;
void init(float time, const RC& pos0, float vr, float vc, float gravity_acc, float spread, float life_t)
{
time_stamp = time;
start_pos = pos0;
vel_r = vr + spread * (rnd::rand() - 0.5f);
vel_c = vc + spread * (rnd::rand() - 0.5f);
g = gravity_acc;
pos_r = static_cast<float>(pos0.r);
pos_c = static_cast<float>(pos0.c);
life_time = life_t;
}
void update(float dt)
{
pos_c += vel_c * dt;
pos_r += vel_r * dt / pix_ar;
pos_r += g * dt; // r is pointing down.
}
bool alive(float time) const
{
return time - time_stamp < life_time;
}
template<int NR, int NC>
void draw(ScreenHandler<NR, NC>& sh, const std::string& str, Color fg_color, Color bg_color, float time) const
{
if (alive(time))
sh.write_buffer(str, math::roundI(pos_r), math::roundI(pos_c), fg_color, bg_color);
}
};
struct ParticleGradientGroup
{
Gradient<Color> fg_color_gradient;
Gradient<Color> bg_color_gradient;
Gradient<std::string> string_gradient;
};
struct ParticleHandler
{
ParticleHandler(size_t N_particles)
: particle_stream(N_particles), num_particles(N_particles), num_particles_active(N_particles) {}
void update(const RC& start_pos, bool trigger,
float vel_r, float vel_c, float g,
float spread, float life_time, int particle_cluster_size,
float dt, float time)
{
int particle_cluster_idx = 0;
for (auto& particle : particle_stream)
{
if (particle.alive(time))
particle.update(dt);
else if (trigger)
{
if (!particle.dead)
particle.init(time, start_pos, vel_r, vel_c, g, spread, life_time);
if (particle_cluster_idx++ >= particle_cluster_size)
trigger = false;
}
}
}
template<int NR, int NC>
void draw(ScreenHandler<NR, NC>& sh, const std::string& str, Color fg_color, Color bg_color, float time) const
{
for (const auto& particle : particle_stream)
if (!particle.dead)
particle.draw(sh, str, fg_color, bg_color, time);
}
template<int NR, int NC>
void draw(ScreenHandler<NR, NC>& sh, const std::vector<std::string>& str,
const Gradient<Color>& fg_color, const Gradient<Color>& bg_color, float time) const
{
for (const auto& particle : particle_stream)
if (!particle.dead && particle.alive(time))
{
auto t = (time - particle.time_stamp)/particle.life_time;
int str_idx = static_cast<int>(std::round(t*str.size())) - 1;
str_idx = math::clamp(str_idx, 0, static_cast<int>(str.size()) - 1);
particle.draw(sh, str[str_idx], fg_color(t), bg_color(t), time);
}
}
template<int NR, int NC>
void draw(ScreenHandler<NR, NC>& sh, const std::vector<std::string>& str,
const std::vector<std::pair<float, std::pair<Gradient<Color>, Gradient<Color>>>>& color_fg_bg_vec,
float time) const
{
for (const auto& particle : particle_stream)
if (!particle.dead && particle.alive(time))
{
auto t = (time - particle.time_stamp)/particle.life_time;
int str_idx = static_cast<int>(std::round(t*str.size())) - 1;
str_idx = math::clamp(str_idx, 0, static_cast<int>(str.size()) - 1);
const auto& col_fg_bg = rnd::rand_select(color_fg_bg_vec);
const Gradient<Color>& fg_color = col_fg_bg.first;
const Gradient<Color>& bg_color = col_fg_bg.second;
particle.draw(sh, str[str_idx], fg_color(t), bg_color(t), time);
}
}
template<int NR, int NC>
void draw(ScreenHandler<NR, NC>& sh,
const std::vector<std::pair<float, ParticleGradientGroup>>& gradient_groups,
float time) const
{
for (const auto& particle : particle_stream)
if (!particle.dead && particle.alive(time))
{
auto t = (time - particle.time_stamp)/particle.life_time;
const auto& grads = rnd::rand_select(gradient_groups);
const Gradient<Color>& fg_grad = grads.fg_color_gradient;
const Gradient<Color>& bg_grad = grads.bg_color_gradient;
const Gradient<std::string>& str_grad = grads.string_gradient;
particle.draw(sh, str_grad(t), fg_grad(t), bg_grad(t), time);
}
}
void set_num_active_particles(float amount_ratio_active)
{
num_particles_active = static_cast<int>(std::round(static_cast<float>(num_particles) * amount_ratio_active));
for (size_t p_idx = num_particles_active; p_idx < num_particles; ++p_idx)
particle_stream[p_idx].dead = true;
}
std::vector<Particle> particle_stream;
const size_t num_particles = 0;
private:
size_t num_particles_active = 0;
};