Zappy GUI
C++ graphic client: renderer, camera, network
Loading...
Searching...
No Matches
ForkBehavior.cpp
Go to the documentation of this file.
1#include "ForkBehavior.hpp"
2
3#include <cmath>
4#include <cstdlib>
5
6static constexpr int PARTICLE_COUNT = 10;
7
8ForkBehavior::ForkBehavior(VisualState& visual, float server_tick_rate)
9 : _visual(visual), _duration(10.0f / server_tick_rate)
10{
11 _visual.scale = 0.05f;
12}
13
15{
16 for (int i = 0; i < PARTICLE_COUNT; i++) {
17 float angle = (static_cast<float>(i) / PARTICLE_COUNT) * 2.0f * PI +
18 (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 0.4f;
19 float speed = 0.9f + (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 0.85f;
20
21 Particle p;
22 p.pos = {_visual.pos.x, _visual.pos.y + 0.1f, _visual.pos.z};
23 p.vel.x = std::cos(angle) * speed;
24 p.vel.y = 0.6f + (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 0.5f;
25 p.vel.z = std::sin(angle) * speed;
26 p.color = {180, 255, 120, 255}; // vivid green
27 p.size = 0.06f + (static_cast<float>(rand()) / static_cast<float>(RAND_MAX)) * 0.05f;
28 p.alpha = 1.0f;
29 p.delay = 0.0f;
30 p.active = true;
31 _particles.push_back(p);
32 }
33}
34
36{
37 _elapsed += dt;
38 float t = _elapsed / _duration;
39 if (t > 1.0f) t = 1.0f;
40
41 float smooth = t * t * (3.0f - 2.0f * t);
42 _visual.scale = 0.05f + smooth * (1.0f - 0.05f);
43
44 // burst fires once at 75% through — egg is nearly full size
45 if (!_burstSpawned && t >= 0.75f) {
47 _burstSpawned = true;
48 }
49
50 float burstAge = _elapsed - _duration * 0.75f;
51 for (auto& p : _particles) {
52 if (burstAge <= 0.0f) break;
53 p.pos.x += p.vel.x * dt;
54 p.pos.y += p.vel.y * dt;
55 p.pos.z += p.vel.z * dt;
56 p.vel.y -= 1.5f * dt;
57 p.alpha = 1.0f - (burstAge / (_duration * 0.25f));
58 if (p.alpha < 0.0f) p.alpha = 0.0f;
59 }
60}
61
63{
64 if (_elapsed >= _duration) {
65 _visual.scale = 1.0f;
66 return true;
67 }
68 return false;
69}
static constexpr int PARTICLE_COUNT
static constexpr int PARTICLE_COUNT
std::vector< Particle > _particles
VisualState & _visual
ForkBehavior(VisualState &visual, float server_tick_rate)
void update(float dt) override
void _spawnParticles()
bool isDone() const override
Returns true when the behavior has completed and can be removed.
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