summaryrefslogtreecommitdiff
path: root/src/entity.c
diff options
context:
space:
mode:
authorBoredGuy <osome3717@gmail.com>2026-03-16 17:47:57 +0300
committerBoredGuy <osome3717@gmail.com>2026-03-16 17:47:57 +0300
commitac02199694ffef81a00a8ea47420e88ef0a35f67 (patch)
treeab8be37dd6960954d7baa54bb1273980063c66ba /src/entity.c
parent2c2be83a370318ca69824538ed3e61784295a772 (diff)
More work on systems
- Will be done soon (I think) - Added first entity
Diffstat (limited to 'src/entity.c')
-rw-r--r--src/entity.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/entity.c b/src/entity.c
index 7a9db49..4a7d98f 100644
--- a/src/entity.c
+++ b/src/entity.c
@@ -3,6 +3,8 @@
#include <string.h>
#include "game.h"
#include "entity.h"
+//Entities
+#include "player.h"
extern struct game game;
@@ -15,6 +17,15 @@ Rectangle get_entity_collider_world(const struct entity* entity) {
};
}
+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*,
@@ -23,17 +34,72 @@ void entity_handle_collision
) {
}
+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(const struct entity* entity) {
+ if (entity_animated(entity)) {
+ const struct animation* current_animation =
+ &entity->animations[entity->current_animation];
+ DrawTexturePro(entity->texture,
+ animation_get_source_rect(current_animation),
+ get_entity_dest_rect_world(entity),
+ (Vector2) {0.0, 0.0},
+ 0,
+ WHITE);
+ } else {
+ DrawTexturePro(entity->texture,
+ entity->sprite_source_rect,
+ get_entity_dest_rect_world(entity),
+ (Vector2) {0.0, 0.0},
+ 0,
+ WHITE);
+ }
}
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) {
+ update_player(entity, dt);
+}