summaryrefslogtreecommitdiff
path: root/platformer/src/player.c
diff options
context:
space:
mode:
Diffstat (limited to 'platformer/src/player.c')
-rw-r--r--platformer/src/player.c71
1 files changed, 55 insertions, 16 deletions
diff --git a/platformer/src/player.c b/platformer/src/player.c
index 737a4c9..9f0e822 100644
--- a/platformer/src/player.c
+++ b/platformer/src/player.c
@@ -15,7 +15,7 @@
* - player_init / player_reset: body.pos = spawn, vel = 0, on_ground = true,
* body.half_width = PLAYER_HALF_WIDTH, body.height = PLAYER_HEIGHT,
* facing_yaw = 0, forward_speed = 0, state = PLAYER_IDLE,
- * heightened_movement = false, heightened_timer = 0,
+ * heightened_movement = false, heightened_timer = 0, jump_buffer = 0,
* retreating_movement = false, jump_kind = JUMP_NONE, skid_time = 0,
* skid_speed = 0, skid_yaw = 0, wall_time = 0, wall_jump_yaw = 0,
* has_last_wall = false, body.gravity_scale = 1, body.hit_wall = false,
@@ -74,7 +74,15 @@
* WHY: at full run a U-turn used to keep all its speed; the skid
* makes reversing a committed, weighty slide.
*
- * C. Jump (from IDLE / RUN / SKID): if in->jump_pressed and body.on_ground:
+ * C. Jump. An A press sets jump_buffer = PLAYER_JUMP_BUFFER; it counts down
+ * every tick. While it's > 0, the first moment a jump is possible, it
+ * happens and the buffer is spent (take_jump). On the ground that's
+ * the press itself; pressed just before landing, it's the landing tick
+ * (so an early press still gets the second jump); pressed just before
+ * grabbing a wall, it's the grab (an early wall jump). WHY: players
+ * press a hair early while still falling and felt their press eaten.
+ * Holding A still decides hop vs. full jump.
+ * Jumps start from IDLE / RUN / SKID on the ground:
* on_ground = false; state = JUMP, and then one of
* - in SKID: the RETREAT jump, like SM64's side flip. The player
* already faces the stick (or where it last pointed), forward_speed =
@@ -87,7 +95,8 @@
* jump_kind = JUMP_SECOND. heightened_movement stays on for the
* whole jump, however long it lasts.
* - otherwise a normal jump: body.vel.y = PLAYER_JUMP_VELOCITY;
- * jump_kind = JUMP_NORMAL.
+ * jump_kind = JUMP_NORMAL. heightened_movement stays OFF, so a
+ * first jump can't grab walls (tried; it made wall hangs too easy).
*
* C2. Landing: landing from a normal jump (JUMP_NORMAL, short hop or
* full) or a retreat jump (JUMP_RETREAT) turns heightened_movement on
@@ -135,13 +144,17 @@
* (walked off a platform edge) -> FALL.
*
* F. Wall hang (WALL_HANG). From JUMP or FALL, enter it when ALL hold:
- * heightened_movement is on; the last physics step hit a wall
+ * heightened_movement is on (second, retreat or wall jump); the last physics step hit a wall
* (body.hit_wall) whose box allows_wall_hang; that wall's SURFACE isn't
* the one last jumped off (same facing and same face position, so a
* cliff built from side-by-side boxes counts as one wall; cleared on
* landing); and the
* player was moving into it within PLAYER_WALL_HANG_ANGLE_DEG of head-on
- * (facing_yaw vs. -wall_normal). On entry: wall_jump_yaw = the approach
+ * (facing_yaw vs. -wall_normal); and the player WOULDN'T CLEAR the
+ * wall's top anyway (their jump's apex, pos.y + vel.y^2 / 2g, is below
+ * it). WHY that last rule: hopping onto a ledge from just below its
+ * lip with a heightened jump would otherwise grab the face instead of
+ * landing on top. On entry: wall_jump_yaw = the approach
* direction MIRRORED off the wall (the part along the wall is kept, the
* part into it flips: 5 degrees in from the left leaves 5 degrees to the
* right); facing_yaw = wall_jump_yaw, so the nose shows exactly where a
@@ -154,7 +167,7 @@
* - The body presses into the wall at a tiny speed (WALL_PRESS_SPEED)
* so physics keeps reporting it; no contact means the player slid
* past the wall's bottom or end.
- * - A: the wall jump, along wall_jump_yaw. Within
+ * - A (take_jump): the wall jump, along wall_jump_yaw. Within
* PLAYER_WALL_JUMP_EARLY_TIME of grabbing it's a second jump
* (PLAYER_SECOND_JUMP_VELOCITY, launched at PLAYER_SECOND_JUMP_MAX_SPEED,
* JUMP_SECOND); later it's a normal jump (PLAYER_JUMP_VELOCITY,
@@ -181,7 +194,7 @@
* No magic numbers; everything comes from config.h.
*
* NOT IN SCOPE: Triple jump, long jump, wall kick, ground pound, skids
- * below full run speed, slopes, coyote time, jump buffering.
+ * below full run speed, slopes, coyote time.
* Leave a short comment at the state enum pointing to the
* README roadmap as the place to grow.
*
@@ -289,10 +302,21 @@ static void leave_ground(Player *p, float vel_y, JumpKind kind)
p->state = PLAYER_JUMP;
}
+/* C. Is there an A press waiting (pressed within PLAYER_JUMP_BUFFER)? If so,
+ * it's used up now: call this only where a jump can actually start. */
+static bool take_jump(Player *p)
+{
+ if (p->jump_buffer <= 0.0f)
+ return false;
+ p->jump_buffer = 0.0f;
+ return true;
+}
+
/* C. A jump from IDLE/RUN. While heightened_movement is on (just after
* landing a normal jump), A gives the higher second jump instead, like
* SM64's double jump. heightened_movement stays on through the whole
- * second jump. */
+ * second jump. A plain first jump does NOT turn it on, so it can't grab
+ * walls: wall hangs off a single jump made them too easy. */
static void start_jump(Player *p)
{
if (p->heightened_movement)
@@ -389,7 +413,7 @@ static void update_ground(Player *p, const Input *in, Wish wish, float dt)
}
p->state = grounded_state(p);
- if (in->jump_pressed)
+ if (take_jump(p))
start_jump(p);
}
@@ -414,7 +438,7 @@ static void update_skid(Player *p, const Input *in, Wish wish, float dt)
if (wish.mag > 0.0f)
p->facing_yaw = wish.yaw;
- if (in->jump_pressed) {
+ if (take_jump(p)) {
start_retreat_jump(p);
return;
}
@@ -456,17 +480,28 @@ static bool same_wall_as_last(const Player *p)
fabsf(wall_plane(b->wall, b->wall_normal) - p->last_wall_plane) < 0.01f;
}
-/* F. Can the player grab the wall they just hit? Only in a heightened move,
- * only on walls the level marks as hangable, not the wall they just jumped
- * off, and only when running into it close to head-on (a glancing hit
- * slides along it as usual). */
+/* F. Will this jump carry the feet above the wall's top anyway? Then the
+ * player is hopping ONTO it (a ledge just above), not clinging to it. */
+static bool would_clear_top(const Player *p)
+{
+ const Body *b = &p->body;
+ float apex = b->pos.y;
+ if (b->vel.y > 0.0f)
+ apex += b->vel.y * b->vel.y / (2.0f * GRAVITY);
+ return apex > b->wall.max.y;
+}
+
+/* F. Can the player grab the wall they just hit? Only in a heightened move
+ * (second, retreat or wall jump), only on walls the level marks as hangable, not the wall they
+ * just jumped off, not one they're about to clear, and only when running
+ * into it close to head-on (a glancing hit slides along it as usual). */
static bool wants_wall_hang(const Player *p)
{
const Body *b = &p->body;
if (!p->heightened_movement || !b->hit_wall || !b->wall.allows_wall_hang ||
p->forward_speed <= 0.0f)
return false;
- if (same_wall_as_last(p))
+ if (same_wall_as_last(p) || would_clear_top(p))
return false;
float head_on_yaw = atan2f(-b->wall_normal.x, -b->wall_normal.z); /* straight into the wall */
@@ -535,7 +570,7 @@ static void update_wall_hang(Player *p, const Input *in, Wish wish, float dt)
return;
}
- if (in->jump_pressed) {
+ if (take_jump(p)) {
start_wall_jump(p);
return;
}
@@ -646,6 +681,7 @@ void player_init(Player *p, Vec3 spawn)
p->skid_time = 0.0f;
p->skid_speed = 0.0f;
p->skid_yaw = 0.0f;
+ p->jump_buffer = 0.0f;
p->wall_time = 0.0f;
p->wall_jump_yaw = 0.0f;
p->has_last_wall = false;
@@ -667,6 +703,8 @@ void player_update(Player *p, const Input *in, float camera_yaw, float dt)
{
Wish wish = compute_wish(in, camera_yaw);
count_down_heightened(p, dt);
+ if (in->jump_pressed)
+ p->jump_buffer = PLAYER_JUMP_BUFFER; /* C: remembered until a jump can happen */
/* IDLE and RUN share one helper: they differ only in speed. */
switch (p->state) {
@@ -677,6 +715,7 @@ void player_update(Player *p, const Input *in, float camera_yaw, float dt)
case PLAYER_JUMP: update_jump(p, in, wish, dt); break;
case PLAYER_FALL: update_fall(p, in, wish, dt); break;
}
+ p->jump_buffer = fmaxf(p->jump_buffer - dt, 0.0f);
/* E. Turn speed into a velocity, then let physics move us. The player
* moves where they face, except in a skid (they've already turned to