Zappy GUI
C++ graphic client: renderer, camera, network
Loading...
Searching...
No Matches
LevelUpBehavior.cpp
Go to the documentation of this file.
1#include "LevelUpBehavior.hpp"
2
3#include <cmath>
4#include <cstdlib>
5
6static constexpr int PARTICLE_COUNT = 20;
7
8LevelUpBehavior::LevelUpBehavior(VisualState& visual, float server_tick_rate)
9 : _visual(visual), _duration(20.0f / server_tick_rate)
10{
11}
12
14{
15 static const Color COLORS[] = {
16 {255, 220, 0, 255}, // yellow
17 {255, 180, 0, 255}, // amber
18 {255, 130, 0, 255}, // orange
19 {255, 80, 0, 255}, // deep orange
20 };
21
22 _particles.clear();
23 Vector3 base = {_visual.pos.x, _visual.pos.y + 0.3f, _visual.pos.z};
24
25 for (int i = 0; i < PARTICLE_COUNT; i++) {
26 float angle = (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 2.0f * PI;
27 float spread = (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 0.25f;
28 float speed = 2.5f + (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 2.5f;
29
30 Particle p;
31 p.pos = base;
32 p.vel.x = std::cos(angle) * spread * speed;
33 p.vel.y = speed;
34 p.vel.z = std::sin(angle) * spread * speed;
35 p.color = COLORS[rand() % 4];
36 p.size = 0.04f + (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 0.05f;
37 p.alpha = 0.0f;
38 p.delay = (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * (_duration * 0.6f);
39 p.active = false;
40 _particles.push_back(p);
41 }
42}
43
45{
46 if (!_spawned) {
48 _spawned = true;
49 }
50
51 _elapsed += dt;
52
53 for (auto& p : _particles) {
54 if (!p.active) {
55 if (_elapsed >= p.delay) {
56 p.active = true;
57 p.pos = {_visual.pos.x, _visual.pos.y + 0.3f, _visual.pos.z};
58 p.alpha = 1.0f;
59 }
60 continue;
61 }
62 float remaining = _duration - _elapsed;
63 p.alpha = remaining > 0.0f ? std::min(1.0f, remaining / (_duration * 0.4f)) : 0.0f;
64 p.pos.x += p.vel.x * dt;
65 p.pos.y += p.vel.y * dt;
66 p.pos.z += p.vel.z * dt;
67 p.vel.y -= 3.5f * dt; // gravity pulls back down
68 }
69}
70
72{
73 if (_elapsed >= _duration) {
74 _particles.clear();
75 return true;
76 }
77 return false;
78}
static constexpr int PARTICLE_COUNT
static constexpr int PARTICLE_COUNT
std::vector< Particle > _particles
LevelUpBehavior(VisualState &visual, float server_tick_rate)
VisualState & _visual
bool isDone() const override
Returns true when the behavior has completed and can be removed.
void update(float dt) override
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