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
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
//------------------------------------------------------------------------------
87
struct
EntityMapping
88
{
89
MemDb::TableId
table
;
90
MemDb::RowId
instance
;
91
};
92
93
//------------------------------------------------------------------------------
96
inline
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
//------------------------------------------------------------------------------
107
inline
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
//------------------------------------------------------------------------------
118
inline
constexpr
Entity
119
Entity::FromId
(
Ids::Id64
id
)
120
{
121
Entity
ret =
Entity
((uint64_t)
id
);
122
return
ret;
123
}
124
125
//------------------------------------------------------------------------------
128
inline
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
//------------------------------------------------------------------------------
138
inline
constexpr
Entity
139
Entity::Invalid
()
140
{
141
return
0xFFFFFFFFFFFFFFFF;
142
}
143
144
//------------------------------------------------------------------------------
147
inline
constexpr
uint32_t
148
Entity::HashCode
()
const
149
{
150
return
index
;
151
}
152
153
//------------------------------------------------------------------------------
156
inline
Entity
&
157
Entity::operator=
(uint64_t
id
)
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
//------------------------------------------------------------------------------
169
inline
const
bool
170
Entity::operator==
(
const
Entity
& rhs)
const
171
{
172
return
Ids::Id64
(*
this
) ==
Ids::Id64
(rhs);
173
}
174
175
//------------------------------------------------------------------------------
178
inline
const
bool
179
Entity::operator!=
(
const
Entity
& rhs)
const
180
{
181
return
Ids::Id64
(*
this
) !=
Ids::Id64
(rhs);
182
}
183
184
//------------------------------------------------------------------------------
187
inline
const
bool
188
Entity::operator<
(
const
Entity
& rhs)
const
189
{
190
return
index
< rhs.
index
;
191
}
192
193
//------------------------------------------------------------------------------
196
inline
const
bool
197
Entity::operator>
(
const
Entity
& rhs)
const
198
{
199
return
index
> rhs.
index
;
200
}
201
202
}
// namespace Game
id.h
ID_32_TYPE
#define ID_32_TYPE(x)
Definition
id.h:16
Game
Game::EditorState.
Definition
graphicsmanager.h:66
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:76
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:70
Game::Entity::Traits::fully_qualified_name
static constexpr auto fully_qualified_name
Definition
entity.h:68
Game::Entity::Traits::field_typenames
static constexpr const char * field_typenames[num_fields]
Definition
entity.h:73
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:69
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:80
Game::Entity::Traits::namespace_name
static constexpr auto namespace_name
Definition
entity.h:67
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:148
Game::Entity::operator>
const bool operator>(const Entity &rhs) const
Definition
entity.h:197
Game::Entity::Invalid
static constexpr Entity Invalid()
Definition
entity.h:139
Game::Entity::Entity
Entity()=default
Game::Entity::operator==
const bool operator==(const Entity &rhs) const
Definition
entity.h:170
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:179
Game::Entity::index
uint64_t index
Definition
entity.h:36
Game::Entity::operator=
Entity & operator=(uint64_t rhs)
Definition
entity.h:157
Game::Entity::FromId
static constexpr Entity FromId(Ids::Id64 id)
Definition
entity.h:119
Game::Entity::operator<
const bool operator<(const Entity &rhs) const
Definition
entity.h:188
Game::EntityMapping
Maps an entity to a table and instance id.
Definition
entity.h:88
Game::EntityMapping::instance
MemDb::RowId instance
Definition
entity.h:90
Game::EntityMapping::table
MemDb::TableId table
Definition
entity.h:89
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.