-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathECS.c
57 lines (48 loc) · 1.49 KB
/
ECS.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
#include <stdio.h>
#include "ECS.h"
#include "Player.h"
extern int screenWidth, screenHeight;
extern unsigned long ENTITIES, MIN_KEY;
extern struct Config config;
struct Component_Tables components;
struct Registry drawables, hazards, players;
void runSystems() {
for (size_t i = 0; i < players.count; i++) {
playerMove(players.idArray[i]);
}
}
void prepareECS() {
players.idArray = calloc(64, sizeof(unsigned long));
players.count = 0;
drawables.idArray = calloc(64, sizeof(unsigned long));
drawables.count = 0;
hazards.idArray = calloc(64, sizeof(unsigned long));
hazards.count = 0;
components.position = hashCreateTable();
components.player = hashCreateTable();
components.drawV = hashCreateTable();
components.collision = hashCreateTable();
}
void registerEntity(struct Registry* registry, unsigned long id) {
size_t index = registry->count;
if (index == sizeof(registry->idArray)) {
unsigned long* oldArray = registry->idArray;
size_t newSize = sizeof(oldArray) * 2;
unsigned long* newArray = calloc(newSize, sizeof(unsigned long));
for (size_t i = 0; i < sizeof(registry->idArray); i++) {
newArray[i] = oldArray[i];
}
free(oldArray);
registry->idArray = newArray;
}
registry->idArray[index] = id;
registry->count++;
}
bool registryContainsEntity(struct Registry* registry, unsigned long id) {
for(size_t i = 0; i < sizeof(registry->idArray); i++) {
if (registry->idArray[i] == id) {
return true;
}
}
return false;
}