summaryrefslogtreecommitdiff
path: root/src/entity.h
blob: 94eb00fb6154bbd333f6375e43cb9cca24b8a88b (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
#ifndef ENTITY_H_
#define ENTITY_H_

#include <raylib.h>
#include <stdbool.h>
#include <stdint.h>
#include "constants.h"

#define ENTITY_ACTIVE 1
#define ENTITY_COLLISION_ACTIVE (1 << 1)
#define ENTITY_VISIBLE (1 << 2)

enum entity_type {
  Player_Entity
};

enum draw_layer {
  Foreground_Layer,
  Background_Layer
};

enum direction {
  Direction_Up,
  Direction_Down,
  Direction_Left,
  Direction_Right
};

struct animation {
  int frame_count;
  bool is_looping;
  bool is_completed;

  Rectangle source_rects[ANIMATION_MAX_FRAMES];
  float frame_times[ANIMATION_MAX_FRAMES];

  int current_frame;
  float current_frame_time;
};

struct entity {
  int entity_id;
  enum entity_type type;
  uint32_t flags;

  Vector2 position; //The position of the entity(roughly the center)
  Vector2 velocity;
  Rectangle collider;

  Texture texture;
  Rectangle sprite_dest_rect;  //Relative to the position of entity (roughly the center)
  struct animation animations[ENTITY_MAX_ANIMATIONS];
  int current_animation;
  enum draw_layer draw_layer;
};

void update_entity(struct entity* entity, float dt);
void draw_entity(const struct entity* entity);

Rectangle get_entity_collider_world(const struct entity* entity);

void entity_handle_collision
(
 struct entity* self,
 struct entity* other,
 enum direction collision_direction
 );

void add_entity(const struct entity* e);

static inline bool entity_active(const struct entity* e) {
  return e->flags & ENTITY_ACTIVE;
}

static inline bool same_entity(const struct entity* a, const struct entity* b) {
  return a->entity_id == b->entity_id;
}

static inline bool entity_visible(const struct entity* e) {
  return e->flags & ENTITY_VISIBLE;
}

#endif