This repository has been archived on 2022-01-13. You can view files and clone it, but cannot push or open issues or pull requests.
jtmm2-old/src/collide.c

86 lines
2.2 KiB
C
Raw Normal View History

#include <gint/defs/types.h>
#include <stdbool.h>
#include "collide.h"
#include "conf.h"
#include "level.h"
2020-12-21 12:18:55 +01:00
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 ||
2020-12-21 12:18:55 +01:00
cursor.x >= level->width || cursor.y >= level->height) {
return 0; /* the point is out of bounds */
}
2020-12-21 12:18:55 +01:00
else {
return level->layers[layer_id][cursor.x + cursor.y * level->width];
}
}
2020-12-21 13:35:28 +01:00
/* TODO: 'collide{right,left,up,down} are a bad idea, should replace all
* of these for by an unique function i.e. 'player_collide'.
*/
2020-12-21 12:18:55 +01:00
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;
2020-12-21 12:18:55 +01:00
if (collide_point(pos, level, layer_id)) {
return true;
}
pos.y += player->hbox.y;
2020-12-21 12:18:55 +01:00
if (collide_point(pos, level, layer_id)) {
return true;
}
return false;
}
2020-12-21 12:18:55 +01:00
bool collide_left(Player *player, Vec position, const Level *level, uint layer_id) {
Vec pos;
vec_cpy(&pos, position);
vec_sub(&pos, player->origin);
2020-12-21 12:18:55 +01:00
if (collide_point(pos, level, layer_id)) {
return true;
}
pos.y += player->hbox.y;
2020-12-21 12:18:55 +01:00
if (collide_point(pos, level, layer_id)) {
return true;
}
return false;
}
2020-12-21 12:18:55 +01:00
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;
2020-12-21 12:18:55 +01:00
if (collide_point(pos, level, layer_id)) {
return true;
}
pos.x += player->hbox.x;
2020-12-21 12:18:55 +01:00
if (collide_point(pos, level, layer_id)) {
return true;
}
return false;
}
2020-12-21 12:18:55 +01:00
bool collide_up(Player *player, Vec position, const Level *level, uint layer_id) {
Vec pos;
vec_cpy(&pos, position);
vec_sub(&pos, player->origin);
2020-12-21 12:18:55 +01:00
if (collide_point(pos, level, layer_id)) {
return true;
}
pos.x += player->hbox.x;
2020-12-21 12:18:55 +01:00
if (collide_point(pos, level, layer_id)) {
return true;
}
return false;
}
2020-12-21 12:18:55 +01:00
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);
}