forked from mas-bandwidth/cubes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.h
93 lines (71 loc) · 2.72 KB
/
world.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
#ifndef WORLD_H
#define WORLD_H
#include "platform.h"
#include "entity.h"
#include "cubes.h"
#include <stdio.h>
struct World
{
bool active = true; // true if the world is active (eg. player input is processed, physics runs)
uint64_t frame = 0; // current frame (server or client frame depending on context)
uint64_t tick = 0; // current tick (ticks @ 240HZ)
double time = 0.0; // current time in seconds
EntityManager * entity_manager = nullptr; // indexes entities in this world
PhysicsManager * physics_manager = nullptr; // the physics simulation
CubeManager * cube_manager = nullptr; // manager for cube entities
};
inline void world_init( World & world )
{
world.entity_manager = new EntityManager();
world.physics_manager = new PhysicsManager();
world.cube_manager = new CubeManager( world.entity_manager, world.physics_manager );
world.physics_manager->Initialize();
}
inline void world_add_cube( World & world, const vec3f & position, float scale, bool active, int required_index = ENTITY_NULL )
{
world.cube_manager->CreateCube( position, scale, active, required_index );
}
inline void world_setup_cubes( World & world )
{
const float PlayerCubeSize = 1.5f;
// const float NonPlayerCubeSize = 0.4f;
world.physics_manager->AddPlane( vec3f(0,0,1), 0 );
world_add_cube( world, vectorial::vec3f(0,0,10), PlayerCubeSize, true, ENTITY_PLAYER_BEGIN );
/*
const int grid_size = 30;
const float origin = -grid_size / 2.0f + 0.5f;
const float z = NonPlayerCubeSize / 2.0f;
for ( int y = 0; y < grid_size; ++y )
{
for ( int x = 0; x < grid_size; ++x )
{
world_add_cube( world, vec3f( x + origin, y + origin, z ), NonPlayerCubeSize, false );
}
}
*/
}
inline void world_tick( World & world )
{
if ( world.active )
{
world.cube_manager->UpdateAuthority( world.time, TickDeltaTime );
world.cube_manager->PrePhysicsUpdate();
#ifndef CLIENT
// hack: run physics on the server only for now
world.physics_manager->Update( world.tick, world.time, TickDeltaTime );
world.physics_manager->WalkInteractions( world.entity_manager );
#endif // #ifndef CLIENT
world.cube_manager->PostPhysicsUpdate();
world.entity_manager->UpdateAuthority( world.time, TickDeltaTime );
}
world.time += TickDeltaTime;
world.tick++;
}
inline void world_free( World & world )
{
delete world.entity_manager;
delete world.physics_manager;
delete world.cube_manager;
world = World();
}
#endif // #ifndef WORLD_H