From cbf55c5d380c99759b28cf55a9f5518df74ad3b9 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Thu, 11 Nov 2021 00:34:14 +0100 Subject: [PATCH] wall jump --- inc/conf.h | 2 +- inc/tile.h | 2 +- src/player.c | 42 +++++++++++++++++++++++++++++++++--------- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/inc/conf.h b/inc/conf.h index a635acc..ef98e52 100644 --- a/inc/conf.h +++ b/inc/conf.h @@ -12,6 +12,6 @@ #define FRICTION_AIR (ACCELERATION_AIR / MAX_WALK_SPEED) #define FRICTION_GROUND (ACCELERATION_GROUND / MAX_WALK_SPEED) #define FRICTION_BREAK (0.6f / MAX_WALK_SPEED) -#define JUMP_GRACE 4 +#define JUMP_GRACE 1 #define JUMP_BUFFER 12 #define JUMP_SPD -5.0 diff --git a/inc/tile.h b/inc/tile.h index 2a238ed..bf73b95 100644 --- a/inc/tile.h +++ b/inc/tile.h @@ -5,5 +5,5 @@ enum Tile { TILE_SOLID, TILE_PLAYER, TILE_SHATTERED, - TILE_OOB = TILE_SOLID + TILE_OOB = TILE_VOID }; diff --git a/src/player.c b/src/player.c index b5166a8..22ec922 100644 --- a/src/player.c +++ b/src/player.c @@ -10,7 +10,8 @@ static struct Player self; -static void jump(int on_ground); +static int jump(int on_ground); +static void walljump(void); static int collide_solid(int x, int y); void @@ -43,14 +44,22 @@ player_update(void) /* resistance & gravity */ self.spd.y *= 1.0f - AIR_RESISTANCE; - self.spd.y += GRAVITY; + if (!on_ground) + self.spd.y += GRAVITY; - jump(on_ground); + /* jump buffer */ + if (input_pressed(K_JUMP)) + self.jump_buffer = JUMP_BUFFER; + else if (self.jump_buffer) + self.jump_buffer--; + /* jump or walljump */ + if (!jump(on_ground)) + walljump(); player_move(player_update_rem()); } -static void +static int jump(int on_ground) { /* grace */ @@ -58,11 +67,6 @@ jump(int on_ground) self.jump_grace = JUMP_GRACE; else if (self.jump_grace) self.jump_grace--; - /* buffer */ - if (input_pressed(K_JUMP)) - self.jump_buffer = JUMP_BUFFER; - else if (self.jump_buffer) - self.jump_buffer--; /* jump */ if (self.jump_grace && self.jump_buffer && input_down(K_JUMP)) { struct Vec shatter_pos = VEC(self.pos.x, self.pos.y); @@ -73,6 +77,26 @@ jump(int on_ground) shatter(shatter_pos.x, shatter_pos.y); shatter_pos.x += PLAYER_WIDTH - 1; shatter(shatter_pos.x, shatter_pos.y); + return 1; + } + return 0; +} + +static void +walljump(void) +{ + const int wall_adjacent = collide_solid(self.pos.x - 2, self.pos.y) - + collide_solid(self.pos.x + 2, self.pos.y); + if (self.jump_buffer && wall_adjacent && input_down(K_JUMP)) { + struct Vec shatter_pos = VEC(self.pos.x, self.pos.y); + self.jump_buffer = 0; + self.spd.y = JUMP_SPD; + self.spd.x = wall_adjacent * MAX_WALK_SPEED; + shatter_pos.x += + (wall_adjacent == 1) ? (-2) : (PLAYER_WIDTH + 1); + shatter(shatter_pos.x, shatter_pos.y); + shatter_pos.y += PLAYER_HEIGHT - 1; + shatter(shatter_pos.x, shatter_pos.y); } }