-
Notifications
You must be signed in to change notification settings - Fork 1
/
Context.c
147 lines (114 loc) · 3.95 KB
/
Context.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
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
#include <stdlib.h>
#include "Context.h"
#include "stdio.h"
#include "string.h"
void initContext(Context *_data) {
EntityData* data = &_data->entityData;
for(uint32_t i = 0; i < MAX_ENTITIES; ++i){
data->id[i] = FREE_ID;
}
}
EntityID cloneEntity(Context* _data, EntityID _entityId){
EntityData* data = &_data->entityData;
const int32_t size = findFirstFreeEntity(_data);
data->id[size] = size + 1;
if(data->id[size] > data->highestId){
data->highestId = data->id[size];
}
Position newPosition;
newPosition.x = data->positions[_entityId].x;
newPosition.y = data->positions[_entityId].y;
data->positions[size] = newPosition;
Dimension dimension;
dimension.width = data->dimensions[_entityId].width;
dimension.height =data->dimensions[_entityId].height;
data->dimensions[size] = dimension;
Rotation rotation;
rotation.angle = data->rotation[_entityId].angle;
data->rotation[size] = rotation;
Collider collider;
collider.type = data->collider[_entityId].type;
data->collider[size] = collider;
InputListener input;
input.listens = data->inputListeners[_entityId].listens;
data->inputListeners[size] = input;
data->sprites[size] = data->sprites[_entityId];
return size;
}
EntityID addEntity(Context* _data, int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, float _angle, Sprite* _sprite, CollisionType _collision) {
EntityData* data = &_data->entityData;
const int32_t size = findFirstFreeEntity(_data);
if(size == NO_FREE_ID_FOUND){
printf("Failed to add new entity, exceeded limit of %d!", MAX_ENTITIES);
return NO_FREE_ID_FOUND;
}
data->id[size] = size + 1;
if(data->id[size] > data->highestId){
data->highestId = data->id[size];
}
Position newPosition;
newPosition.x = _x;
newPosition.y = _y;
data->positions[size] = newPosition;
Dimension dimension;
dimension.width = _width;
dimension.height =_height;
data->dimensions[size] = dimension;
Rotation rotation;
rotation.angle = _angle;
data->rotation[size] = rotation;
Collider collider;
collider.type = _collision;
data->collider[size] = collider;
InputListener input;
input.listens = false;
data->inputListeners[size] = input;
if(_sprite != NULL){
data->sprites[size] = _sprite;
}
return size;
}
void setPosition(Context* _data, EntityID _entityId, int32_t _x, int32_t _y) {
EntityData* data = &_data->entityData;
data->positions[_entityId].x = _x;
data->positions[_entityId].y = _y;
}
void addVelocityComponent(Context* _data, float _vx, float _vy, EntityID _entityId){
EntityData* data = &_data->entityData;
Velocity* component = (Velocity*)malloc(sizeof(Velocity));
component->vy = _vy;
component->vx = _vx;
data->velocity[_entityId] = component;
}
void addInputListener(Context* _data, bool listens, EntityID _entityId){
EntityData* data = &_data->entityData;
InputListener listener;
listener.listens = listens;
data->inputListeners[_entityId] = listener;
}
void removeEntity(Context* _data, EntityID _id) {
EntityData* data = &_data->entityData;
data->id[_id - 1] = FREE_ID;
free(data->sprites[_id - 1]);
data->sprites[_id - 1] = NULL;
free(data->velocity[_id - 1]);
data->velocity[_id - 1] = NULL;
}
void showEntityAtPosition(Context *_data, EntityID _index) {
printf("Entity #%d, has position %f,%f and dimensions %d, %d\n",
_index,
_data->entityData.positions[_index].x,
_data->entityData.positions[_index].y,
_data->entityData.dimensions[_index].width,
_data->entityData.dimensions[_index].height
);
}
int32_t findFirstFreeEntity(Context *_data) {
EntityData* data = &_data->entityData;
for(uint32_t i = 0; i < MAX_ENTITIES; ++i){
if(data->id[i] == FREE_ID){
return i;
}
}
return NO_FREE_ID_FOUND;
}