summaryrefslogtreecommitdiff
path: root/platformer/src/player.c
blob: 1f1b94cec2d9bb8200b22e5ab4908cc62f5efcb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * ============================================================
 * FILE:        player.c
 *
 * PURPOSE:     The heart of the current milestone: how the character runs,
 *              turns and jumps. Modeled on SM64's approach: an explicit state
 *              machine, speed that follows the analog stick, and a facing
 *              direction that turns toward the stick instead of snapping.
 *
 * FITS IN:     Implements player.h. Reads Input (controls.h). Writes the
 *              Body's velocity, then calls physics_step() (physics.h) to do the
 *              actual motion. Tunables all come from config.h.
 *
 * IMPLEMENT:
 *   - player_init / player_reset: body.pos = spawn, vel = 0, on_ground = true,
 *     facing_yaw = 0, forward_speed = 0, state = PLAYER_IDLE.
 *
 *   - player_update(p, in, camera_yaw, dt). Use one static helper per state
 *     (update_idle / update_run / 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:
 *          fwd   = (-sin(camera_yaw), 0, -cos(camera_yaw))
 *          right = ( cos(camera_yaw), 0, -sin(camera_yaw))
 *          wish  = fwd * in->move.y + right * in->move.x
 *          mag   = vec2_length(in->move)          (0..1, already deadzoned)
 *          wish_yaw = atan2f(wish.x, wish.z)      (only meaningful if mag > 0)
 *
 *     B. Ground movement (IDLE / RUN):
 *          - If mag > 0: facing_yaw = approach_angle(facing_yaw, wish_yaw,
 *              PLAYER_TURN_RATE * dt);  forward_speed = approachf(forward_speed,
 *              PLAYER_MAX_SPEED * mag, PLAYER_GROUND_ACCEL * dt)
 *          - Else: forward_speed = approachf(forward_speed, 0,
 *              PLAYER_GROUND_FRICTION * dt)
 *          - 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.
 *
 *     D. Air movement (JUMP / FALL): the same as B, but turn rate and accel are
 *          multiplied by PLAYER_AIR_CONTROL, 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,
 *            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).
 *          - If the player is on the ground state-wise but body.on_ground is false
 *            (walked off an edge, once edges exist) -> FALL.
 *
 *     E. Apply: body.vel.x = sin(facing_yaw) * forward_speed
 *               body.vel.z = cos(facing_yaw) * forward_speed
 *        then physics_step(&p->body, dt).
 *
 *   - player_state_name: a switch that returns string literals.
 *
 * CONVENTIONS: Facing vector = (sin(yaw), 0, cos(yaw)). Speeds are in m/s,
 *              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,
 *              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.
 *
 * DONE WHEN:   Small stick tilts walk slowly and full tilt runs at
 *              PLAYER_MAX_SPEED. Turning is quick but not instant, and letting go
 *              of the stick brings 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.
 * ============================================================
 */