Nebula
Toggle main menu visibility
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
24
namespace
Game
25
{
26
27
// TODO: move this to a separate file
28
typedef
uint32_t
WorldId
;
29
ID_32_TYPE
(
WorldHash
);
30
31
//------------------------------------------------------------------------------
34
struct
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
//------------------------------------------------------------------------------
62
struct
Entity::Traits
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_t"
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
//------------------------------------------------------------------------------
86
struct
EntityMapping
87
{
88
MemDb::TableId
table
;
89
MemDb::RowId
instance
;
90
};
91
92
//------------------------------------------------------------------------------
95
inline
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
//------------------------------------------------------------------------------
106
inline
constexpr
Entity::Entity
(uint64_t
world
, uint64_t
generation
, uint64_t
index
)
107
{
108
this->index =
index
;
109
this->generation =
generation
;
110
this->world =
world
;
111
this->
reserved
= 0x0;
112
}
113
114
//------------------------------------------------------------------------------
117
inline
constexpr
Entity
118
Entity::FromId
(
Ids::Id64
id
)
119
{
120
Entity
ret =
Entity
((uint64_t)
id
);
121
return
ret;
122
}
123
124
//------------------------------------------------------------------------------
127
inline
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
//------------------------------------------------------------------------------
137
inline
constexpr
Entity
138
Entity::Invalid
()
139
{
140
return
0xFFFFFFFFFFFFFFFF;
141
}
142
143
//------------------------------------------------------------------------------
146
inline
constexpr
uint32_t
147
Entity::HashCode
()
const
148
{
149
return
index
;
150
}
151
152
//------------------------------------------------------------------------------
155
inline
Entity
&
156
Entity::operator=
(uint64_t
id
)
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
//------------------------------------------------------------------------------
168
inline
const
bool
169
Entity::operator==
(
const
Entity
& rhs)
const
170
{
171
return
Ids::Id64
(*
this
) ==
Ids::Id64
(rhs);
172
}
173
174
//------------------------------------------------------------------------------
177
inline
const
bool
178
Entity::operator!=
(
const
Entity
& rhs)
const
179
{
180
return
Ids::Id64
(*
this
) !=
Ids::Id64
(rhs);
181
}
182
183
//------------------------------------------------------------------------------
186
inline
const
bool
187
Entity::operator<
(
const
Entity
& rhs)
const
188
{
189
return
index
< rhs.
index
;
190
}
191
192
//------------------------------------------------------------------------------
195
inline
const
bool
196
Entity::operator>
(
const
Entity
& rhs)
const
197
{
198
return
index
> rhs.
index
;
199
}
200
201
}
// namespace Game
id.h
ID_32_TYPE
#define ID_32_TYPE(x)
Definition
id.h:16
Game
Game::EditorState.
Definition
graphicsmanager.h:67
Game::WorldId
uint32_t WorldId
Definition
entity.h:28
Ids::Id64
uint64_t Id64
Definition
id.h:137
Game::Entity::Traits
Definition
entity.h:63
Game::Entity::Traits::field_byte_offsets
static constexpr size_t field_byte_offsets[num_fields]
Definition
entity.h:75
Game::Entity::Traits::type
Entity type
Definition
entity.h:65
Game::Entity::Traits::field_names
static constexpr const char * field_names[num_fields]
Definition
entity.h:69
Game::Entity::Traits::fully_qualified_name
static constexpr auto fully_qualified_name
Definition
entity.h:67
Game::Entity::Traits::field_typenames
static constexpr const char * field_typenames[num_fields]
Definition
entity.h:72
Game::Entity::Traits::Traits
Traits()=delete
Game::Entity::Traits::name
static constexpr auto name
Definition
entity.h:66
Game::Entity::Traits::num_fields
static constexpr size_t num_fields
Definition
entity.h:68
Game::Entity::Traits::fixed_column_index
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
Game::Entity
An entity is essentially just an Id with some utility functions attached.
Definition
entity.h:35
Game::Entity::reserved
uint64_t reserved
Definition
entity.h:39
Game::Entity::HashCode
constexpr uint32_t HashCode() const
Definition
entity.h:147
Game::Entity::operator>
const bool operator>(const Entity &rhs) const
Definition
entity.h:196
Game::Entity::Invalid
static constexpr Entity Invalid()
Definition
entity.h:138
Game::Entity::Entity
Entity()=default
Game::Entity::operator==
const bool operator==(const Entity &rhs) const
Definition
entity.h:169
Game::Entity::world
uint64_t world
Definition
entity.h:38
Game::Entity::generation
uint64_t generation
Definition
entity.h:37
Game::Entity::operator!=
const bool operator!=(const Entity &rhs) const
Definition
entity.h:178
Game::Entity::index
uint64_t index
Definition
entity.h:36
Game::Entity::operator=
Entity & operator=(uint64_t rhs)
Definition
entity.h:156
Game::Entity::FromId
static constexpr Entity FromId(Ids::Id64 id)
Definition
entity.h:118
Game::Entity::operator<
const bool operator<(const Entity &rhs) const
Definition
entity.h:187
Game::EntityMapping
Maps an entity to a table and instance id.
Definition
entity.h:87
Game::EntityMapping::instance
MemDb::RowId instance
Definition
entity.h:89
Game::EntityMapping::table
MemDb::TableId table
Definition
entity.h:88
Game::WorldHash
Definition
entity.h:29
MemDb::RowId
row identifier
Definition
tableid.h:18
MemDb::TableId
Table identifier.
Definition
tableid.h:14
tableid.h
code
application
game
entity.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.