Zappy GUI
C++ graphic client: renderer, camera, network
Loading...
Searching...
No Matches
DeathBehavior.cpp
Go to the documentation of this file.
1#include "DeathBehavior.hpp"
2
3#include <cmath>
4#include <cstdlib>
5
6static constexpr int PARTICLE_COUNT = 14;
7
8DeathBehavior::DeathBehavior(VisualState& visual, float server_tick_rate)
9 : _visual(visual), _duration(10.0f / server_tick_rate)
10{
11}
12
14{
15 _particles.clear();
16 for (int i = 0; i < PARTICLE_COUNT; i++) {
17 float theta = (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 2.0f * PI;
18 float phi = (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * PI;
19 float speed = 0.8f + (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 1.2f;
20
21 Particle p;
22 p.pos = _visual.pos;
23 p.vel.x = speed * std::sin(phi) * std::cos(theta);
24 p.vel.y = speed * std::cos(phi) * 0.6f + 0.4f; // bias upward
25 p.vel.z = speed * std::sin(phi) * std::sin(theta);
26 p.color = WHITE;
27 p.size = 0.04f + (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 0.06f;
28 p.alpha = 1.0f;
29 p.delay = 0.0f;
30 p.active = true;
31 _particles.push_back(p);
32 }
33}
34
36{
37 if (!_spawned) {
39 _spawned = true;
40 }
41
42 _elapsed += dt;
43 float t = _elapsed / _duration;
44 if (t > 1.0f) t = 1.0f;
45
46 float smooth = t * t * (3.0f - 2.0f * t);
47 _visual.scale = 1.0f - smooth * (1.0f - 0.05f);
48
49 for (auto& p : _particles) {
50 p.pos.x += p.vel.x * dt;
51 p.pos.y += p.vel.y * dt;
52 p.pos.z += p.vel.z * dt;
53 p.vel.y -= 2.0f * dt; // gravity
54 p.alpha = 1.0f - t;
55 }
56}
57
58bool DeathBehavior::isDone() const { return _elapsed >= _duration; }
static constexpr int PARTICLE_COUNT
std::vector< Particle > _particles
bool isDone() const override
Returns true when the behavior has completed and can be removed.
DeathBehavior(VisualState &visual, float server_tick_rate)
void update(float dt) override
VisualState & _visual
Visual-only state for an entity, driven by behaviors each frame. Logical state (x,...
float alpha
Definition DrawTypes.hpp:10
Vector3 pos
Definition DrawTypes.hpp:6
Vector3 vel
Definition DrawTypes.hpp:7
bool active
Definition DrawTypes.hpp:12
float delay
Definition DrawTypes.hpp:11
Color color
Definition DrawTypes.hpp:8
float size
Definition DrawTypes.hpp:9