Nebula
Loading...
Searching...
No Matches
entity.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
20//------------------------------------------------------------------------------
21#include "ids/id.h"
22#include "memdb/tableid.h"
23
24namespace Game
25{
26
27// TODO: move this to a separate file
28typedef uint32_t WorldId;
30
31//------------------------------------------------------------------------------
34struct Entity
35{
36 uint32_t index : 22; // 4M concurrent entities
37 uint32_t generation : 10; // 1024 generations per index
38 uint32_t world : 8; // world id (not to be confused with world hash)
39 uint32_t reserved : 24; // not currently in use
40
41 Entity() = default;
42 constexpr Entity(uint64_t id);
43 constexpr Entity(uint32_t world, uint32_t generation, uint32_t index);
44
45 static constexpr Entity FromId(Ids::Id64 id);
46
47 explicit constexpr operator Ids::Id64() const;
48 static constexpr Entity Invalid();
49 constexpr uint32_t HashCode() const;
50 Entity& operator=(uint64_t rhs);
51 const bool operator==(const Entity& rhs) const;
52 const bool operator!=(const Entity& rhs) const;
53 const bool operator<(const Entity& rhs) const;
54 const bool operator>(const Entity& rhs) const;
55
56 struct Traits;
57};
58
59//------------------------------------------------------------------------------
63{
64 Traits() = delete;
65 using type = Entity;
66 static constexpr auto name = "Entity";
67 static constexpr auto fully_qualified_name = "Game.Entity";
68 static constexpr size_t num_fields = 1;
69 static constexpr const char* field_names[num_fields] = {
70 "id"
71 };
72 static constexpr const char* field_typenames[num_fields] = {
73 "uint64"
74 };
75 static constexpr size_t field_byte_offsets[num_fields] = { 0 };
76
79 static constexpr uint32_t fixed_column_index = 0;
80};
81
82//------------------------------------------------------------------------------
91
92//------------------------------------------------------------------------------
95inline constexpr Entity::Entity(uint64_t id)
96{
97 this->index = id & 0x00000000003FFFFF;
98 this->generation = (id & 0x0000000FFC00000) >> 22;
99 this->world = (id & 0x000000FF00000000) >> 32;
100 this->reserved = 0x0;
101}
102
103//------------------------------------------------------------------------------
106inline constexpr Entity::Entity(uint32_t world, uint32_t generation, uint32_t index)
107{
108 this->index = index;
109 this->generation = generation;
110 this->world = world;
111 this->reserved = 0x0;
112}
113
114//------------------------------------------------------------------------------
117inline constexpr Entity
119{
120 Entity ret = Entity((uint64_t)id);
121 return ret;
122}
123
124//------------------------------------------------------------------------------
127inline constexpr Game::Entity::operator Ids::Id64() const
128{
129 return (((uint64_t)world << 32) & 0x000000FF00000000)
130 + (((uint64_t)generation << 22) & 0x0000000FFC00000)
131 + ((uint64_t)index & 0x00000000003FFFFF);
132}
133
134//------------------------------------------------------------------------------
137inline constexpr Entity
139{
140 return 0xFFFFFFFFFFFFFFFF;
141}
142
143//------------------------------------------------------------------------------
146inline constexpr uint32_t
148{
149 return index;
150}
151
152//------------------------------------------------------------------------------
155inline Entity&
157{
158 this->index = id & 0x00000000003FFFFF;
159 this->generation = (id & 0x0000000FFC00000) >> 22;
160 this->world = (id & 0x000000FF00000000) >> 32;
161 this->reserved = (id & 0xFFFFFF0000000000) >> 40;
162 return *this;
163}
164
165//------------------------------------------------------------------------------
168inline const bool
169Entity::operator==(const Entity& rhs) const
170{
171 return Ids::Id64(*this) == Ids::Id64(rhs);
172}
173
174//------------------------------------------------------------------------------
177inline const bool
178Entity::operator!=(const Entity& rhs) const
179{
180 return Ids::Id64(*this) != Ids::Id64(rhs);
181}
182
183//------------------------------------------------------------------------------
186inline const bool
187Entity::operator<(const Entity& rhs) const
188{
189 return index < rhs.index;
190}
191
192//------------------------------------------------------------------------------
195inline const bool
196Entity::operator>(const Entity& rhs) const
197{
198 return index > rhs.index;
199}
200
201} // namespace Game
#define ID_32_TYPE(x)
Definition id.h:16
Game::EditorState.
Definition graphicsmanager.h:60
uint32_t WorldId
Definition entity.h:28
uint64_t Id64
Definition id.h:137
Definition entity.h:63
static constexpr size_t field_byte_offsets[num_fields]
Definition entity.h:75
static constexpr const char * field_names[num_fields]
Definition entity.h:69
static constexpr auto fully_qualified_name
Definition entity.h:67
static constexpr const char * field_typenames[num_fields]
Definition entity.h:72
static constexpr auto name
Definition entity.h:66
static constexpr size_t num_fields
Definition entity.h:68
static constexpr uint32_t fixed_column_index
This is the column that the entity "owner" will reside in, in every table.
Definition entity.h:79
An entity is essentially just an Id with some utility functions attached.
Definition entity.h:35
uint32_t reserved
Definition entity.h:39
constexpr uint32_t HashCode() const
Definition entity.h:147
const bool operator>(const Entity &rhs) const
Definition entity.h:196
static constexpr Entity Invalid()
Definition entity.h:138
Entity()=default
const bool operator==(const Entity &rhs) const
Definition entity.h:169
uint32_t world
Definition entity.h:38
uint32_t generation
Definition entity.h:37
uint32_t index
Definition entity.h:36
const bool operator!=(const Entity &rhs) const
Definition entity.h:178
Entity & operator=(uint64_t rhs)
Definition entity.h:156
static constexpr Entity FromId(Ids::Id64 id)
Definition entity.h:118
const bool operator<(const Entity &rhs) const
Definition entity.h:187
Maps an entity to a table and instance id.
Definition entity.h:87
MemDb::RowId instance
Definition entity.h:89
MemDb::TableId table
Definition entity.h:88
Definition entity.h:29
row identifier
Definition tableid.h:18
Table identifier.
Definition tableid.h:14