forked from kdx/jtmm2-old
86 lines
2.2 KiB
C
86 lines
2.2 KiB
C
#include <gint/defs/types.h>
|
|
#include <stdbool.h>
|
|
|
|
#include "collide.h"
|
|
#include "conf.h"
|
|
#include "level.h"
|
|
|
|
uint8_t collide_point(Vec point, const Level *level, uint layer_id) {
|
|
Vec cursor; /* the final position to test */
|
|
vec_cpy(&cursor, point);
|
|
vec_div(&cursor, TILE_SIZE); /* get the expected tile ID */
|
|
if (point.x < 0 || point.y < 0 ||
|
|
cursor.x >= level->width || cursor.y >= level->height) {
|
|
return 0; /* the point is out of bounds */
|
|
}
|
|
else {
|
|
return level->layers[layer_id][cursor.x + cursor.y * level->width];
|
|
}
|
|
}
|
|
|
|
/* TODO: 'collide{right,left,up,down} are a bad idea, should replace all
|
|
* of these for by an unique function i.e. 'player_collide'.
|
|
*/
|
|
bool collide_right(Player *player, Vec position, const Level *level, uint layer_id) {
|
|
Vec pos;
|
|
vec_cpy(&pos, position);
|
|
vec_sub(&pos, player->origin);
|
|
pos.x += player->hbox.x;
|
|
if (collide_point(pos, level, layer_id)) {
|
|
return true;
|
|
}
|
|
pos.y += player->hbox.y;
|
|
if (collide_point(pos, level, layer_id)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool collide_left(Player *player, Vec position, const Level *level, uint layer_id) {
|
|
Vec pos;
|
|
vec_cpy(&pos, position);
|
|
vec_sub(&pos, player->origin);
|
|
if (collide_point(pos, level, layer_id)) {
|
|
return true;
|
|
}
|
|
pos.y += player->hbox.y;
|
|
if (collide_point(pos, level, layer_id)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool collide_down(Player *player, Vec position, const Level *level, uint layer_id) {
|
|
Vec pos;
|
|
vec_cpy(&pos, position);
|
|
vec_sub(&pos, player->origin);
|
|
pos.y += player->hbox.y;
|
|
if (collide_point(pos, level, layer_id)) {
|
|
return true;
|
|
}
|
|
pos.x += player->hbox.x;
|
|
if (collide_point(pos, level, layer_id)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool collide_up(Player *player, Vec position, const Level *level, uint layer_id) {
|
|
Vec pos;
|
|
vec_cpy(&pos, position);
|
|
vec_sub(&pos, player->origin);
|
|
if (collide_point(pos, level, layer_id)) {
|
|
return true;
|
|
}
|
|
pos.x += player->hbox.x;
|
|
if (collide_point(pos, level, layer_id)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool collide_full(Player *player, Vec position, const Level *level, uint layer_id) {
|
|
return collide_up(player, position, level, layer_id) ||
|
|
collide_down(player, position, level, layer_id);
|
|
}
|