From 4818b21ec8f39c15ce160055c2dd9a754c79f2bc Mon Sep 17 00:00:00 2001 From: KikooDX Date: Sat, 2 Jan 2021 17:52:22 +0100 Subject: [PATCH] Clipping doesn't work. --- src/player.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/player.c b/src/player.c index 8c8222f..2a3740c 100644 --- a/src/player.c +++ b/src/player.c @@ -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;