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
|
#include <stdio.h>
#include <raylib.h>
#include <string.h>
#include "game.h"
#include "entity.h"
//Entities
#include "wall.h"
#include "player.h"
extern struct game game;
Rectangle get_entity_collider_world(const struct entity* entity) {
return (Rectangle) {
.x = entity->collider.x + entity->position.x,
.y = entity->collider.y + entity->position.y,
.width = entity->collider.width,
.height = entity->collider.height
};
}
Rectangle get_entity_dest_rect_world(const struct entity* entity) {
return (Rectangle) {
.x = entity->sprite_dest_rect.x + entity->position.x,
.y = entity->sprite_dest_rect.y + entity->position.y,
.width = entity->sprite_dest_rect.width,
.height = entity->sprite_dest_rect.height
};
}
void entity_handle_collision
(
struct entity* self,
struct entity* other,
enum direction direction
) {
switch (self->type) {
case Player_Entity:
player_handle_collision(self, other, direction);
break;
default:
break;
}
}
static inline bool entity_animated(const struct entity* e) {
return (e->flags & ENTITY_ANIMATED) != 0;
}
void update_animation(struct animation* animation, float dt) {
if (animation->is_completed)
return;
animation->current_frame_time += dt;
while (animation->current_frame_time >= animation->frame_times[animation->current_frame]) {
bool animation_completed = animation->current_frame >= animation->frame_count-1;
if (animation_completed && animation->is_looping) {
animation->current_frame = 0;
animation->current_frame_time = 0;
return;
} else if (animation_completed) {
animation->is_completed = true;
return;
}
animation->current_frame_time -= animation->frame_times[animation->current_frame];
animation->current_frame++;
}
}
Rectangle animation_get_source_rect(const struct animation* animation) {
return animation->source_rects[animation->current_frame];
}
void draw_entity_default(const struct entity* entity) {
Rectangle source_rect;
if (entity_animated(entity)) {
const struct animation* current_animation =
&entity->animations[entity->current_animation];
source_rect = animation_get_source_rect(current_animation);
} else {
source_rect = entity->sprite_source_rect;
}
if (entity->flip) {
source_rect.width *= -1;
}
DrawTexturePro(entity->texture,
source_rect,
get_entity_dest_rect_world(entity),
(Vector2) {0.0, 0.0},
0,
WHITE);
}
void draw_entity(const struct entity* entity) {
/* if (entity->flags & ENTITY_COLLISION_ACTIVE) { */
/* DrawRectangleRec( */
/* get_entity_collider_world(entity), */
/* RED */
/* ); */
/* } */
switch (entity->type) {
default:
draw_entity_default(entity);
break;
}
}
void add_entity(const struct entity* e) {
static int entity_id = 0;
for (int i = 0; i < MAX_ENTITY_COUNT; i++) {
struct entity* current = &game.entities[i];
if (entity_active(current))
continue;
memcpy(current, e, sizeof(struct entity));
current->entity_id = entity_id++;
return;
}
}
void update_entity(struct entity* entity, float dt) {
switch (entity->type) {
case Player_Entity:
update_player(entity, dt);
break;
default:
break;
}
}
|