summaryrefslogtreecommitdiff
path: root/platformer/src/player.c
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2026-09-29 18:46:53 -0400
committerSchark <jordan@schark.online>2026-09-29 18:46:53 -0400
commit923b5928eeafd9bf969b95c4d49699d280b1ce0f (patch)
tree0210db826b4cbdfc91704dc6655652b96cbd36b4 /platformer/src/player.c
parent25992eb0baa1be5b49e8732e4b63734cdafe3b80 (diff)
downloadgamedev-923b5928eeafd9bf969b95c4d49699d280b1ce0f.tar.gz
gamedev-923b5928eeafd9bf969b95c4d49699d280b1ce0f.zip
Double jump foundation
Diffstat (limited to 'platformer/src/player.c')
-rw-r--r--platformer/src/player.c136
1 files changed, 118 insertions, 18 deletions
diff --git a/platformer/src/player.c b/platformer/src/player.c
index cbb2c1c..88df0c4 100644
--- a/platformer/src/player.c
+++ b/platformer/src/player.c
@@ -14,7 +14,8 @@
* IMPLEMENT:
* - 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.
+ * facing_yaw = 0, forward_speed = 0, state = PLAYER_IDLE,
+ * heightened_movement = false, heightened_timer = 0, jump_number = 0.
*
* - player_update(p, in, camera_yaw, dt). Use one static helper per state
* (update_idle / update_run / update_jump / update_fall), dispatched by a
@@ -43,18 +44,45 @@
* - State: forward_speed > 0 -> RUN, otherwise IDLE.
*
* C. Jump (from IDLE / RUN): if in->jump_pressed and body.on_ground:
- * body.vel.y = PLAYER_JUMP_VELOCITY; on_ground = false; state = JUMP.
+ * on_ground = false; state = JUMP, and then either
+ * - heightened_movement on: the SECOND jump, SM64's double jump.
+ * body.vel.y = PLAYER_SECOND_JUMP_VELOCITY (1.75x the height);
+ * jump_number = 2. heightened_movement stays on for the whole jump,
+ * however long it lasts. Until it lands, the top speed is
+ * PLAYER_SECOND_JUMP_MAX_SPEED instead of PLAYER_MAX_SPEED (see D).
+ * - otherwise a normal jump: body.vel.y = PLAYER_JUMP_VELOCITY;
+ * jump_number = 1.
+ *
+ * C2. heightened_movement: landing from a normal jump (jump_number == 1,
+ * short hop or full) turns it on with heightened_timer =
+ * PLAYER_SECOND_JUMP_WINDOW. Landing any other way (from the second
+ * jump, or after walking off an edge) turns it off. Every tick,
+ * unless the player is in the second jump, the timer counts down and
+ * it turns off at 0. WHY a named state rather than a private "can
+ * double jump" flag: later moves will key off it too.
*
* D. Air movement (JUMP / FALL): the same as B, but the turn rate is
* multiplied by PLAYER_AIR_CONTROL, accel is PLAYER_AIR_ACCEL, there's
* no instant turn from a standstill, and there's NO friction when the
* stick is released (you keep your momentum, like SM64).
- * - Variable jump height: in JUMP, if !in->jump_held and vel.y > 0,
+ * - Top speed: steering aims for top_speed() * mag, where top_speed()
+ * is PLAYER_SECOND_JUMP_MAX_SPEED while jump_number == 2 and
+ * PLAYER_MAX_SPEED otherwise. While forward_speed is ABOVE it (a
+ * full run into the second jump), the stick only turns: the extra
+ * speed bleeds off at PLAYER_SECOND_JUMP_SLOWDOWN, whatever the
+ * stick does. So a 6 m/s run into the second jump eases down to
+ * 5 m/s over ~0.3 s. WHY a separate, gentle rate: slowing at
+ * PLAYER_AIR_ACCEL (~0.25 s) felt like hitting a wall of syrup
+ * right at takeoff. 6 m/s only comes back (through normal ground
+ * accel) after landing. WHY no new state:
+ * jump_number == 2 already means "in the second jump".
+ * - Variable jump height (both jumps): in JUMP, if !in->jump_held and vel.y > 0,
* multiply vel.y by PLAYER_JUMP_CUT ONCE (don't repeat it every tick;
* either switch to FALL right away or keep a flag). Comment WHY: tapping A
* gives a hop, holding it gives a full jump.
* - JUMP -> FALL when vel.y <= 0.
- * - FALL -> IDLE/RUN when body.on_ground (landing).
+ * - FALL -> IDLE/RUN when body.on_ground (landing; apply C2 first, so
+ * an A press on the landing tick already gets the second jump).
* - If the player is on the ground state-wise but body.on_ground is false
* (walked off a platform edge) -> FALL.
*
@@ -68,7 +96,7 @@
* accelerations in m/s^2, and every one of them multiplies by dt.
* No magic numbers; everything comes from config.h.
*
- * NOT IN SCOPE: Double/triple jump, long jump, wall kick, ground pound,
+ * NOT IN SCOPE: Triple jump, long jump, wall kick, ground pound,
* skidding/turnaround, slopes, coyote time, jump buffering.
* Leave a short comment at the state enum pointing to the
* README roadmap as the place to grow.
@@ -77,8 +105,10 @@
* PLAYER_MAX_SPEED after a noticeable build-up. Turning is quick but
* not instant while moving, and instant for big turns from a standstill. Letting go
* of the stick skids the player to a short, smooth stop. A tap of A
- * gives a short hop and holding A gives a full ~2.4 m jump. The
- * player lands cleanly back in IDLE/RUN.
+ * gives a short hop and holding A gives a full ~2.4 m jump. Pressing
+ * A within 0.3 s of landing gives a second jump 1.75x as high, with
+ * heightened_movement on from that landing until the second jump
+ * lands. The player lands cleanly back in IDLE/RUN.
* ============================================================
*/
@@ -109,12 +139,25 @@ static Wish compute_wish(const Input *in, float camera_yaw)
return wish;
}
+/* The fastest the player may go right now. The second jump trades
+ * horizontal speed for its extra height, until it lands. */
+static float top_speed(const Player *p)
+{
+ return p->jump_number == 2 ? PLAYER_SECOND_JUMP_MAX_SPEED : PLAYER_MAX_SPEED;
+}
+
+/* Turns the facing toward the stick, at most turn_rate. */
+static void turn(Player *p, Wish wish, float turn_rate, float dt)
+{
+ p->facing_yaw = approach_angle(p->facing_yaw, wish.yaw, turn_rate * dt);
+}
+
/* Turns toward the stick and speeds up toward a speed set by how far it's
* pushed. Shared by ground and air movement; the air passes smaller rates. */
static void steer(Player *p, Wish wish, float turn_rate, float accel, float dt)
{
- p->facing_yaw = approach_angle(p->facing_yaw, wish.yaw, turn_rate * dt);
- p->forward_speed = approachf(p->forward_speed, PLAYER_MAX_SPEED * wish.mag, accel * dt);
+ turn(p, wish, turn_rate, dt);
+ p->forward_speed = approachf(p->forward_speed, top_speed(p) * wish.mag, accel * dt);
}
/* Standing still vs walking vs running is only a matter of speed. */
@@ -123,6 +166,47 @@ static PlayerState grounded_state(const Player *p)
return p->forward_speed > 0.0f ? PLAYER_RUN : PLAYER_IDLE;
}
+/* C. Leaving the ground. 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. */
+static void start_jump(Player *p)
+{
+ if (p->heightened_movement) {
+ p->body.vel.y = PLAYER_SECOND_JUMP_VELOCITY;
+ p->jump_number = 2;
+ } else {
+ p->body.vel.y = PLAYER_JUMP_VELOCITY;
+ p->jump_number = 1;
+ }
+ p->body.on_ground = false;
+ p->state = PLAYER_JUMP;
+}
+
+/* C2. Touching down. Landing a normal jump (a hop or a full one) opens the
+ * short window for the second jump; landing any other way closes it, so
+ * the second jump can't chain into another one. */
+static void land(Player *p)
+{
+ if (p->jump_number == 1) {
+ p->heightened_movement = true;
+ p->heightened_timer = PLAYER_SECOND_JUMP_WINDOW;
+ } else {
+ p->heightened_movement = false;
+ }
+ p->jump_number = 0;
+}
+
+/* C2. The window after landing runs out on its own. It doesn't during the
+ * second jump: heightened_movement lasts as long as that jump does. */
+static void count_down_heightened(Player *p, float dt)
+{
+ if (!p->heightened_movement || p->jump_number == 2)
+ return;
+ p->heightened_timer -= dt;
+ if (p->heightened_timer <= 0.0f)
+ p->heightened_movement = false;
+}
+
/* B + C. IDLE and RUN: full control, friction when the stick is released,
* and the only place a jump can start. */
static void update_ground(Player *p, const Input *in, Wish wish, float dt)
@@ -147,27 +231,38 @@ static void update_ground(Player *p, const Input *in, Wish wish, float dt)
}
p->state = grounded_state(p);
- if (in->jump_pressed) {
- p->body.vel.y = PLAYER_JUMP_VELOCITY;
- p->body.on_ground = false;
- p->state = PLAYER_JUMP;
- }
+ if (in->jump_pressed)
+ start_jump(p);
}
/* D. Air movement: weaker steering, and NO friction when the stick is
* released, so you keep your momentum like in SM64. */
static void air_steer(Player *p, Wish wish, float dt)
{
+ float air_turn_rate = PLAYER_TURN_RATE * PLAYER_AIR_CONTROL;
+
+ /* Faster than the top speed allows (a full run into the second jump):
+ * shed the extra slowly, whatever the stick says, so takeoff doesn't feel
+ * like hitting a wall. The stick can still turn. */
+ if (p->forward_speed > top_speed(p)) {
+ if (wish.mag > 0.0f)
+ turn(p, wish, air_turn_rate, dt);
+ p->forward_speed = approachf(p->forward_speed, top_speed(p),
+ PLAYER_SECOND_JUMP_SLOWDOWN * dt);
+ return;
+ }
+
if (wish.mag > 0.0f)
- steer(p, wish,
- PLAYER_TURN_RATE * PLAYER_AIR_CONTROL, PLAYER_AIR_ACCEL, dt);
+ steer(p, wish, air_turn_rate, PLAYER_AIR_ACCEL, dt);
}
/* FALL: in the air, going down. Landing hands straight over to the ground
- * logic on the same tick, so a jump pressed on the landing tick isn't lost. */
+ * logic on the same tick, so a jump pressed on the landing tick isn't lost
+ * (and already counts as the second jump). */
static void update_fall(Player *p, const Input *in, Wish wish, float dt)
{
if (p->body.on_ground) {
+ land(p);
p->state = grounded_state(p);
update_ground(p, in, wish, dt);
return;
@@ -188,7 +283,8 @@ static void update_jump(Player *p, const Input *in, Wish wish, float dt)
air_steer(p, wish, dt);
/* Variable jump height: letting go of A while rising cuts the upward speed.
- * Tapping A gives a short hop, holding it gives the full jump. Switching to
+ * Tapping A gives a short hop, holding it gives the full jump (for the
+ * second jump too). Switching to
* FALL right away makes sure the cut happens only once. */
if (!in->jump_held) {
p->body.vel.y *= PLAYER_JUMP_CUT;
@@ -206,6 +302,9 @@ void player_init(Player *p, Vec3 spawn)
p->facing_yaw = 0.0f;
p->forward_speed = 0.0f;
p->state = PLAYER_IDLE;
+ p->heightened_movement = false;
+ p->heightened_timer = 0.0f;
+ p->jump_number = 0;
}
void player_reset(Player *p, Vec3 spawn)
@@ -216,6 +315,7 @@ void player_reset(Player *p, Vec3 spawn)
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);
/* IDLE and RUN share one helper: they differ only in speed. */
switch (p->state) {