-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathentity.h
168 lines (148 loc) · 4.23 KB
/
entity.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright © 2015, The Network Protocol Company, Inc. All Rights Reserved.
#ifndef ENTITY_H
#define ENTITY_H
#include "const.h"
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include "vectorial/vec3f.h"
#include "vectorial/vec4f.h"
#include "vectorial/quat4f.h"
#include "vectorial/mat4f.h"
using namespace vectorial;
enum EntityFlags
{
ENTITY_FLAG_ALLOCATED = 1
};
enum EntityType
{
ENTITY_TYPE_WORLD = 0,
ENTITY_TYPE_CUBE = 1
};
struct Entity
{
Entity()
{
entity_index = ENTITY_WORLD;
physics_index = PHYSICS_NULL;
position = vec3f(0,0,0);
orientation = quat4f(0,0,0,1);
linear_velocity = vec3f(0,0,0);
angular_velocity = vec3f(0,0,0);
}
int physics_index;
int entity_index;
vec3f position;
quat4f orientation;
vec3f linear_velocity;
vec3f angular_velocity;
};
struct EntityManager
{
int num_entities = 0;
int entity_alloc_index = ENTITY_PLAYER_END;
uint8_t flags[MaxEntities];
uint8_t types[MaxEntities];
uint8_t sequence[MaxEntities];
int authority[MaxEntities];
float authority_time[MaxEntities];
Entity * entities[MaxEntities];
EntityManager()
{
memset( flags, 0, sizeof( flags ) );
memset( types, 0, sizeof( types ) );
memset( sequence, 0, sizeof( sequence ) );
memset( authority, 0, sizeof( authority ) );
memset( authority_time, 0, sizeof( authority_time ) );
memset( entities, 0, sizeof( entities ) );
}
int Allocate( int required_index = ENTITY_NULL )
{
assert( num_entities >= 0 );
assert( num_entities <= MaxEntities );
if ( num_entities == MaxEntities )
return ENTITY_NULL;
if ( required_index != ENTITY_NULL )
{
if ( ( flags[required_index] & ENTITY_FLAG_ALLOCATED ) == 0 )
{
num_entities++;
flags[required_index] = ENTITY_FLAG_ALLOCATED;
return required_index;
}
else
return ENTITY_NULL;
}
int allocated_index = ENTITY_NULL;
for ( int i = ENTITY_PLAYER_END; i < MaxEntities; ++i )
{
allocated_index = entity_alloc_index++;
if ( ( flags[allocated_index] & ENTITY_FLAG_ALLOCATED ) == 0 )
break;
}
num_entities++;
assert( allocated_index != ENTITY_NULL );
assert( num_entities <= MaxEntities );
flags[allocated_index] = ENTITY_FLAG_ALLOCATED;
return allocated_index;
}
void Free( int index )
{
assert( index >= 0 );
assert( index <= MaxEntities );
assert( flags[index] & ENTITY_FLAG_ALLOCATED );
assert( num_entities > 0 );
flags[index] = 0;
types[index] = ENTITY_TYPE_WORLD;
entities[index] = nullptr;
sequence[index]++;
}
EntityType GetType( int index ) const
{
assert( index >= 0 );
assert( index < MaxEntities );
return (EntityType) types[index];
}
uint8_t & GetFlag( int index )
{
assert( index >= 0 );
assert( index < MaxEntities );
return flags[index];
}
void SetEntity( int index, Entity * entity, EntityType type )
{
assert( index >= 0 );
assert( index < MaxEntities );
assert( flags[index] & ENTITY_FLAG_ALLOCATED );
assert( entities[index] == nullptr );
entities[index] = entity;
types[index] = type;
}
Entity * GetEntity( int index )
{
assert( index >= 0 );
assert( index < MaxEntities );
return entities[index];
}
int GetAuthority( int index )
{
assert( index >= 0 );
assert( index < MaxEntities );
return authority[index];
}
void SetAuthority( int index, int value )
{
assert( index >= 0 );
assert( index < MaxEntities );
assert( value >= ENTITY_WORLD );
assert( value < ENTITY_PLAYER_END );
authority[index] = value;
authority_time[index] = 0;
}
void UpdateAuthority( double t, float dt )
{
for ( int i = ENTITY_PLAYER_BEGIN; i < ENTITY_PLAYER_END; ++i )
SetAuthority( i, i );
}
};
#endif // #ifndef ENTITY_H