diff options
Diffstat (limited to 'src/entity.c')
| -rw-r--r-- | src/entity.c | 66 |
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); +} |
