50 lines
1.5 KiB
C
50 lines
1.5 KiB
C
#include <gint/defs/types.h>
|
|
#include <stdbool.h>
|
|
|
|
#include "collide.h"
|
|
#include "conf.h"
|
|
#include "level.h"
|
|
#include "tiles.h"
|
|
|
|
Tile_flags 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 {
|
|
const Tile tile_id = level->layers[layer_id][cursor.x + cursor.y * level->width];
|
|
return tile_get_flags(tile_id);
|
|
}
|
|
}
|
|
|
|
Tile_flags player_collide_or(Player *player, Vec position, const Level *level, uint layer_id) {
|
|
Vec pos;
|
|
vec_cpy(&pos, position);
|
|
Tile_flags flags = 0;
|
|
flags |= collide_point(pos, level, layer_id);
|
|
pos.x += player->hbox.x;
|
|
flags |= collide_point(pos, level, layer_id);
|
|
pos.y += player->hbox.y;
|
|
flags |= collide_point(pos, level, layer_id);
|
|
pos.x -= player->hbox.x;
|
|
flags |= collide_point(pos, level, layer_id);
|
|
return flags;
|
|
}
|
|
|
|
Tile_flags player_collide_and(Player *player, Vec position, const Level *level, uint layer_id) {
|
|
Vec pos;
|
|
vec_cpy(&pos, position);
|
|
Tile_flags flags = -1;
|
|
flags &= collide_point(pos, level, layer_id);
|
|
pos.x += player->hbox.x;
|
|
flags &= collide_point(pos, level, layer_id);
|
|
pos.y += player->hbox.y;
|
|
flags &= collide_point(pos, level, layer_id);
|
|
pos.x -= player->hbox.x;
|
|
flags &= collide_point(pos, level, layer_id);
|
|
return flags;
|
|
}
|