diff options
Diffstat (limited to 'platformer/src/player.c')
| -rw-r--r-- | platformer/src/player.c | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/platformer/src/player.c b/platformer/src/player.c new file mode 100644 index 0000000..ed8da51 --- /dev/null +++ b/platformer/src/player.c @@ -0,0 +1,225 @@ +/* + * ============================================================ + * 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. + * ============================================================ + */ + +#include "config.h" +#include "player.h" + +/* What the stick is asking for this tick, in world space. */ +typedef struct { + float mag; /* 0..1, how far the stick is pushed */ + float yaw; /* world direction to move in; only meaningful if mag > 0 */ +} Wish; + +static void update_ground(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 + * camera", so the stick is rotated by the camera's yaw (camera.h's + * convention for forward/right on the ground plane). */ +static Wish compute_wish(const Input *in, float camera_yaw) +{ + Vec3 fwd = vec3_make(-sinf(camera_yaw), 0.0f, -cosf(camera_yaw)); + Vec3 right = vec3_make( cosf(camera_yaw), 0.0f, -sinf(camera_yaw)); + Vec3 dir = vec3_add(vec3_scale(fwd, in->move.y), vec3_scale(right, in->move.x)); + + Wish wish; + wish.mag = vec2_length(in->move); + wish.yaw = atan2f(dir.x, dir.z); /* inverse of facing = (sin(yaw), 0, cos(yaw)) */ + return wish; +} + +/* 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); +} + +/* Standing still vs walking vs running is only a matter of speed. */ +static PlayerState grounded_state(const Player *p) +{ + return p->forward_speed > 0.0f ? PLAYER_RUN : PLAYER_IDLE; +} + +/* 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) +{ + /* Walked off an edge (once edges exist): start falling. */ + if (!p->body.on_ground) { + p->state = PLAYER_FALL; + update_fall(p, in, wish, dt); + return; + } + + if (wish.mag > 0.0f) + steer(p, wish, PLAYER_TURN_RATE, PLAYER_GROUND_ACCEL, dt); + else + p->forward_speed = approachf(p->forward_speed, 0.0f, PLAYER_GROUND_FRICTION * 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; + } +} + +/* 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) +{ + if (wish.mag > 0.0f) + steer(p, wish, + PLAYER_TURN_RATE * PLAYER_AIR_CONTROL, + PLAYER_GROUND_ACCEL * PLAYER_AIR_CONTROL, 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. */ +static void update_fall(Player *p, const Input *in, Wish wish, float dt) +{ + if (p->body.on_ground) { + p->state = grounded_state(p); + update_ground(p, in, wish, dt); + return; + } + air_steer(p, wish, dt); +} + +/* JUMP: in the air, still rising from a jump. */ +static void update_jump(Player *p, const Input *in, Wish wish, float dt) +{ + /* Past the apex (or landed already): it's a fall now. */ + if (p->body.vel.y <= 0.0f) { + p->state = PLAYER_FALL; + update_fall(p, in, wish, dt); + return; + } + + 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 + * FALL right away makes sure the cut happens only once. */ + if (!in->jump_held) { + p->body.vel.y *= PLAYER_JUMP_CUT; + p->state = PLAYER_FALL; + } +} + +void player_init(Player *p, Vec3 spawn) +{ + p->body.pos = spawn; + p->body.vel = vec3_make(0.0f, 0.0f, 0.0f); + p->body.on_ground = true; + p->facing_yaw = 0.0f; + p->forward_speed = 0.0f; + p->state = PLAYER_IDLE; +} + +void player_reset(Player *p, Vec3 spawn) +{ + player_init(p, spawn); +} + +void player_update(Player *p, const Input *in, float camera_yaw, float dt) +{ + Wish wish = compute_wish(in, camera_yaw); + + /* IDLE and RUN share one helper: they differ only in speed. */ + switch (p->state) { + case PLAYER_IDLE: + case PLAYER_RUN: update_ground(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; + physics_step(&p->body, dt); +} + +const char *player_state_name(PlayerState s) +{ + switch (s) { + case PLAYER_IDLE: return "IDLE"; + case PLAYER_RUN: return "RUN"; + case PLAYER_JUMP: return "JUMP"; + case PLAYER_FALL: return "FALL"; + } + return "?"; +} |
