lock direction after walljump

This commit is contained in:
KikooDX 2021-11-11 01:16:01 +01:00
parent cbf55c5d38
commit a51212acc7
4 changed files with 20 additions and 4 deletions

View File

@ -7,6 +7,7 @@ struct Player {
struct VecF rem;
int jump_grace;
int jump_buffer;
int lock_direction;
};
void player_init(struct Vec pos);

View File

@ -2,3 +2,4 @@
int sign(int);
float signf(float);
int abs(int);

View File

@ -22,14 +22,17 @@ player_init(struct Vec pos)
self.rem = VECFZ;
self.jump_grace = 0;
self.jump_buffer = 0;
self.lock_direction = 0;
}
void
player_update(void)
{
const int on_ground = collide_solid(self.pos.x, self.pos.y + 1);
const struct Vec dir = VEC(input_down(K_RIGHT) - input_down(K_LEFT),
input_down(K_DOWN) - input_down(K_UP));
struct Vec dir = VEC(input_down(K_RIGHT) - input_down(K_LEFT),
input_down(K_DOWN) - input_down(K_UP));
if (self.lock_direction && dir.x != self.lock_direction)
dir.x = 0;
/* acceleration & friction */
if (on_ground && !dir.x) {
@ -56,6 +59,10 @@ player_update(void)
if (!jump(on_ground))
walljump();
/* unlock direction */
if (self.lock_direction && abs(self.spd.x) < MAX_WALK_SPEED / 2)
self.lock_direction = 0;
player_move(player_update_rem());
}
@ -92,6 +99,7 @@ walljump(void)
self.jump_buffer = 0;
self.spd.y = JUMP_SPD;
self.spd.x = wall_adjacent * MAX_WALK_SPEED;
self.lock_direction = wall_adjacent;
shatter_pos.x +=
(wall_adjacent == 1) ? (-2) : (PLAYER_WIDTH + 1);
shatter(shatter_pos.x, shatter_pos.y);
@ -125,8 +133,8 @@ player_update_rem(void)
void
player_move(struct Vec spd)
{
float sign_x = signf(spd.x);
const float sign_y = signf(spd.y);
float sign_x = signf(self.spd.x);
const float sign_y = signf(self.spd.y);
if (!sign_x && !sign_y)
sign_x = 1.0f;

View File

@ -11,3 +11,9 @@ signf(float n)
{
return (n > 0.0) - (n < 0.0);
}
int
abs(int n)
{
return (n > 0) ? (n) : (-n);
}