-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldService.c
78 lines (70 loc) · 2.28 KB
/
WorldService.c
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
#include "IWorld.h"
#include <assert.h>
#include <string.h>
#include "engine.h"
#include "enginedefs.h"
#include "raymath.h"
static UID num_entities = 0;
static Entity entities[MAX_ENTITIES] = {0};
UID world_create_entity(Vector3 position,Vector3 scale,Vector3 rotation){
Entity* ent = &entities[num_entities];
memset(ent,0,sizeof(Entity));
ent->active = 1;
memset(ent->components,0,sizeof(Component) * MAX_COMPONENTS);
ent->transform = MatrixTranslate(position.x,position.y,position.z);
ent->transform = MatrixMultiply(ent->transform,MatrixScale(scale.x,scale.y,scale.z));
ent->transform = MatrixMultiply(ent->transform,MatrixRotateXYZ(rotation));
ent->id = num_entities;
return num_entities++;
}
int world_add_component(UID id,void* component){
Entity* ent = &entities[id];
int i = 0;
for(; i < MAX_COMPONENTS;++i){
if(ent->components[i] == NULL){
ent->components[i] = component;
return i;
}
}
assert(i< MAX_COMPONENTS);
return -1;//Failed to add component
}
void world_update(void* data){
Component* comp;
for(int i =0; i < num_entities;++i){
Entity* ent = &entities[i];
if(!ent->active) continue;
for(int y = 0; y < MAX_COMPONENTS && ent->components[y] != NULL;++y){
comp = ent->components[y];
if(comp->type == Updateable || comp->type == Both){
comp->funcs[Updateable](ent);
}
}
}
}
void world_render(void* data){
Engine* eng = engine_get();
eng->gfx->begin(0);
eng->gfx->clear(0x151515FF);
Component* comp;
for(int i =0; i < num_entities;++i){
Entity* ent = &entities[i];
if(!ent->active) continue;
for(int y = 0; y < MAX_COMPONENTS && ent->components[y] != NULL;++y){
comp = ent->components[y];
if(comp->type == Renderable || comp->type == Both){
comp->funcs[Renderable](ent);
}
}
}
eng->gfx->end(0);
}
static IWorld world = {0};
void create_world_provider(void* engine){
world.createEntity = world_create_entity;
world.addComponent = world_add_component;
world.private_funcs[0] = world_update;
world.private_funcs[1] = world_render;
Engine* eng = (Engine*)engine;
eng->world = &world;
}