diff --git a/src/entity.c b/src/entity.c index fc45e2a..001b9e1 100644 --- a/src/entity.c +++ b/src/entity.c @@ -2,19 +2,48 @@ #include "game.h" #include "map.h" #include "cfg.h" +#include + +void +_points(Entity *this, int ox, int oy, int *x0, int *x1, int *y0, int *y1) +{ + *x0 = this->pos[0] - this->width / 2 + ox; + *y0 = this->pos[1] - this->width / 2 + oy; + *x1 = *x0 + this->width - 1; + *y1 = *y0 + this->height - 1; +} + bool entity_collide(Entity *this, Game *g, int ox, int oy) { (void)g; - const int x0 = this->pos[0] - this->width / 2 + ox; - const int y0 = this->pos[1] - this->height / 2 + oy; - const int x1 = x0 + this->width - 1; - const int y1 = y0 + this->height - 1; + int x0, y0, x1, y1; + _points(this, ox, oy, &x0, &x1, &y0, &y1); return (map_get_px(x0, y0) == 1 || map_get_px(x0, y1) == 1 || map_get_px(x1, y0) == 1 || map_get_px(x1, y1) == 1); } +bool +entity_meet(Entity *this, Entity *other) +{ + int tx0, ty0, tx1, ty1; + int ox0, oy0, ox1, oy1; + _points(this, 0, 0, &tx0, &tx1, &ty0, &ty1); + _points(other, 0, 0, &ox0, &ox1, &oy0, &oy1); + return (tx0 < ox1 && tx1 > ox0 && ty0 < oy1 && ty1 > oy0); +} + +Entity * +entity_place_meeting(Entity *this, Game *g, EntityType type) +{ + for (int i = 0; i < MAX_ENTITIES; i++) + if (this != &g->entities[i] && g->entities[i].type == type && + entity_meet(this, &g->entities[i])) + return &g->entities[i]; + return NULL; +} + void entity_move(Entity *this, Game *g) { diff --git a/src/entity.h b/src/entity.h index be8fc0b..9460273 100644 --- a/src/entity.h +++ b/src/entity.h @@ -31,4 +31,6 @@ struct Entity { }; bool entity_collide(Entity *this, struct Game *g, int ox, int oy); +bool entity_meet(Entity *this, Entity *other); +Entity *entity_place_meeting(Entity *this, struct Game *g, EntityType type); void entity_move(Entity *this, struct Game *g);