summaryrefslogtreecommitdiff
path: root/platformer
diff options
context:
space:
mode:
Diffstat (limited to 'platformer')
-rw-r--r--platformer/README.md9
-rw-r--r--platformer/game_feel.md50
-rw-r--r--platformer/src/config.h27
-rw-r--r--platformer/src/main.c9
-rw-r--r--platformer/src/player.c242
-rw-r--r--platformer/src/player.h50
6 files changed, 303 insertions, 84 deletions
diff --git a/platformer/README.md b/platformer/README.md
index 33d509c..5a91052 100644
--- a/platformer/README.md
+++ b/platformer/README.md
@@ -39,7 +39,8 @@ plus plain box platforms to jump on) *is* the game until the controls feel great
frame rate. SM64 ran its logic at 30 Hz. We use **60 Hz** because modern displays and gamepads make
the lower input latency noticeable, and the per-tick math stays the same either way.
- **An explicit player state machine.** SM64 has Mario "actions" (idle, walking, jumping, and so on).
- We start with four states (`IDLE`, `RUN`, `JUMP`, `FALL`) and add more only when a move requires it.
+ We started with four states (`IDLE`, `RUN`, `JUMP`, `FALL`) and add more only when a move requires it
+ (so far `SKID`).
- **Analog-magnitude movement.** How far you push the stick sets how fast you move. A radial deadzone
keeps the full 0..1 range usable.
- **Camera-relative controls.** "Up" on the stick means "away from the camera". The player orbits
@@ -91,7 +92,8 @@ The game is designed for a gamepad (Xbox-style layout names; GLFW maps other pad
| Left stick | move (tilt amount = speed, relative to the camera) |
| Right stick | orbit camera (around the player; their spot on screen is kept) |
| Right stick click | recenter: swing the camera behind the player, centered |
-| A (south button) | jump (hold longer to jump higher); press again within 0.3 s of landing for a second jump 1.75× as high |
+| A (south button) | jump (hold longer to jump higher); press again within 0.2 s of landing for a second jump 1.75× as high |
+| Left stick pulled back at full run | skid; press A during the skid for a retreat jump (2× height, toward the stick; landing it also opens the second-jump window) |
| Back / Select | reset player to spawn |
| Start | quit |
@@ -188,7 +190,8 @@ Suggested fill-in order (each step can be compiled before moving on):
- Richer level geometry: slopes, moving platforms, step-up onto small ledges, hand-built or loaded levels
- Camera collision (the camera can currently pass through tall platforms)
- Loading levels
-- SM64-style extra moves: triple jump, long jump, wall kick, ground pound (the second jump exists)
+- SM64-style extra moves: triple jump, long jump, wall kick, ground pound (the second jump and the
+ skid's retreat jump exist)
- Character model and animation
- Audio
- On-screen text/HUD beyond the FPS counter in the top-right corner
diff --git a/platformer/game_feel.md b/platformer/game_feel.md
index c6c8201..e6862a8 100644
--- a/platformer/game_feel.md
+++ b/platformer/game_feel.md
@@ -68,8 +68,8 @@ and eased, not snappier.
is the continuous-physics figure) and a tap gives about 0.72 m. Level geometry is sized against 2.33 m.
- **A jump pressed on the landing tick is never lost.** The FALL state hands off to the ground
logic on the same tick it detects landing.
-- **The second jump (SM64's double jump).** Landing any first jump, hop or full, turns on
- `heightened_movement` for `PLAYER_SECOND_JUMP_WINDOW` (0.3 s, 18 ticks counting the landing tick).
+- **The second jump (SM64's double jump).** Landing any first jump (hop or full) or a retreat jump
+ turns on `heightened_movement` for `PLAYER_SECOND_JUMP_WINDOW` (0.2 s, 12 ticks counting the landing tick).
Pressing A while it's on launches at `PLAYER_SECOND_JUMP_VELOCITY` (18.41 m/s): a full second jump
reaches **4.08 m** (1.75× the first, measured at 60 Hz, 54 ticks of airtime), and tapping A gives
about 1.4 m. `heightened_movement` stays on for the whole second jump, whatever its length, and turns
@@ -84,10 +84,34 @@ and eased, not snappier.
no extra state for this: `jump_number == 2` already means "in the second jump", and `top_speed()`
in `player.c` reads it. Walking off an edge and landing
also turns it off. A press on the landing tick already counts.
-- **`heightened_movement` is a named player state on purpose.** It's a field on `Player`, separate
- from the IDLE/RUN/JUMP/FALL action, because later moves are meant to key off it. The title bar
- shows `| heightened` while it's on (the title refreshes every 0.25 s, so the 0.3 s window may
- blink briefly or not show at all; the second jump itself always shows it).
+- **The skid (SM64's turnaround skid).** At full run and above (at least 95% of `PLAYER_MAX_SPEED`,
+ `PLAYER_SKID_SPEED_FRACTION`, so faster future moves count too), pulling the stick more than 120°
+ (`PLAYER_SKID_ANGLE_DEG`) off the run direction enters the `SKID` state. The player **faces the stick at once**
+ (and keeps tracking it) while still sliding the old way (`skid_yaw`), so they're turned around and
+ sliding backward. There's no steering. The skid always lasts `PLAYER_SKID_DURATION` (0.5 s, 30 ticks)
+ and ends stopped, with speed following `start × (1 − (t / 0.5)^PLAYER_SKID_GLIDE_POWER)`. At power 2
+ (**glide, then plant**), braking starts near zero and builds: from 6 m/s the speed is 5.76 / 5.04 /
+ 3.84 / 2.16 m/s at 0.1 / 0.2 / 0.3 / 0.4 s, the slide covers 1.95 m, and braking peaks at about 24 m/s²
+ right at the end. A higher power glides longer and plants harder, and 1 is even braking.
+ `retreating_movement` is on for the whole skid. Pushing sideways (90°) never skids. Sliding off an
+ edge ends the skid and keeps the slide's direction in the air.
+ **History:** the first skid braked evenly at 18 m/s² (0.33 s) and turned only at the end. The user
+ wanted it longer, gentler at first ("a short slide rather than a fast turnaround"), and facing the
+ stick immediately.
+- **The retreat jump (like SM64's side flip).** Pressing A at any point during the skid launches the player
+ the way they already face (toward the stick, or where it last pointed if it was let go) at `PLAYER_RETREAT_JUMP_SPEED` (4 m/s)
+ in that direction, which is also its top speed until landing. It uses `PLAYER_RETREAT_JUMP_VELOCITY`
+ (19.66 m/s): a full retreat jump reaches **4.67 m** (2.00× the first jump, the highest jump), with
+ 59 ticks of airtime and about 3.9 m of travel; a tap gives about 1.4 m. `retreating_movement` stays
+ on through the jump and turns off on landing. Landing it **does** open the second-jump window
+ (`heightened_movement`, 0.2 s), so skid → retreat jump → second jump chains.
+- **`heightened_movement` and `retreating_movement` are named player states on purpose.** They're
+ fields on `Player`, separate from the IDLE/RUN/SKID/JUMP/FALL action, because later moves are meant
+ to key off them. Which jump the player is in is `jump_kind` (`JUMP_NORMAL`, `JUMP_SECOND`,
+ `JUMP_RETREAT`); the jumps share the `JUMP` action. The title bar
+ shows `| heightened` / `| retreating` while they're on (the title refreshes every 0.25 s, so the
+ 0.2 s window may blink briefly or not show at all; the jumps themselves always show
+ it). The skid also shows as `SKID` in the state slot.
- **SM64-style circling is intentional.** Controls stay purely camera-relative even while the camera
swings on its own, so holding the stick sideways during a camera swing makes the player run a
wide arc (about 12 m radius, 24 m across, at 6 m/s). The user explicitly chose this over locking the stick direction.
@@ -206,11 +230,10 @@ New automatic camera behaviors should follow the same approach: slow, eased, and
with the camera, so the swing chases forever. It only settles when the player steers to hold a line.
- **The starting pitch (20°) differs from the auto-tilt target (15°),** so every session begins with a
small upward tilt. Setting `CAMERA_PITCH_DEFAULT` to 0.26 rad would skip it.
-- **Heavy stops, but not heavy reversals.** Letting go skids, yet pushing the stick the opposite way
- at full speed keeps all the speed and swings around in a tight 0.26 s U-turn, since velocity always
- points along the facing direction. SM64 solves this with a turnaround skid (brake, then turn). That's
- listed under `NOT IN SCOPE` in `player.c` for now, but it's the natural next step if reversals feel
- too light next to the stops.
+- **Reversals below full run are still light.** At full run, pulling back skids (see the skid
+ section). Below 95% of top speed, pulling back still swings around in a tight 0.26 s U-turn with all
+ the speed kept, since velocity always points along the facing direction. Extending the skid to
+ slower runs is listed under `NOT IN SCOPE` in `player.c`.
- **High-angle auto-tilt resets are long** (60° → 15° takes about 15.7 s of motion). For big tilts the
top speed (`CAMERA_AUTO_PITCH_SPEED`) sets the time; for small ones, the acceleration does.
- **No camera collision:** the camera can pass through tall platforms (it's on the roadmap).
@@ -239,9 +262,12 @@ New automatic camera behaviors should follow the same approach: slow, eased, and
| Ground friction | 40 → 16 → 9.5 → 11.3 m/s² (0.23 → 0.45 → 0.53 s stop; the last step kept 0.53 s at 6 m/s) | 11.3 m/s² |
| Air accel | 30% of ground accel → its own constant, value kept | 9 m/s² |
| Second jump height | 2× (19.66 m/s) → 1.75× (18.41 m/s), sets `heightened_movement` | 18.41 m/s |
-| Second jump window | 0.5 s → 0.3 s after landing | 0.3 s |
+| Second jump window | 0.5 s → 0.3 s → 0.2 s after landing | 0.2 s |
+| Second jump from | normal jump landings → also retreat jump landings | both |
| Second jump top speed | 6 m/s (same as running) → 4 → 5 m/s until it lands (4 felt a tad harsh) | 5 m/s |
| Second jump slowdown | air accel 9 m/s² (0.25 s, "wall of syrup") → own rate 3.5 m/s² (0.60 s) | 3.5 m/s² |
+| Skid | none (full-speed U-turn) → 18 m/s² even braking, turn at the end (0.33 s) → face the stick at once, glide-then-plant curve (power 2) | 0.5 s |
+| Retreat jump | added: 2× height, launched at 4 m/s toward the stick, sets `retreating_movement` | 19.66 m/s |
| Standstill turn | turn rate → instant at speed 0 → instant only past 90° (ground only) | > 90° instant |
---
diff --git a/platformer/src/config.h b/platformer/src/config.h
index c767709..f496ee1 100644
--- a/platformer/src/config.h
+++ b/platformer/src/config.h
@@ -51,13 +51,28 @@
* PLAYER_JUMP_CUT 0.50f vel.y multiplier when A released while rising
* PLAYER_SECOND_JUMP_VELOCITY 18.41f m/s upward at second-jump start (1.75x
* the first jump's height, ~4.1 m apex)
- * PLAYER_SECOND_JUMP_WINDOW 0.3f s after landing a jump that the second
+ * PLAYER_SECOND_JUMP_WINDOW 0.2f s after landing a jump that the second
* jump is available (heightened_movement)
* PLAYER_SECOND_JUMP_MAX_SPEED 5.0f m/s top horizontal speed during the second
* jump (it trades speed for height)
* PLAYER_SECOND_JUMP_SLOWDOWN 3.5f m/s^2 how fast speed above that top speed
* bleeds off in the air (gradual on purpose)
*
+ * Player: skid and retreat jump
+ * PLAYER_SKID_ANGLE_DEG 120.0f degrees; pulling the stick more than this
+ * away from the run direction skids
+ * PLAYER_SKID_SPEED_FRACTION 0.95f of PLAYER_MAX_SPEED; a skid needs a full
+ * run (or faster, once later moves exist)
+ * PLAYER_SKID_DURATION 0.5f s a skid lasts; it always ends stopped
+ * PLAYER_SKID_GLIDE_POWER 2.0f shape of the skid's braking: speed =
+ * start * (1 - (t / duration)^power).
+ * 1 = even braking; higher = glides
+ * longer, then plants harder at the end
+ * PLAYER_RETREAT_JUMP_VELOCITY 19.66f m/s upward for the jump out of a skid
+ * (2x the first jump's height, ~4.7 m)
+ * PLAYER_RETREAT_JUMP_SPEED 4.0f m/s launched toward the stick, and the top
+ * speed until that jump lands
+ *
* Physics
* GRAVITY 40.0f m/s^2 (stronger than real 9.8 on purpose:
* platformers feel floaty with real gravity)
@@ -155,10 +170,18 @@
#define PLAYER_JUMP_VELOCITY 14.0f /* m/s upward at jump start; higher = taller jumps (~2.4 m apex at 14) */
#define PLAYER_JUMP_CUT 0.50f /* vel.y multiplier on early A release; lower = shorter taps */
#define PLAYER_SECOND_JUMP_VELOCITY 18.41f /* m/s upward for the second jump; tuned to 1.75x the first jump's height (4.08 m at 60 Hz) */
-#define PLAYER_SECOND_JUMP_WINDOW 0.3f /* s after landing a jump that A gives the second jump; higher = more forgiving timing */
+#define PLAYER_SECOND_JUMP_WINDOW 0.2f /* s after landing a normal or retreat jump that A gives the second jump; higher = more forgiving timing */
#define PLAYER_SECOND_JUMP_MAX_SPEED 5.0f /* m/s top horizontal speed until the second jump lands; lower = more vertical, less distance */
#define PLAYER_SECOND_JUMP_SLOWDOWN 3.5f /* m/s^2 bleed from run speed down to that top speed; lower = more gradual, higher = more abrupt */
+/* ---- Player: skid and retreat jump ------------------------------------- */
+#define PLAYER_SKID_ANGLE_DEG 120.0f /* degrees off the run direction that counts as "pulling back"; lower = skids trigger more easily */
+#define PLAYER_SKID_SPEED_FRACTION 0.95f /* of PLAYER_MAX_SPEED needed to skid (full run or faster); lower = skid from slower runs */
+#define PLAYER_SKID_DURATION 0.5f /* s from pulling back to stopped (and the retreat jump's window); higher = longer slide */
+#define PLAYER_SKID_GLIDE_POWER 2.0f /* skid braking curve; 1 = even, higher = freer glide first, then a harder plant */
+#define PLAYER_RETREAT_JUMP_VELOCITY 19.66f /* m/s upward for the jump out of a skid; tuned to 2x the first jump's height (4.67 m at 60 Hz) */
+#define PLAYER_RETREAT_JUMP_SPEED 4.0f /* m/s launched toward the stick, and the top speed until landing; higher = travels farther */
+
/* ---- Physics ----------------------------------------------------------- */
#define GRAVITY 40.0f /* m/s^2; stronger than real 9.8 on purpose, real gravity feels floaty */
#define TERMINAL_VELOCITY 50.0f /* m/s max fall speed; lower = long falls feel more controlled */
diff --git a/platformer/src/main.c b/platformer/src/main.c
index 61972cc..ce02b85 100644
--- a/platformer/src/main.c
+++ b/platformer/src/main.c
@@ -63,8 +63,8 @@
* window_set_title() with something like
* "platformer | RUN | speed 7.42 m/s | y 1.83 | pad | heightened"
* using player_state_name(), forward_speed, body.pos.y,
- * input.using_gamepad, and "| heightened" only while
- * player.heightened_movement is on. This is our "HUD" until text rendering exists.
+ * input.using_gamepad, and "| heightened" / "| retreating" only while
+ * player.heightened_movement / player.retreating_movement is on. This is our "HUD" until text rendering exists.
*
* - Shutdown in reverse order: render_shutdown() -> window_shutdown().
*
@@ -99,13 +99,14 @@
static void update_debug_title(const Player *player, const Input *input)
{
char title[128];
- snprintf(title, sizeof title, "%s | %s | speed %.2f m/s | y %.2f | %s%s",
+ snprintf(title, sizeof title, "%s | %s | speed %.2f m/s | y %.2f | %s%s%s",
WINDOW_TITLE,
player_state_name(player->state),
player->forward_speed,
player->body.pos.y,
input->using_gamepad ? "pad" : "keyboard",
- player->heightened_movement ? " | heightened" : "");
+ player->heightened_movement ? " | heightened" : "",
+ player->retreating_movement ? " | retreating" : "");
window_set_title(title);
}
diff --git a/platformer/src/player.c b/platformer/src/player.c
index 88df0c4..b4341de 100644
--- a/platformer/src/player.c
+++ b/platformer/src/player.c
@@ -15,10 +15,12 @@
* - 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, jump_number = 0.
+ * heightened_movement = false, heightened_timer = 0,
+ * retreating_movement = false, jump_kind = JUMP_NONE, skid_time = 0,
+ * skid_speed = 0, skid_yaw = 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
+ * (update_ground for IDLE + RUN / update_skid / update_jump / update_fall), dispatched by a
* `switch (p->state)`. Keep each helper short. Shared steps:
*
* A. Stick -> world direction (camera-relative). Using camera.h's convention:
@@ -42,31 +44,60 @@
* purpose: starts build up and stops skid briefly, so the player
* feels like a heavy, real object rather than a cursor.
* - State: forward_speed > 0 -> RUN, otherwise IDLE.
+ * - Pulling back at full run -> SKID (see B2), checked before steering.
*
- * C. Jump (from IDLE / RUN): if in->jump_pressed and body.on_ground:
- * on_ground = false; state = JUMP, and then either
+ * B2. Skid (SM64's turnaround skid). In IDLE/RUN, if mag > 0,
+ * forward_speed >= PLAYER_MAX_SPEED * PLAYER_SKID_SPEED_FRACTION ("full
+ * run and above", so faster future moves count too), and wish_yaw is
+ * more than PLAYER_SKID_ANGLE_DEG from facing_yaw: state = SKID,
+ * retreating_movement = true, skid_yaw = facing_yaw (the slide keeps
+ * going the old way), skid_speed = forward_speed, skid_time = 0.
+ * While skidding:
+ * - The player FACES THE STICK at once (and keeps tracking it), while
+ * still sliding along skid_yaw: turned around, sliding backward.
+ * - No steering. Speed follows a curve, whatever the stick does:
+ * forward_speed = skid_speed * (1 - (skid_time / PLAYER_SKID_DURATION)
+ * ^ PLAYER_SKID_GLIDE_POWER)
+ * Braking starts near zero, so it glides like a short slide, then
+ * builds so the player stops exactly at PLAYER_SKID_DURATION.
+ * - At PLAYER_SKID_DURATION: speed 0, retreating_movement off, IDLE.
+ * - Sliding off an edge ends the skid too (facing_yaw goes back to
+ * skid_yaw, so the fall keeps the slide's direction).
+ * 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:
+ * 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 =
+ * PLAYER_RETREAT_JUMP_SPEED, body.vel.y = PLAYER_RETREAT_JUMP_VELOCITY
+ * (2x the height, the highest jump); jump_kind = JUMP_RETREAT.
+ * retreating_movement stays on until it lands.
* - 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).
+ * 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_number = 1.
+ * jump_kind = JUMP_NORMAL.
*
- * 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.
+ * C2. Landing: landing from a normal jump (JUMP_NORMAL, short hop or
+ * full) or a retreat jump (JUMP_RETREAT) turns heightened_movement on
+ * with heightened_timer = PLAYER_SECOND_JUMP_WINDOW, so either one
+ * can lead into the second jump. Landing any other way (from the
+ * second jump, or after walking off an edge) turns it off. Every
+ * landing turns retreating_movement off and resets jump_kind to
+ * JUMP_NONE. Every tick, unless the player is in the second jump, the
+ * heightened timer counts down and it turns off at 0. WHY named
+ * states rather than private "can double jump" flags: later moves
+ * will key off them 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).
* - Top speed: steering aims for top_speed() * mag, where top_speed()
- * is PLAYER_SECOND_JUMP_MAX_SPEED while jump_number == 2 and
+ * is PLAYER_SECOND_JUMP_MAX_SPEED in the second jump,
+ * PLAYER_RETREAT_JUMP_SPEED in the retreat jump, 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
@@ -74,9 +105,9 @@
* 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,
+ * accel) after landing. WHY no new state: jump_kind already says
+ * which jump the player is in.
+ * - Variable jump height (every jump): 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.
@@ -86,8 +117,9 @@
* - If the player is on the ground state-wise but body.on_ground is false
* (walked off a platform edge) -> FALL.
*
- * E. Apply: body.vel.x = sin(facing_yaw) * forward_speed
- * body.vel.z = cos(facing_yaw) * forward_speed
+ * E. Apply: move_yaw = skid_yaw while in SKID, else facing_yaw.
+ * body.vel.x = sin(move_yaw) * forward_speed
+ * body.vel.z = cos(move_yaw) * forward_speed
* then physics_step(&p->body, dt).
*
* - player_state_name: a switch that returns string literals.
@@ -96,8 +128,8 @@
* accelerations in m/s^2, and every one of them multiplies by dt.
* No magic numbers; everything comes from config.h.
*
- * NOT IN SCOPE: Triple jump, long jump, wall kick, ground pound,
- * skidding/turnaround, slopes, coyote time, jump buffering.
+ * NOT IN SCOPE: Triple jump, long jump, wall kick, ground pound, skids
+ * below full run speed, slopes, coyote time, jump buffering.
* Leave a short comment at the state enum pointing to the
* README roadmap as the place to grow.
*
@@ -106,9 +138,12 @@
* 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. Pressing
- * A within 0.3 s of landing gives a second jump 1.75x as high, with
+ * A within 0.2 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.
+ * lands. Pulling the stick back at full run turns the player to
+ * face the stick and slides them backward to a stop over 0.5 s,
+ * and A during that slide launches a 2x-height jump toward the
+ * stick at 4 m/s; landing it opens the second-jump window too. The player lands cleanly back in IDLE/RUN.
* ============================================================
*/
@@ -122,6 +157,7 @@ typedef struct {
} Wish;
static void update_ground(Player *p, const Input *in, Wish wish, float dt);
+static void update_skid(Player *p, const Input *in, Wish wish, float dt);
static void update_fall(Player *p, const Input *in, Wish wish, float dt);
/* A. Stick -> world direction. "Up" on the stick means "away from the
@@ -139,11 +175,27 @@ 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. */
+/* The fastest the player may go right now. The second and retreat jumps
+ * trade horizontal speed for their extra height, until they land. */
static float top_speed(const Player *p)
{
- return p->jump_number == 2 ? PLAYER_SECOND_JUMP_MAX_SPEED : PLAYER_MAX_SPEED;
+ switch (p->jump_kind) {
+ case JUMP_SECOND: return PLAYER_SECOND_JUMP_MAX_SPEED;
+ case JUMP_RETREAT: return PLAYER_RETREAT_JUMP_SPEED;
+ default: return PLAYER_MAX_SPEED;
+ }
+}
+
+/* Degrees to radians, for the _DEG tunables. */
+static float deg_to_rad(float deg)
+{
+ return deg * (M3D_PI / 180.0f);
+}
+
+/* How far the stick points from where the player faces, 0..PI radians. */
+static float angle_off_facing(const Player *p, Wish wish)
+{
+ return fabsf(wrap_angle(wish.yaw - p->facing_yaw));
}
/* Turns the facing toward the stick, at most turn_rate. */
@@ -166,49 +218,75 @@ 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)
+/* C. Leaves the ground with the given upward speed. */
+static void leave_ground(Player *p, float vel_y, JumpKind kind)
{
- 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.vel.y = vel_y;
p->body.on_ground = false;
+ p->jump_kind = kind;
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. */
+/* 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. */
+static void start_jump(Player *p)
+{
+ if (p->heightened_movement)
+ leave_ground(p, PLAYER_SECOND_JUMP_VELOCITY, JUMP_SECOND);
+ else
+ leave_ground(p, PLAYER_JUMP_VELOCITY, JUMP_NORMAL);
+}
+
+/* C. The jump out of a skid, like SM64's side flip: the player already faces
+ * where the stick points (the skid turned them) and launches that way,
+ * higher than any other jump but slower. retreating_movement stays on
+ * until it lands. */
+static void start_retreat_jump(Player *p)
+{
+ p->forward_speed = PLAYER_RETREAT_JUMP_SPEED;
+ leave_ground(p, PLAYER_RETREAT_JUMP_VELOCITY, JUMP_RETREAT);
+}
+
+/* C2. Touching down. Landing a normal jump (a hop or a full one) or a
+ * retreat jump opens the short window for the second jump; landing any
+ * other way closes it, so the second jump can't chain into another one.
+ * Any landing ends a retreat jump's retreating_movement. */
static void land(Player *p)
{
- if (p->jump_number == 1) {
+ if (p->jump_kind == JUMP_NORMAL || p->jump_kind == JUMP_RETREAT) {
p->heightened_movement = true;
p->heightened_timer = PLAYER_SECOND_JUMP_WINDOW;
} else {
p->heightened_movement = false;
}
- p->jump_number = 0;
+ p->retreating_movement = false;
+ p->jump_kind = JUMP_NONE;
}
/* 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)
+ if (!p->heightened_movement || p->jump_kind == JUMP_SECOND)
return;
p->heightened_timer -= dt;
if (p->heightened_timer <= 0.0f)
p->heightened_movement = false;
}
+/* B2. Pulling the stick back while running flat out. "Full run and above",
+ * so the faster moves still to come can skid too. */
+static bool wants_skid(const Player *p, Wish wish)
+{
+ return wish.mag > 0.0f
+ && p->forward_speed >= PLAYER_MAX_SPEED * PLAYER_SKID_SPEED_FRACTION
+ && angle_off_facing(p, wish) > deg_to_rad(PLAYER_SKID_ANGLE_DEG);
+}
+
/* B + C. IDLE and RUN: full control, friction when the stick is released,
- * and the only place a jump can start. */
+ * and where normal jumps start. */
static void update_ground(Player *p, const Input *in, Wish wish, float dt)
{
/* Walked off a platform edge: start falling. */
@@ -218,12 +296,21 @@ static void update_ground(Player *p, const Input *in, Wish wish, float dt)
return;
}
+ if (wants_skid(p, wish)) {
+ p->state = PLAYER_SKID;
+ p->retreating_movement = true;
+ p->skid_yaw = p->facing_yaw; /* keep sliding the way we were running */
+ p->skid_speed = p->forward_speed;
+ p->skid_time = 0.0f;
+ update_skid(p, in, wish, dt);
+ return;
+ }
+
if (wish.mag > 0.0f) {
/* Standing still there's no momentum to swing around, so a big turn
* faces the stick right away. Small ones keep the quick turn rate so
* nudging the stick to adjust your aim still looks smooth. */
- float turn = fabsf(wrap_angle(wish.yaw - p->facing_yaw));
- if (p->forward_speed == 0.0f && turn > PLAYER_SNAP_TURN_DEG * (M3D_PI / 180.0f))
+ if (p->forward_speed == 0.0f && angle_off_facing(p, wish) > deg_to_rad(PLAYER_SNAP_TURN_DEG))
p->facing_yaw = wish.yaw;
steer(p, wish, PLAYER_TURN_RATE, PLAYER_GROUND_ACCEL, dt);
} else {
@@ -235,6 +322,46 @@ static void update_ground(Player *p, const Input *in, Wish wish, float dt)
start_jump(p);
}
+/* B2 + C. SKID: pulled back at full run. The player turns to face the stick
+ * right away but keeps sliding the old way (skid_yaw), like a short
+ * slide on the heels. retreating_movement is on the whole time, and A
+ * launches the retreat jump. */
+static void update_skid(Player *p, const Input *in, Wish wish, float dt)
+{
+ /* Slid off a platform edge: the skid (and its jump) is over. The air
+ * moves along facing_yaw, so point it back along the slide to keep the
+ * player's momentum going the same way. */
+ if (!p->body.on_ground) {
+ p->retreating_movement = false;
+ p->facing_yaw = p->skid_yaw;
+ p->state = PLAYER_FALL;
+ update_fall(p, in, wish, dt);
+ return;
+ }
+
+ /* Face the stick immediately; the slide direction doesn't change. */
+ if (wish.mag > 0.0f)
+ p->facing_yaw = wish.yaw;
+
+ if (in->jump_pressed) {
+ start_retreat_jump(p);
+ return;
+ }
+
+ /* Glide, then plant: braking starts near zero and builds, so the first
+ * part feels like a free slide and the player stops exactly when the skid
+ * ends. PLAYER_SKID_GLIDE_POWER shapes the curve (1 = even braking). */
+ p->skid_time += dt;
+ float t = clampf(p->skid_time / PLAYER_SKID_DURATION, 0.0f, 1.0f);
+ p->forward_speed = p->skid_speed * (1.0f - powf(t, PLAYER_SKID_GLIDE_POWER));
+
+ if (t >= 1.0f) {
+ p->forward_speed = 0.0f;
+ p->retreating_movement = false;
+ p->state = PLAYER_IDLE;
+ }
+}
+
/* 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)
@@ -284,7 +411,7 @@ static void update_jump(Player *p, const Input *in, Wish wish, float 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 (for the
- * second jump too). Switching to
+ * second and retreat jumps too). Switching to
* FALL right away makes sure the cut happens only once. */
if (!in->jump_held) {
p->body.vel.y *= PLAYER_JUMP_CUT;
@@ -304,7 +431,11 @@ void player_init(Player *p, Vec3 spawn)
p->state = PLAYER_IDLE;
p->heightened_movement = false;
p->heightened_timer = 0.0f;
- p->jump_number = 0;
+ p->retreating_movement = false;
+ p->jump_kind = JUMP_NONE;
+ p->skid_time = 0.0f;
+ p->skid_speed = 0.0f;
+ p->skid_yaw = 0.0f;
}
void player_reset(Player *p, Vec3 spawn)
@@ -321,13 +452,17 @@ void player_update(Player *p, const Input *in, float camera_yaw, float dt)
switch (p->state) {
case PLAYER_IDLE:
case PLAYER_RUN: update_ground(p, in, wish, dt); break;
+ case PLAYER_SKID: update_skid(p, in, wish, dt); break;
case PLAYER_JUMP: update_jump(p, in, wish, dt); break;
case PLAYER_FALL: update_fall(p, in, wish, dt); break;
}
- /* E. Turn "speed along facing" into a velocity, then let physics move us. */
- p->body.vel.x = sinf(p->facing_yaw) * p->forward_speed;
- p->body.vel.z = cosf(p->facing_yaw) * p->forward_speed;
+ /* 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
+ * the stick but keep sliding the way they were running. */
+ float move_yaw = p->state == PLAYER_SKID ? p->skid_yaw : p->facing_yaw;
+ p->body.vel.x = sinf(move_yaw) * p->forward_speed;
+ p->body.vel.z = cosf(move_yaw) * p->forward_speed;
physics_step(&p->body, dt);
}
@@ -338,6 +473,7 @@ const char *player_state_name(PlayerState s)
case PLAYER_RUN: return "RUN";
case PLAYER_JUMP: return "JUMP";
case PLAYER_FALL: return "FALL";
+ case PLAYER_SKID: return "SKID";
}
return "?";
}
diff --git a/platformer/src/player.h b/platformer/src/player.h
index 5f8d963..4d8fdc6 100644
--- a/platformer/src/player.h
+++ b/platformer/src/player.h
@@ -15,9 +15,18 @@
* PLAYER_IDLE, on ground, not moving
* PLAYER_RUN, on ground, moving (walk vs run is just speed)
* PLAYER_JUMP, in air, going up after a jump
- * PLAYER_FALL in air, going down (or walked off something)
+ * PLAYER_FALL, in air, going down (or walked off something)
+ * PLAYER_SKID on ground, braking after pulling the stick back at
+ * full run; retreating_movement is on
* } PlayerState;
*
+ * typedef enum JumpKind { which jump the player is in the air from
+ * JUMP_NONE, on the ground, or airborne without jumping
+ * JUMP_NORMAL, a normal jump
+ * JUMP_SECOND, the higher jump from heightened_movement
+ * JUMP_RETREAT the jump out of a skid, from retreating_movement
+ * } JumpKind;
+ *
* typedef struct Player {
* Body body; position / velocity / on_ground (physics.h)
* float facing_yaw; direction the player faces, radians.
@@ -26,14 +35,20 @@
* float forward_speed; ground speed along facing_yaw, m/s (>= 0)
* PlayerState state;
* bool heightened_movement; on for PLAYER_SECOND_JUMP_WINDOW s after
- * landing a normal jump, and for the whole
+ * landing a normal or retreat jump, and for the whole
* second jump. While on, A gives the second
* jump. Later moves will read it too.
* float heightened_timer; s left before heightened_movement ends
* (not counted down during the second jump)
- * int jump_number; which jump the player is in the air from:
- * 0 = none (walked off an edge, or grounded),
- * 1 = a normal jump, 2 = the second jump
+ * bool retreating_movement; on for the whole skid (PLAYER_SKID) and
+ * the retreat jump out of it. While skidding, A
+ * gives the retreat jump. Later moves will read
+ * it too.
+ * JumpKind jump_kind; which jump the player is in the air from
+ * float skid_time; s since the current skid began
+ * float skid_speed; forward_speed when the skid began
+ * float skid_yaw; direction the skid slides (the old run
+ * direction; the player already faces the stick)
* } Player;
*
* void player_init(Player *p, Vec3 spawn);
@@ -46,8 +61,8 @@
*
* CONVENTIONS: Include guard: PLAYER_H.
*
- * NOT IN SCOPE: Health, animation, extra moves beyond the second jump (see the
- * README roadmap).
+ * NOT IN SCOPE: Health, animation, extra moves beyond the second jump and the
+ * skid's retreat jump (see the README roadmap).
*
* DONE WHEN: main.c and render.c compile against this header alone.
* ============================================================
@@ -61,14 +76,25 @@
/* SM64-style "actions". Add states here only when a new move needs one;
* see the README roadmap (triple jump, long jump, wall kick, ...). The second
- * jump didn't need one: it's a JUMP with a higher launch (see jump_number). */
+ * and retreat jumps didn't need one: they're a JUMP with a different launch
+ * (see JumpKind). The skid did, because it follows its own ground rules. */
typedef enum PlayerState {
PLAYER_IDLE, /* on ground, not moving */
PLAYER_RUN, /* on ground, moving (walk vs run is just speed) */
PLAYER_JUMP, /* in air, going up after a jump */
- PLAYER_FALL /* in air, going down (or walked off something) */
+ PLAYER_FALL, /* in air, going down (or walked off something) */
+ PLAYER_SKID /* on ground, braking after pulling back at full run */
} PlayerState;
+/* Which jump the player is in the air from. It decides the top speed in the
+ * air and what landing turns on or off. */
+typedef enum JumpKind {
+ JUMP_NONE, /* on the ground, or airborne without jumping (walked off an edge) */
+ JUMP_NORMAL, /* a normal jump */
+ JUMP_SECOND, /* the higher jump from heightened_movement */
+ JUMP_RETREAT /* the jump out of a skid, from retreating_movement */
+} JumpKind;
+
typedef struct Player {
Body body; /* position / velocity / on_ground */
float facing_yaw; /* radians; facing vector = (sin(yaw), 0, cos(yaw)) */
@@ -76,7 +102,11 @@ typedef struct Player {
PlayerState state;
bool heightened_movement; /* second jump available (or in progress); later moves read it too */
float heightened_timer; /* s left before heightened_movement ends; paused during the second jump */
- int jump_number; /* airborne from: 0 = no jump (walked off an edge), 1 = normal, 2 = second */
+ bool retreating_movement; /* skidding (or in the jump out of a skid); later moves read it too */
+ JumpKind jump_kind; /* which jump the player is in the air from */
+ float skid_time; /* s since the current skid began */
+ float skid_speed; /* forward_speed when the skid began */
+ float skid_yaw; /* direction the skid slides; facing_yaw already points at the stick */
} Player;
void player_init(Player *p, Vec3 spawn);