-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcubes.h
160 lines (130 loc) · 4.99 KB
/
cubes.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
// Copyright © 2015, The Network Protocol Company, Inc. All Rights Reserved.
#ifndef CUBES_H
#define CUBES_H
#include "entity.h"
#include "physics.h"
#include "shared.h"
struct CubeEntity : public Entity
{
float scale;
CubeEntity() : scale( 1.0f ) {}
};
struct CubeManager
{
EntityManager * entity_manager;
PhysicsManager * physics_manager;
bool allocated[MaxCubes];
int entity_index_to_cube_index[MaxCubes];
CubeEntity cubes[MaxCubes];
CubeManager( EntityManager * entity_manager, PhysicsManager * physics_manager )
{
assert( entity_manager );
assert( physics_manager );
this->entity_manager = entity_manager;
this->physics_manager = physics_manager;
memset( allocated, 0, sizeof( allocated ) );
for ( int i = 0; i < MaxCubes; ++i )
entity_index_to_cube_index[i] = -1;
}
CubeEntity * CreateCube( const vec3f & position, float scale, bool active, int required_entity_index = ENTITY_NULL )
{
const int entity_index = entity_manager->Allocate( required_entity_index );
if ( entity_index == ENTITY_NULL )
return nullptr;
for ( int i = 0; i < MaxCubes; ++i )
{
if ( !allocated[i] )
{
allocated[i] = true;
entity_index_to_cube_index[entity_index] = i;
cubes[i].entity_index = entity_index;
PhysicsObjectState initial_state;
initial_state.active = active;
initial_state.position = position;
cubes[i].physics_index = physics_manager->AddObject( entity_index, initial_state, PHYSICS_SHAPE_CUBE, scale );
cubes[i].position = position;
cubes[i].scale = scale;
entity_manager->SetEntity( entity_index, &cubes[i], ENTITY_TYPE_CUBE );
return &cubes[i];
}
}
return nullptr;
}
void DestroyCube( CubeEntity * cube_entity )
{
assert( cube_entity );
const int entity_index = cube_entity->entity_index;
assert( entity_index >= 0 );
assert( entity_index < MaxEntities );
const int cube_index = entity_index_to_cube_index[entity_index];
assert( cube_index >= 0 );
assert( cube_index < MaxCubes );
allocated[cube_index] = false;
entity_index_to_cube_index[entity_index] = -1;
cubes[cube_index] = CubeEntity();
entity_manager->Free( entity_index );
}
void SetCubeState( int cube_index,
const vec3f & position,
const quat4f & orientation,
const vec3f & linear_velocity,
const vec3f & angular_velocity )
{
assert( cube_index >= 0 );
assert( cube_index < MaxCubes );
if ( !allocated[cube_index] )
return;
cubes[cube_index].position = position;
cubes[cube_index].orientation = orientation;
cubes[cube_index].linear_velocity = linear_velocity;
cubes[cube_index].angular_velocity = angular_velocity;
}
void PrePhysicsUpdate()
{
PhysicsObjectState object_state;
for ( int i = 0; i < MaxCubes; ++i )
{
if ( !allocated[i] )
continue;
object_state.active = physics_manager->IsActive( cubes[i].physics_index );
object_state.position = cubes[i].position;
object_state.orientation = cubes[i].orientation;
object_state.linear_velocity = cubes[i].linear_velocity;
object_state.angular_velocity = cubes[i].angular_velocity;
physics_manager->SetObjectState( cubes[i].physics_index, object_state );
}
}
void PostPhysicsUpdate()
{
PhysicsObjectState object_state;
for ( int i = 0; i < MaxCubes; ++i )
{
if ( !allocated[i] )
continue;
physics_manager->GetObjectState( cubes[i].physics_index, object_state );
cubes[i].position = object_state.position;
cubes[i].orientation = object_state.orientation;
cubes[i].linear_velocity = object_state.linear_velocity;
cubes[i].angular_velocity = object_state.angular_velocity;
}
}
void UpdateAuthority( double t, float dt )
{
for ( int i = 0; i < MaxCubes; ++i )
{
if ( !allocated[i] )
continue;
const int authority = entity_manager->GetAuthority( cubes[i].entity_index );
if ( authority == 0 )
continue;
bool active = physics_manager->IsActive( cubes[i].physics_index );
if ( active )
continue;
float & authority_time = entity_manager->authority_time[cubes[i].entity_index];
authority_time += dt;
if ( authority_time > AuthorityThreshold )
entity_manager->SetAuthority( cubes[i].entity_index, 0 );
}
}
};
#endif // #ifndef CUBES_H