diff options
Diffstat (limited to '')
| -rw-r--r-- | platformer/src/player.c | 241 |
1 files changed, 222 insertions, 19 deletions
diff --git a/platformer/src/player.c b/platformer/src/player.c index 7fe0d76..737a4c9 100644 --- a/platformer/src/player.c +++ b/platformer/src/player.c @@ -17,10 +17,13 @@ * facing_yaw = 0, forward_speed = 0, state = PLAYER_IDLE, * heightened_movement = false, heightened_timer = 0, * retreating_movement = false, jump_kind = JUMP_NONE, skid_time = 0, - * skid_speed = 0, skid_yaw = 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, + * body.ground_box = -1. * * - player_update(p, in, camera_yaw, dt). Use one static helper per state - * (update_ground for IDLE + RUN / update_skid / update_jump / update_fall), dispatched by a + * (update_ground for IDLE + RUN / update_skid / update_jump / update_fall / + * update_wall_hang), dispatched by a * `switch (p->state)`. Keep each helper short. Shared steps: * * A. Stick -> world direction (camera-relative). Using camera.h's convention: @@ -77,7 +80,8 @@ * 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. + * retreating_movement AND heightened_movement are on until it + * lands (so it can grab a wall; see F). * - heightened_movement on: the SECOND jump, SM64's double jump. * body.vel.y = PLAYER_SECOND_JUMP_VELOCITY (1.75x the height); * jump_kind = JUMP_SECOND. heightened_movement stays on for the @@ -91,8 +95,10 @@ * 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 + * JUMP_NONE, and clears has_last_wall. The heightened timer counts + * down (turning it off at 0) only while jump_kind == JUMP_NONE and + * not wall hanging: a jump or hang that carries heightened_movement + * keeps it until it ends. WHY named * states rather than private "can double jump" flags: later moves * will key off them too. * @@ -128,10 +134,45 @@ * - If the player is on the ground state-wise but body.on_ground is false * (walked off a platform edge) -> FALL. * - * 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). + * 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 + * (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 + * 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 + * wall jump will launch; forward_speed = 0; vel.y = 0; wall_time = 0. + * While hanging: + * - heightened_movement stays on for the whole hang. + * - Gravity is off (body.gravity_scale = 0) and the player slides down + * at vel.y = -min(PLAYER_WALL_SLIDE_ACCEL * wall_time, + * PLAYER_WALL_SLIDE_MAX_SPEED): a slow slide that builds gently. + * - 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 + * 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, + * launched at PLAYER_MAX_SPEED, JUMP_NORMAL). Either way + * heightened_movement stays on for the whole jump (so any wall jump + * can grab the next wall) and this wall's surface is remembered. + * - After PLAYER_WALL_HANG_DURATION, or losing contact: let go. FALL + * at normal gravity with forward_speed = 0, jump_kind = JUMP_NONE and + * heightened_movement off (so no grabbing again before landing). + * - Reaching the floor is a normal landing. + * + * E. Apply: move_yaw = skid_yaw while in SKID, straight into the wall + * (-body.wall_normal) while in WALL_HANG, else facing_yaw; speed = + * WALL_PRESS_SPEED while in WALL_HANG, else forward_speed. + * body.vel.x = sin(move_yaw) * speed + * body.vel.z = cos(move_yaw) * speed + * body.gravity_scale = 0 in WALL_HANG, else 1. + * Then physics_step(&p->body, dt). * * - player_state_name: a switch that returns string literals. * @@ -156,13 +197,19 @@ * 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. A * full jump at full run builds gently to ~7 m/s, and that extra - * speed eases back to 6 m/s after landing. The player lands cleanly back in IDLE/RUN. + * speed eases back to 6 m/s after landing. With heightened_movement + * on, hitting a violet (hangable) wall near head-on clings for + * 0.5 s, sliding slowly down, and A jumps off it mirrored. The player lands cleanly back in IDLE/RUN. * ============================================================ */ #include "config.h" #include "player.h" +/* How hard a wall-hanging player presses into the wall, m/s. Not a feel + * tunable: just enough for physics to keep reporting the contact each tick. */ +#define WALL_PRESS_SPEED 0.5f + /* What the stick is asking for this tick, in world space. */ typedef struct { float mag; /* 0..1, how far the stick is pushed */ @@ -172,6 +219,7 @@ typedef struct { 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); +static void update_wall_hang(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 @@ -259,7 +307,8 @@ static void start_jump(Player *p) * until it lands. */ static void start_retreat_jump(Player *p) { - p->forward_speed = PLAYER_RETREAT_JUMP_SPEED; + p->forward_speed = PLAYER_RETREAT_JUMP_SPEED; + p->heightened_movement = true; /* a heightened move: it can grab a wall */ leave_ground(p, PLAYER_RETREAT_JUMP_VELOCITY, JUMP_RETREAT); } @@ -277,13 +326,15 @@ static void land(Player *p) } p->retreating_movement = false; p->jump_kind = JUMP_NONE; + p->has_last_wall = false; /* any wall can be grabbed again */ } -/* 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. */ +/* C2. The window after landing runs out on its own. It doesn't during a jump + * or wall hang that carries heightened_movement (second, retreat, early + * wall jump): there it lasts as long as that move does. */ static void count_down_heightened(Player *p, float dt) { - if (!p->heightened_movement || p->jump_kind == JUMP_SECOND) + if (!p->heightened_movement || p->jump_kind != JUMP_NONE || p->state == PLAYER_WALL_HANG) return; p->heightened_timer -= dt; if (p->heightened_timer <= 0.0f) @@ -382,6 +433,126 @@ static void update_skid(Player *p, const Input *in, Wish wish, float dt) } } +/* ---- F. Wall hang ----------------------------------------------------- */ + +/* Where a wall's face lies along its normal: the x of an east- or + * west-facing side, the z of a north- or south-facing one. */ +static float wall_plane(Box wall, Vec3 normal) +{ + if (normal.x > 0.5f) return wall.max.x; + if (normal.x < -0.5f) return wall.min.x; + if (normal.z > 0.5f) return wall.max.z; + return wall.min.z; +} + +/* Is the wall just hit the same SURFACE as the one last jumped off? Same + * facing and same face position: a cliff built from side-by-side boxes is + * still one wall, so zigzagging along it can't climb it forever. */ +static bool same_wall_as_last(const Player *p) +{ + const Body *b = &p->body; + return p->has_last_wall && + vec3_dot(b->wall_normal, p->last_wall_normal) > 0.99f && + 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). */ +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)) + return false; + + float head_on_yaw = atan2f(-b->wall_normal.x, -b->wall_normal.z); /* straight into the wall */ + float off_head_on = fabsf(wrap_angle(p->facing_yaw - head_on_yaw)); + return off_head_on < deg_to_rad(PLAYER_WALL_HANG_ANGLE_DEG); +} + +/* F. Grab the wall. The wall jump's direction is decided now, from how the + * player came in: mirror it off the wall, keeping the part along the wall + * and flipping the part into it. Coming in 5 degrees from the left leaves + * 5 degrees to the right. */ +static void start_wall_hang(Player *p) +{ + Vec3 n = p->body.wall_normal; + Vec3 in = vec3_make(sinf(p->facing_yaw), 0.0f, cosf(p->facing_yaw)); + Vec3 out = vec3_sub(in, vec3_scale(n, 2.0f * vec3_dot(in, n))); + p->wall_jump_yaw = atan2f(out.x, out.z); + + p->facing_yaw = p->wall_jump_yaw; /* face where the wall jump will go */ + p->forward_speed = 0.0f; + p->body.vel.y = 0.0f; /* catch on the wall; the slide builds from here */ + p->wall_time = 0.0f; + p->state = PLAYER_WALL_HANG; + p->heightened_movement = true; /* stays on for the whole hang */ +} + +/* F. The hang is over: drop straight down at normal gravity as a plain fall. + * heightened_movement ends, so the player can't grab on again before landing. */ +static void let_go_of_wall(Player *p) +{ + p->heightened_movement = false; + p->jump_kind = JUMP_NONE; + p->forward_speed = 0.0f; + p->state = PLAYER_FALL; +} + +/* F. Jump off the wall along wall_jump_yaw. Right after grabbing it's the + * high one, a second jump; later it's a normal jump at run speed. Each takes + * on its jump kind's height, top speed in the air and landing rules. Either + * way heightened_movement stays on for the whole jump, so ANY wall jump can + * grab the next wall. */ +static void start_wall_jump(Player *p) +{ + p->has_last_wall = true; + p->last_wall_normal = p->body.wall_normal; + p->last_wall_plane = wall_plane(p->body.wall, p->body.wall_normal); + p->facing_yaw = p->wall_jump_yaw; + p->heightened_movement = true; + + if (p->wall_time < PLAYER_WALL_JUMP_EARLY_TIME) { + p->forward_speed = PLAYER_SECOND_JUMP_MAX_SPEED; + leave_ground(p, PLAYER_SECOND_JUMP_VELOCITY, JUMP_SECOND); + } else { + p->forward_speed = PLAYER_MAX_SPEED; + leave_ground(p, PLAYER_JUMP_VELOCITY, JUMP_NORMAL); + } +} + +/* F. WALL_HANG: cling for a moment, sliding slowly down, and jump off. */ +static void update_wall_hang(Player *p, const Input *in, Wish wish, float dt) +{ + /* Slid all the way down to a floor: that's an ordinary landing. */ + if (p->body.on_ground) { + p->state = PLAYER_FALL; + update_fall(p, in, wish, dt); + return; + } + + if (in->jump_pressed) { + start_wall_jump(p); + return; + } + + /* Time's up, or we slid past the wall's bottom or end: let go. */ + p->wall_time += dt; + if (p->wall_time >= PLAYER_WALL_HANG_DURATION || !p->body.hit_wall || + !p->body.wall.allows_wall_hang) { + let_go_of_wall(p); + return; + } + + /* Gravity is off while hanging (see E); slide down slowly instead, building + * gently up to a capped speed. */ + p->body.vel.y = -fminf(PLAYER_WALL_SLIDE_ACCEL * p->wall_time, PLAYER_WALL_SLIDE_MAX_SPEED); +} + /* 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) @@ -424,12 +595,21 @@ static void update_fall(Player *p, const Input *in, Wish wish, float dt) update_ground(p, in, wish, dt); return; } + if (wants_wall_hang(p)) { + start_wall_hang(p); + 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) { + if (wants_wall_hang(p)) { + start_wall_hang(p); + return; + } + /* Past the apex (or landed already): it's a fall now. */ if (p->body.vel.y <= 0.0f) { p->state = PLAYER_FALL; @@ -466,6 +646,16 @@ void player_init(Player *p, Vec3 spawn) p->skid_time = 0.0f; p->skid_speed = 0.0f; p->skid_yaw = 0.0f; + p->wall_time = 0.0f; + p->wall_jump_yaw = 0.0f; + p->has_last_wall = false; + p->last_wall_normal = vec3_make(0.0f, 0.0f, 0.0f); + p->last_wall_plane = 0.0f; + p->body.gravity_scale = 1.0f; + p->body.ground_box = -1; + p->body.hit_wall = false; + p->body.wall_normal = vec3_make(0.0f, 0.0f, 0.0f); + p->body.wall = (Box){ { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 0.0f }, false, BOX_STYLE_PLATFORM }; } void player_reset(Player *p, Vec3 spawn) @@ -483,16 +673,28 @@ void player_update(Player *p, const Input *in, float camera_yaw, float dt) 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_WALL_HANG: update_wall_hang(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 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; + * moves where they face, except in a skid (they've already turned to + * the stick but keep sliding the way they were running) and a wall hang + * (they face where the wall jump will go, but press gently straight + * into the wall so physics keeps reporting it; physics has just + * reported that wall, so wall_normal is current). The hang drives its + * own slide, so gravity is switched off for it. */ + bool hanging = p->state == PLAYER_WALL_HANG; + float move_yaw = p->facing_yaw; + if (p->state == PLAYER_SKID) + move_yaw = p->skid_yaw; + else if (hanging) + move_yaw = atan2f(-p->body.wall_normal.x, -p->body.wall_normal.z); + float move_speed = hanging ? WALL_PRESS_SPEED : p->forward_speed; + p->body.vel.x = sinf(move_yaw) * move_speed; + p->body.vel.z = cosf(move_yaw) * move_speed; + p->body.gravity_scale = hanging ? 0.0f : 1.0f; physics_step(&p->body, dt); } @@ -504,6 +706,7 @@ const char *player_state_name(PlayerState s) case PLAYER_JUMP: return "JUMP"; case PLAYER_FALL: return "FALL"; case PLAYER_SKID: return "SKID"; + case PLAYER_WALL_HANG: return "WALL_HANG"; } return "?"; } |
