nngn
Loading...
Searching...
No Matches
animation.h
Go to the documentation of this file.
1#ifndef NNGN_ANIMATION_H
2#define NNGN_ANIMATION_H
3
4#include <array>
5#include <chrono>
6#include <memory>
7#include <vector>
8
9#include "lua/table.h"
10#include "math/math.h"
11#include "math/vec2.h"
12#include "math/vec3.h"
13#include "utils/flags.h"
14
15struct lua_State;
16struct Entity;
17
18namespace nngn {
19
20struct Light;
21struct SpriteRenderer;
22struct Timing;
23
25public:
26 enum class Type : uint8_t {
28 };
29private:
30 Type type = {};
31 struct linear_t { float v = 0, step_s = 0, end = 0; };
32 union {
34 std::uniform_real_distribution<float> rnd_f;
35 std::array<std::uniform_real_distribution<float>, 3> rnd_3f;
36 };
37public:
38 AnimationFunction() noexcept : linear() {}
41 bool done() const;
42};
43
44struct Animation {
45 Entity *entity = nullptr;
46};
47
48class SpriteAnimation : public Animation {
49public:
50 struct Frame {
51 std::array<vec2, 2> uv;
52 std::chrono::milliseconds duration;
53 };
54 using Track = std::vector<Frame>;
55 using Group = std::vector<Track>;
56private:
57 std::shared_ptr<const Group> m_group = {};
58 size_t m_cur_track = 0, cur_frame = 0;
59 std::chrono::microseconds timer = {};
60 bool updated = true;
61public:
63 void load(SpriteAnimation *o);
64 auto cur_track() const { return this->m_cur_track; }
65 auto track_count() const { return this->m_group->size(); }
66 const Frame *cur() const;
67 void set_track(size_t index);
68 void update(const Timing &t);
69};
70
71class LightAnimation : public Animation {
72 std::chrono::milliseconds rate = {}, timer = {};
74public:
76 void update(
78 float *a, bool *updated);
79 void update(const Timing &t, Math::rnd_generator_t *rnd);
80};
81
83 Math *math = nullptr;
84 std::vector<SpriteAnimation> sprite = {};
85 std::vector<LightAnimation> light = {};
86public:
87 void init(Math *m) { this->math = m; }
88 size_t max() const { return this->max_sprite() + this->max_light(); }
89 size_t max_sprite() const { return this->sprite.capacity(); }
90 size_t max_light() const { return this->light.capacity(); }
91 size_t n() const { return this->n_sprite() + this->n_light(); }
92 size_t n_sprite() const { return this->sprite.size(); }
93 size_t n_light() const { return this->light.size(); }
94 void set_max(size_t n);
95 void remove(Animation *p);
97 void update(const Timing &t);
98};
99
100}
101
102#endif
Definition animation.h:24
Type type
Definition animation.h:30
linear_t linear
Definition animation.h:33
std::uniform_real_distribution< float > rnd_f
Definition animation.h:34
AnimationFunction() noexcept
Definition animation.h:38
bool done() const
Definition animation.cpp:112
Type
Definition animation.h:26
std::array< std::uniform_real_distribution< float >, 3 > rnd_3f
Definition animation.h:35
Definition animation.h:82
size_t max_sprite() const
Definition animation.h:89
std::vector< SpriteAnimation > sprite
Definition animation.h:84
size_t n_sprite() const
Definition animation.h:92
void init(Math *m)
Definition animation.h:87
size_t n() const
Definition animation.h:91
std::vector< LightAnimation > light
Definition animation.h:85
void remove(Animation *p)
Definition animation.cpp:211
void set_max(size_t n)
Definition animation.cpp:206
size_t max() const
Definition animation.h:88
size_t max_light() const
Definition animation.h:90
size_t n_light() const
Definition animation.h:93
Math * math
Definition animation.h:83
Definition animation.h:71
std::chrono::milliseconds timer
Definition animation.h:72
AnimationFunction f
Definition animation.h:73
std::chrono::milliseconds rate
Definition animation.h:72
Definition math.h:22
Definition animation.h:48
size_t m_cur_track
Definition animation.h:58
std::shared_ptr< const Group > m_group
Definition animation.h:57
void set_track(size_t index)
Definition animation.cpp:140
std::vector< Frame > Track
Definition animation.h:54
auto cur_track() const
Definition animation.h:64
const Frame * cur() const
Definition animation.cpp:134
bool updated
Definition animation.h:60
size_t cur_frame
Definition animation.h:58
std::chrono::microseconds timer
Definition animation.h:59
std::vector< Track > Group
Definition animation.h:55
auto track_count() const
Definition animation.h:65
update
Definition img_common.lua:42
Definition audio.cpp:7
local rnd
Definition players.lua:9
load
Definition entity.lua:7
local a
Definition input.lua:35
local m
Definition input.lua:28
Definition entity.h:24
Definition animation.h:31
float end
Definition animation.h:31
float step_s
Definition animation.h:31
float v
Definition animation.h:31
Definition animation.h:44
Entity * entity
Definition animation.h:45
Definition math.h:24
Definition animation.h:50
std::array< vec2, 2 > uv
Definition animation.h:51
std::chrono::milliseconds duration
Definition animation.h:52
Definition timing.h:20
Non-owning reference to a table on the stack.
Definition table.h:166
Operations on table values.