-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.c
217 lines (163 loc) · 5.28 KB
/
entity.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// Created by Natty on 03.08.20.
//
#include "entity.h"
#include "projectile.h"
struct Entity {
entity_id id;
EntityClass_t entity_class;
float x;
float y;
float rotation;
float health;
float max_health;
float size;
entity_tick_function tick;
entity_render_function renderer;
entity_data data;
};
#define MAX_ENTITIES ((entity_id) 256)
struct EntityManager {
Entity_t* entities[MAX_ENTITIES];
};
EntityManager_t* en_create_manager() {
EntityManager_t* manager = (EntityManager_t*) calloc(1, sizeof(EntityManager_t));
if (manager == NULL) {
fprintf(stderr, "Failed to allocate memory for an entity manager!\n");
exit(EXIT_FAILURE);
}
return manager;
}
void en_tick(EntityManager_t* manager, struct ProjectileManager* projectile_manager, Player_t* player, float delta_time, MTRand* rand, ParticleManager_t* particle_manager) {
for (entity_id i = 0; i < MAX_ENTITIES; i++) {
Entity_t* entity = manager->entities[i];
if (entity != NULL)
entity->tick(player, manager, projectile_manager, entity, delta_time, rand, particle_manager);
}
}
void en_render(EntityManager_t* manager, SDL_Renderer* renderer, float cam_x, float cam_y) {
for (entity_id i = 0; i < MAX_ENTITIES; i++) {
Entity_t* entity = manager->entities[i];
if (entity != NULL)
entity->renderer(renderer, entity, cam_x, cam_y);
}
}
void en_free_manager(EntityManager_t* manager) {
if (manager == NULL)
return;
for (entity_id i = 0; i < MAX_ENTITIES; i++) {
Entity_t* entity = manager->entities[i];
if (entity != NULL)
en_destroy_entity(manager, i);
}
free(manager);
}
Entity_t* en_create_entity(EntityManager_t* manager, float x, float y, const EntityTemplate_t* prototype) {
/**
* This isn't particularly efficient, but whatever.
* */
for (entity_id i = 0; i < MAX_ENTITIES; i++) {
Entity_t* entity = manager->entities[i];
if (entity == NULL) {
Entity_t* e = (Entity_t*) calloc(1, sizeof(Entity_t));
if (e == NULL) {
fprintf(stderr, "Failed to allocate memory for an entity!");
return NULL;
}
e->id = i;
e->x = x;
e->y = y;
e->renderer = prototype->renderer;
e->tick = prototype->tick_func;
e->max_health = prototype->max_health;
e->size = prototype->size;
e->health = e->max_health;
e->rotation = 0;
e->entity_class = prototype->entity_class;
e->data = calloc(1, prototype->entity_data_size);
if (e->data == NULL) {
fprintf(stderr, "Failed to allocate memory for an entity data!");
free(e);
return NULL;
}
if (prototype->initializer != NULL)
prototype->initializer(e);
manager->entities[i] = e;
return e;
}
}
return NULL;
}
bool en_destroy_entity(EntityManager_t* manager, entity_id id) {
Entity_t* entity = manager->entities[id];
if (entity == NULL)
return false;
free(entity->data);
free(entity);
manager->entities[id] = NULL;
return true;
}
Entity_t* en_find_entity(EntityManager_t* manager, float x, float y, float search_range, EntityClass_t entity_type_filter, bool nearest) {
/**
* TODO: Optimize this
* */
Entity_t* nearest_entity = NULL;
float nearest_dist = INFINITY;
for (entity_id i = 0; i < MAX_ENTITIES; i++) {
Entity_t* entity = manager->entities[i];
if (entity != NULL) {
if (entity->entity_class & entity_type_filter) {
float dist = en_dist(entity, x, y) - entity->size / 2;
if (dist < search_range) {
if (nearest) {
if (nearest_dist > dist) {
nearest_dist = dist;
nearest_entity = entity;
}
} else {
return entity;
}
}
}
}
}
return nearest_entity;
}
float en_get_health(const Entity_t* entity) {
return entity->health;
}
float en_dist(const Entity_t* entity, float x, float y) {
return hypotf(entity->x - x, entity->y - y);
}
float en_get_x(const Entity_t* entity) {
return entity->x;
}
float en_get_y(const Entity_t* entity) {
return entity->y;
}
entity_id en_get_id(const Entity_t* entity) {
return entity->id;
}
void en_set_x(Entity_t* entity, float x) {
entity->x = x;
}
void en_set_y(Entity_t* entity, float y) {
entity->y = y;
}
float en_get_rotation(const Entity_t* entity) {
return entity->rotation;
}
void en_set_rotation(Entity_t* entity, float rot) {
entity->rotation = fmodf(fmodf(rot, 2 * PI) + 2 * PI, 2 * PI);
}
void* en_get_data(Entity_t* entity) {
return entity->data;
}
float en_get_max_health(const Entity_t* entity) {
return entity->max_health;
}
void en_apply_damage(Entity_t* entity, float damage) {
entity->health = fminf(entity->health - damage, entity->max_health);
}
void en_tick_do_nothing(Player_t* player, EntityManager_t* manager, Entity_t* entity, float delta_time) {
}