diff --git a/inc/player.h b/inc/player.h index 5b47114..78a6a8d 100644 --- a/inc/player.h +++ b/inc/player.h @@ -7,6 +7,7 @@ struct Player { struct VecF rem; int jump_grace; int jump_buffer; + int lock_direction; }; void player_init(struct Vec pos); diff --git a/inc/util.h b/inc/util.h index d80a265..4e47124 100644 --- a/inc/util.h +++ b/inc/util.h @@ -2,3 +2,4 @@ int sign(int); float signf(float); +int abs(int); diff --git a/src/player.c b/src/player.c index 22ec922..039c15e 100644 --- a/src/player.c +++ b/src/player.c @@ -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; diff --git a/src/util.c b/src/util.c index 2be5c77..11279d3 100644 --- a/src/util.c +++ b/src/util.c @@ -11,3 +11,9 @@ signf(float n) { return (n > 0.0) - (n < 0.0); } + +int +abs(int n) +{ + return (n > 0) ? (n) : (-n); +}