wall jump

This commit is contained in:
KikooDX 2021-11-11 00:34:14 +01:00
parent 91ad5ce1f5
commit cbf55c5d38
3 changed files with 35 additions and 11 deletions

View File

@ -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

View File

@ -5,5 +5,5 @@ enum Tile {
TILE_SOLID,
TILE_PLAYER,
TILE_SHATTERED,
TILE_OOB = TILE_SOLID
TILE_OOB = TILE_VOID
};

View File

@ -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);
}
}