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 uint64_t index : 22; // 4M concurrent entities
37 uint64_t generation : 10; // 1024 generations per index
38 uint64_t world : 8; // world id (not to be confused with world hash)
39 uint64_t reserved : 24; // not currently in use
40
41 Entity() = default;
42 constexpr Entity(uint64_t id);
43 constexpr Entity(uint64_t world, uint64_t generation, uint64_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 namespace_name = "Game";
68 static constexpr auto fully_qualified_name = "Game.Entity";
69 static constexpr size_t num_fields = 1;
70 static constexpr const char* field_names[num_fields] = {
71 "id"
72 };
73 static constexpr const char* field_typenames[num_fields] = {
74 "uint64_t"
75 };
76 static constexpr size_t field_byte_offsets[num_fields] = { 0 };
77
80 static constexpr uint32_t fixed_column_index = 0;
81};
82
83//------------------------------------------------------------------------------
92
93//------------------------------------------------------------------------------
96inline constexpr Entity::Entity(uint64_t id)
97{
98 this->index = id & 0x00000000003FFFFF;
99 this->generation = (id & 0x0000000FFC00000) >> 22;
100 this->world = (id & 0x000000FF00000000) >> 32;
101 this->reserved = 0x0;
102}
103
104//------------------------------------------------------------------------------
107inline constexpr Entity::Entity(uint64_t world, uint64_t generation, uint64_t index)
108{
109 this->index = index;
110 this->generation = generation;
111 this->world = world;
112 this->reserved = 0x0;
113}
114
115//------------------------------------------------------------------------------
118inline constexpr Entity
120{
121 Entity ret = Entity((uint64_t)id);
122 return ret;
123}
124
125//------------------------------------------------------------------------------
128inline constexpr Game::Entity::operator Ids::Id64() const
129{
130 return (((uint64_t)world << 32) & 0x000000FF00000000)
131 + (((uint64_t)generation << 22) & 0x0000000FFC00000)
132 + ((uint64_t)index & 0x00000000003FFFFF);
133}
134
135//------------------------------------------------------------------------------
138inline constexpr Entity
140{
141 return 0xFFFFFFFFFFFFFFFF;
142}
143
144//------------------------------------------------------------------------------
147inline constexpr uint32_t
149{
150 return index;
151}
152
153//------------------------------------------------------------------------------
156inline Entity&
158{
159 this->index = id & 0x00000000003FFFFF;
160 this->generation = (id & 0x0000000FFC00000) >> 22;
161 this->world = (id & 0x000000FF00000000) >> 32;
162 this->reserved = (id & 0xFFFFFF0000000000) >> 40;
163 return *this;
164}
165
166//------------------------------------------------------------------------------
169inline const bool
170Entity::operator==(const Entity& rhs) const
171{
172 return Ids::Id64(*this) == Ids::Id64(rhs);
173}
174
175//------------------------------------------------------------------------------
178inline const bool
179Entity::operator!=(const Entity& rhs) const
180{
181 return Ids::Id64(*this) != Ids::Id64(rhs);
182}
183
184//------------------------------------------------------------------------------
187inline const bool
188Entity::operator<(const Entity& rhs) const
189{
190 return index < rhs.index;
191}
192
193//------------------------------------------------------------------------------
196inline const bool
197Entity::operator>(const Entity& rhs) const
198{
199 return index > rhs.index;
200}
201
202} // namespace Game
#define ID_32_TYPE(x)
Definition id.h:16
Game::EditorState.
Definition graphicsmanager.h:66
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:76
Entity type
Definition entity.h:65
static constexpr const char * field_names[num_fields]
Definition entity.h:70
static constexpr auto fully_qualified_name
Definition entity.h:68
static constexpr const char * field_typenames[num_fields]
Definition entity.h:73
static constexpr auto name
Definition entity.h:66
static constexpr size_t num_fields
Definition entity.h:69
static constexpr uint32_t fixed_column_index
This is the column that the entity "owner" will reside in, in every table.
Definition entity.h:80
static constexpr auto namespace_name
Definition entity.h:67
An entity is essentially just an Id with some utility functions attached.
Definition entity.h:35
uint64_t reserved
Definition entity.h:39
constexpr uint32_t HashCode() const
Definition entity.h:148
const bool operator>(const Entity &rhs) const
Definition entity.h:197
static constexpr Entity Invalid()
Definition entity.h:139
Entity()=default
const bool operator==(const Entity &rhs) const
Definition entity.h:170
uint64_t world
Definition entity.h:38
uint64_t generation
Definition entity.h:37
const bool operator!=(const Entity &rhs) const
Definition entity.h:179
uint64_t index
Definition entity.h:36
Entity & operator=(uint64_t rhs)
Definition entity.h:157
static constexpr Entity FromId(Ids::Id64 id)
Definition entity.h:119
const bool operator<(const Entity &rhs) const
Definition entity.h:188
Maps an entity to a table and instance id.
Definition entity.h:88
MemDb::RowId instance
Definition entity.h:90
MemDb::TableId table
Definition entity.h:89
Definition entity.h:29
row identifier
Definition tableid.h:18
Table identifier.
Definition tableid.h:14