Clipping doesn't work.

This commit is contained in:
KikooDX 2021-01-02 17:52:22 +01:00
parent d4c1575ca9
commit 4818b21ec8
1 changed files with 27 additions and 9 deletions

View File

@ -17,7 +17,10 @@
#define JUMP_SPD (-128 * PXS)
#define GRACE_UNITS (int)(UPS / 5)
#define EARLY_UNITS (int)(UPS / 5)
#define SGN(x) ((x > 0) ? (1) : ((x < 0) ? (-1) : (0)))
#define H_CLIP_MARGIN (TILE_SIZE / 2)
#define V_CLIP_MARGIN (TILE_SIZE / 3)
#define SGN(x) (((x) > 0) ? (1) : (((x) < 0) ? (-1) : (0)))
#define ABS(x) (((x) > 0) ? (x) : (-(x)))
#define PLAYER_COLLIDE(pos) player_collide(player, pos, level, level->solid_layer)
void player_move(Player *player, const Level *level) {
@ -29,15 +32,28 @@ void player_move(Player *player, const Level *level) {
/* snap the player to the grid if they hit a wall */
destination.x += player->spd.x;
if (PLAYER_COLLIDE(destination)) {
destination.x = player->pos.x - player->pos.x % TILE_SIZE;
/* Move the player tile per tile until it enters an
* occuped tile. */
while (!PLAYER_COLLIDE(destination)) {
destination.x += TILE_SIZE * sgn_spd_x;
/* Used for clipping and positionning. */
int offset = destination.y % TILE_SIZE;
if (offset > TILE_SIZE / 2) {
offset -= TILE_SIZE;
}
if (ABS(offset) > H_CLIP_MARGIN) {
offset = 0;
}
destination.y -= offset;
if (PLAYER_COLLIDE(destination))
{
destination.y += offset;
destination.x = player->pos.x - player->pos.x % TILE_SIZE;
/* Move the player tile per tile until it enters an
* occupied tile. */
while (!PLAYER_COLLIDE(destination)) {
destination.x += TILE_SIZE * sgn_spd_x;
}
/* then, move it back one tile */
destination.x -= TILE_SIZE * sgn_spd_x;
player->spd.x = 0;
}
/* then, move it back one tile */
destination.x -= TILE_SIZE * sgn_spd_x;
player->spd.x = 0;
}
/* do the same for y */
destination.y += player->spd.y;
@ -71,6 +87,8 @@ void player_step(Player *player, Input *input, const Level *level, uint step) {
player->spd.x += xacc; /* apply horizontal acceleration */
/* apply gravity */
if (player->spd.y < 0 && !player->jump_held) {
/* The player is rising and let go the jump key,
* accelerate gravity until they reach 0. */
player->spd.y += GRAVITY * FAST_FALL_FACTOR;
} else {
player->spd.y += GRAVITY;