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};
33 nngn::Renderer *renderer = nullptr;
34 nngn::Collider *collider = nullptr;
35 nngn::Animation *anim = nullptr;
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); }
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);
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;
68 nngn::Hash name_hash(const Entity &e) const;
69 nngn::Hash tag_hash(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);
75};
76
77#endif
Definition: entity.h:51
void update_children()
Definition: entity.cpp:175
void clear_flags()
Definition: entity.cpp:182
std::vector< nngn::Hash > name_hashes
Definition: entity.h:54
size_t max() const
Definition: entity.h:56
void set_name(Entity *e, std::string_view s)
Definition: entity.cpp:152
std::vector< std::array< char, 32 > > names
Definition: entity.h:53
std::vector< nngn::Hash > tag_hashes
Definition: entity.h:54
void set_max(std::size_t n)
size_t n() const
Definition: entity.h:57
nngn::Hash tag_hash(const Entity &e) const
Definition: entity.cpp:148
std::span< const char, 32 > tag(const Entity &e) const
Definition: entity.cpp:140
void remove(Entity *e)
Definition: entity.h:60
nngn::static_vector< Entity > v
Definition: entity.h:52
std::vector< std::array< char, 32 > > tags
Definition: entity.h:53
void set_tag(Entity *e, std::string_view s)
Definition: entity.cpp:156
nngn::Hash name_hash(const Entity &e) const
Definition: entity.cpp:144
Entity * add()
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
name
Definition: cathedral.lua:11
l
Definition: light.lua:23
std::chrono::seconds s
Definition: timing.cpp:6
#define NNGN_EXPOSE_ITERATOR(n, m)
Definition: utils.h:67
e
Definition: math.lua:4
Definition: audio.cpp:7
Definition: debug.h:13
Definition: entity.h:24
void set_parent(Entity *e)
nngn::Renderer * renderer
Definition: entity.h:33
void set_collider(nngn::Collider *p)
nngn::vec3 a
Definition: entity.h:32
nngn::Light * light
Definition: entity.h:37
void set_renderer(nngn::Renderer *p)
void set_animation(nngn::Animation *p)
void set_vel(nngn::vec3 v)
void set_pos(nngn::vec3 p)
nngn::Camera * camera
Definition: entity.h:36
float max_v
Definition: entity.h:31
nngn::vec3 v
Definition: entity.h:30
void set_light(nngn::Light *l)
Entity * parent
Definition: entity.h:38
Flag
Definition: entity.h:25
@ ALIVE
Definition: entity.h:26
@ POS_UPDATED
Definition: entity.h:26
nngn::Collider * collider
Definition: entity.h:34
void set_camera(nngn::Camera *p)
nngn::Animation * anim
Definition: entity.h:35
bool pos_updated() const
Definition: entity.h:40
bool alive() const
Definition: entity.h:39
nngn::Flags< Flag > flags
Definition: entity.h:28
nngn::vec3 p
Definition: entity.h:29
Definition: animation.h:44
Abstract orthographic/perspective camera.
Definition: camera.h:18
Definition: colliders.h:15
Wrapper for an unsigned integral representing flags.
Definition: flags.h:18
constexpr bool is_set(AT a) const
Definition: flags.h:26
Definition: light.h:25
Definition: renderers.h:15