summaryrefslogtreecommitdiff
path: root/src/entity.c
blob: 4a7d98f327f9fad4e9333c50e3966c8fad6ebf45 (plain)
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
#include <stdio.h>
#include <raylib.h>
#include <string.h>
#include "game.h"
#include "entity.h"
//Entities
#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*,
 struct entity*,
 enum direction
 ) {
}

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);
}