nngn
Loading...
Searching...
No Matches
entity.h
Go to the documentation of this file.
1#ifndef NNGN_ENTITY_H
2#define NNGN_ENTITY_H
3
4#include <array>
5#include <memory>
6#include <string_view>
7#include <vector>
8
9#include "math/hash.h"
10#include "math/vec3.h"
11#include "utils/flags.h"
12#include "utils/static_vector.h"
13#include "utils/utils.h"
14
15namespace nngn {
16 struct Camera;
17 struct Animation;
18 struct Collider;
19 struct Light;
20 struct Renderer;
21 struct Timing;
22}
23
24struct Entity {
25 enum Flag : std::uintptr_t {
26 ALIVE = 1u << 0, POS_UPDATED = 1u << 1,
27 };
29 nngn::vec3 p = {0, 0, 0};
30 nngn::vec3 v = {0, 0, 0};
31 float max_v = {};
32 nngn::vec3 a = {0, 0, 0};
36 nngn::Camera *camera = nullptr;
37 nngn::Light *light = nullptr;
38 Entity *parent = nullptr;
39 bool alive() const { return this->flags.is_set(Flag::ALIVE); }
40 bool pos_updated() const { return this->flags.is_set(Flag::POS_UPDATED); }
41 void set_pos(nngn::vec3 p);
42 void set_vel(nngn::vec3 v);
47 void set_light(nngn::Light *l);
48 void set_parent(Entity *e);
49};
50
51class Entities {
53 std::vector<std::array<char, 32>> names = {}, tags = {};
54 std::vector<nngn::Hash> name_hashes = {}, tag_hashes = {};
55public:
56 size_t max() const { return this->v.capacity(); }
57 size_t n() const { return this->v.size(); }
58 void set_max(std::size_t n);
59 Entity *add();
60 void remove(Entity *e) { this->v.erase(e); }
66 std::span<const char, 32> name(const Entity &e) const;
67 std::span<const char, 32> tag(const Entity &e) const;
70 void set_name(Entity *e, std::string_view s);
71 void set_tag(Entity *e, std::string_view s);
72 void update(const nngn::Timing &t);
73 void update_children();
74 void clear_flags();
75};
76
77#endif
local class const
Definition animation.lua:7
name
Definition cathedral.lua:11
Definition entity.h:51
void update_children()
Definition entity.cpp:177
std::vector< nngn::Hash > name_hashes
Definition entity.h:54
std::vector< std::array< char, 32 > > names
Definition entity.h:53
std::vector< std::array< char, 32 > > tags
Definition entity.h:53
std::vector< nngn::Hash > tag_hashes
Definition entity.h:54
void clear_flags()
Definition entity.cpp:184
size_t max() const
Definition entity.h:56
void set_name(Entity *e, std::string_view s)
Definition entity.cpp:154
nngn::static_vector< Entity > v
Definition entity.h:52
void set_max(std::size_t n)
Definition entity.cpp:116
size_t n() const
Definition entity.h:57
nngn::Hash tag_hash(const Entity &e) const
Definition entity.cpp:150
std::span< const char, 32 > tag(const Entity &e) const
Definition entity.cpp:142
void remove(Entity *e)
Definition entity.h:60
void set_tag(Entity *e, std::string_view s)
Definition entity.cpp:158
nngn::Hash name_hash(const Entity &e) const
Definition entity.cpp:146
Entity * add()
Definition entity.cpp:124
Fixed-size vector with an embedded free list.
Definition static_vector.h:21
std::size_t size(void) const
Calculates the true size (excluding the entries in the free list).
Definition static_vector.h:87
void erase(T *p)
Removes element p from the vector.
Definition static_vector.h:123
update
Definition img_common.lua:42
local l
Definition light.lua:23
Definition audio.cpp:7
Definition debug.h:13
#define NNGN_EXPOSE_ITERATOR(n, m)
Definition utils.h:67
Definition entity.h:24
void set_parent(Entity *e)
Definition entity.cpp:111
void set_collider(nngn::Collider *p)
Definition entity.cpp:86
nngn::vec3 a
Definition entity.h:32
nngn::Camera * camera
Definition entity.h:36
void set_renderer(nngn::Renderer *p)
Definition entity.cpp:79
void set_animation(nngn::Animation *p)
Definition entity.cpp:94
void set_vel(nngn::vec3 v)
Definition entity.cpp:73
void set_pos(nngn::vec3 p)
Definition entity.cpp:67
nngn::Renderer * renderer
Definition entity.h:33
nngn::Flags< Flag > flags
Definition entity.h:28
float max_v
Definition entity.h:31
nngn::vec3 v
Definition entity.h:30
void set_light(nngn::Light *l)
Definition entity.cpp:104
Flag
Definition entity.h:25
@ ALIVE
Definition entity.h:26
@ POS_UPDATED
Definition entity.h:26
Entity * parent
Definition entity.h:38
void set_camera(nngn::Camera *p)
Definition entity.cpp:99
bool pos_updated() const
Definition entity.h:40
nngn::Animation * anim
Definition entity.h:35
bool alive() const
Definition entity.h:39
nngn::Light * light
Definition entity.h:37
nngn::vec3 p
Definition entity.h:29
nngn::Collider * collider
Definition entity.h:34
Definition animation.h:44
Abstract orthographic/perspective camera.
Definition camera.h:69
Definition colliders.h:15
Wrapper for a small unsigned integral representing flags.
Definition flags.h:18
constexpr bool is_set(AT a) const
Definition flags.h:45
Definition light.h:25
Definition renderers.h:15
local e
Definition math.lua:4
std::chrono::seconds s
Definition timing.cpp:6