diff --git a/include/entity.h b/include/entity.h index f4b5810..a8a8919 100644 --- a/include/entity.h +++ b/include/entity.h @@ -8,6 +8,7 @@ struct Entity { int hb_y; int hb_w; int hb_h; + int layer; color_t hb_color; }; @@ -15,8 +16,8 @@ struct Entity { extern struct Entity *g_entity_grid[ENTITY_GRID_SIZE]; void entity_init(void *restrict entity, int x, int y, int hb_x, int hb_y, - int hb_w, int hb_h, color_t hb_color); + int hb_w, int hb_h, int layer, color_t hb_color); void entity_deinit(void *restrict entity); void entity_draw_hitbox(void *restrict entity); void entity_grid_draw_hitboxes(void); -int entity_collide(void *restrict entity); +struct Entity *entity_collide(void *restrict entity, int mask); diff --git a/include/layers.h b/include/layers.h new file mode 100644 index 0000000..d3621f2 --- /dev/null +++ b/include/layers.h @@ -0,0 +1,9 @@ +#pragma once + +enum Layers { + L_NONE = 0, + L_SOLID = 1 << 1, + L_SEMISOLID = 1 << 2, + L_HARMFULL = 1 << 3, + L_PUSHABLE = 1 << 4, +}; diff --git a/src/entity/collide.c b/src/entity/collide.c index 5111879..eacc281 100644 --- a/src/entity/collide.c +++ b/src/entity/collide.c @@ -1,32 +1,34 @@ #include "entity.h" -static int entity_collide_with(struct Entity *restrict, - struct Entity *restrict); +static int entity_collide_with(struct Entity *restrict, struct Entity *restrict, + int mask); -int -entity_collide(void *restrict entity) +struct Entity * +entity_collide(void *restrict entity, int mask) { struct Entity *const e = entity; int i; i = ENTITY_GRID_SIZE; while (i-- > 0) - if (entity_collide_with(e, g_entity_grid[i])) - return 1; + if (entity_collide_with(e, g_entity_grid[i], mask)) + return g_entity_grid[i]; - return 0; + return NULL; } /* axis aligned bounding box collision checking */ static int -entity_collide_with(struct Entity *restrict self, struct Entity *restrict other) +entity_collide_with(struct Entity *restrict self, struct Entity *restrict other, + int mask) { const int sx = self->x + self->hb_x; const int sy = self->y + self->hb_y; const int ox = other->x + other->hb_x; const int oy = other->y + other->hb_y; - if (self == NULL || other == NULL || self == other) + if (self == NULL || other == NULL || self == other || + !(mask & other->layer)) return 0; return sx < ox + other->hb_w && sx + self->hb_w > ox && diff --git a/src/entity/init.c b/src/entity/init.c index d29facd..544c6fa 100644 --- a/src/entity/init.c +++ b/src/entity/init.c @@ -1,11 +1,12 @@ #include "entity.h" +#include "layers.h" #include struct Entity *g_entity_grid[ENTITY_GRID_SIZE]; void entity_init(void *restrict entity, int x, int y, int hb_x, int hb_y, int hb_w, - int hb_h, color_t hb_color) + int hb_h, int layer, color_t hb_color) { static int init_grid = 1; struct Entity *const e = entity; @@ -16,6 +17,7 @@ entity_init(void *restrict entity, int x, int y, int hb_x, int hb_y, int hb_w, e->hb_y = hb_y; e->hb_w = hb_w; e->hb_h = hb_h; + e->layer = layer; e->hb_color = hb_color; if (init_grid) { diff --git a/src/main.c b/src/main.c index c875535..f3757cc 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include "entity.h" +#include "layers.h" #include "player.h" #include "wall.h" #include @@ -8,9 +9,11 @@ void main(void) { struct Player player; + struct Player newb; struct Wall walls[5]; player_init(&player, 16, 32); + player_init(&newb, 64, 128); wall_init(&walls[0], 16, 0, DWIDTH - 32, 16); wall_init(&walls[1], 16, DHEIGHT - 16, DWIDTH - 32, 16); wall_init(&walls[2], 0, 0, 16, DHEIGHT); @@ -26,7 +29,7 @@ main(void) clearevents(); player.x += keydown(KEY_RIGHT) - keydown(KEY_LEFT); player.y += keydown(KEY_DOWN) - keydown(KEY_UP); - if (entity_collide(&player)) { + if (entity_collide(&player, L_SOLID) != NULL) { player.x = previous_x; player.y = previous_y; } diff --git a/src/player/init.c b/src/player/init.c index f250f97..ec1143c 100644 --- a/src/player/init.c +++ b/src/player/init.c @@ -1,10 +1,11 @@ +#include "layers.h" #include "player.h" #include void player_init(struct Player *restrict p, int x, int y) { - entity_init(p, x, y, 2, 2, 12, 12, C_BLUE); + entity_init(p, x, y, 2, 2, 12, 12, L_PUSHABLE, C_BLUE); p->spd_x = 0.0; p->spd_y = 0.0; p->rem_x = 0.0; diff --git a/src/wall/init.c b/src/wall/init.c index 8b3fc2c..2919467 100644 --- a/src/wall/init.c +++ b/src/wall/init.c @@ -1,8 +1,9 @@ +#include "layers.h" #include "wall.h" #include void wall_init(struct Wall *restrict w, int x, int y, int width, int height) { - entity_init(w, x, y, 0, 0, width, height, C_DARK); + entity_init(w, x, y, 0, 0, width, height, L_SOLID, C_DARK); }