-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFireworks.cpp
More file actions
152 lines (114 loc) · 2.55 KB
/
Fireworks.cpp
File metadata and controls
152 lines (114 loc) · 2.55 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
#include "../Include/defGameEngine.hpp"
struct Particle
{
def::Vector2f offset;
def::Vector2f velocity;
float speed;
def::Pixel col;
};
struct Firework
{
def::Vector2f pos;
def::Vector2f explodedPos;
// Current time in milliseconds
float explosionTimer;
// Time when firework must explode
float explosionTime;
// Local timer to safe current lifetime
float lifeTimer;
// How long particles will be alive
float lifeTime;
std::vector<Particle> particles;
};
class Fireworks : public def::GameEngine
{
public:
Fireworks()
{
GetWindow()->SetTitle("Fireworks");
}
private:
std::vector<Firework> fireworks;
// Count time in milliseconds
float timer;
// When new firework will appear
float appearTime;
const float fireworkSpeed = 100.0f;
const float particleSpeed = 10.0f;
private:
void AddFirework()
{
Firework f;
f.pos.x = ((rand() + int(float(GetWindow()->GetScreenWidth() - 1) * 0.25f)) % int(float(GetWindow()->GetScreenWidth() - 1) * 0.75f));
f.pos.y = GetWindow()->GetScreenHeight() - 1;
f.explodedPos = { -1.0f, -1.0f };
f.explosionTimer = 0.0f;
f.explosionTime = float((rand() % 2) + 1);
f.lifeTimer = 0.0f;
f.lifeTime = float((rand() % 4) + 2);
int particles = (rand() + 20) % 40;
for (int i = 0; i < particles; i++)
{
Particle p;
p.offset = { 0.0f, 0.0f };
p.speed = float((rand() % 10) + 5);
p.col = def::Pixel(rand() % 256, rand() % 256, rand() % 256);
float angle = 2.0f * 3.1415926f * ((float)rand() / (float)RAND_MAX);
p.velocity = { cos(angle), sin(angle) };
f.particles.push_back(p);
}
fireworks.push_back(f);
}
public:
bool OnUserCreate() override
{
srand(time(NULL));
timer = 0.0f;
appearTime = 0.5f;
return true;
}
bool OnUserUpdate(float deltaTime) override
{
if (timer >= appearTime)
{
AddFirework();
timer = 0.0f;
}
else
timer += deltaTime;
Clear(def::BLACK);
size_t i = 0;
for (auto& f : fireworks)
{
if (f.explosionTimer >= f.explosionTime)
{
if (f.explodedPos.x < 0.0f)
f.explodedPos = f.pos;
for (auto& p : f.particles)
{
p.offset += (particleSpeed + p.speed) * p.velocity * deltaTime;
Draw(f.explodedPos + p.offset, p.col);
}
if (f.lifeTimer > f.lifeTime)
fireworks.erase(fireworks.begin() + i);
else
f.lifeTimer += deltaTime;
}
else
{
f.explosionTimer += deltaTime;
f.pos.y -= fireworkSpeed * deltaTime;
Draw(f.pos, def::WHITE);
}
i++;
}
return true;
}
};
int main()
{
Fireworks demo;
demo.Construct(256, 240, 4, 4);
demo.Run();
return 0;
}