masses/source/Effect.hpp
2024-12-21 14:08:37 +01:00

87 lines
2.0 KiB
C++

#ifndef EFFECT_HPP
#define EFFECT_HPP
class DeathEffect {
public:
class Toss {
public:
constexpr static double gravity = 98.0d * 4;
double last_update;
Vector2 starting_position;
Vector2 position;
Vector2 velocity;
Toss(Vector2 start_position) : starting_position(start_position) {
position = starting_position;
velocity.y = (rand() % 140) + 140;
velocity.x = (rand() % (75*2)) - 75;
last_update = GetTime();
}
int update(double t) {
if (position.y > starting_position.y) {
return 1;
}
const double dt = t - last_update;
Vector2 delta;
delta.x = velocity.x * dt;
delta.y = velocity.y * dt;
position.x += delta.x;
position.y -= delta.y;
velocity.y -= gravity * dt;
last_update = t;
return 0;
}
void display(void) {
DrawRectangle(
position.x - (4/2),
position.y - (4/2),
4,
4,
RED
);
}
};
Vector2 source;
std::vector<Toss> tosses;
bool is_done = false;
DeathEffect(Vector2 source_position) : source(source_position) {
int n = (rand() % 50) + 20;
tosses.reserve(n);
for (int i = 0; i < n; i++) {
tosses.emplace_back(
(Vector2) {
.x = source.x + ((rand() % 6) - 3),
.y = source.y + ((rand() % 10) - 5),
}
);
}
}
void update(void) {
bool are_tosses_done = true;
for (auto &t : tosses) {
are_tosses_done &= t.update(GetTime());
}
is_done = are_tosses_done;
}
void display(void) {
for (auto &t : tosses) {
t.display();
}
}
};
#endif