summaryrefslogtreecommitdiff
path: root/platformer/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--platformer/src/config.h12
-rw-r--r--platformer/src/level.c465
-rw-r--r--platformer/src/player.c71
-rw-r--r--platformer/src/player.h9
4 files changed, 328 insertions, 229 deletions
diff --git a/platformer/src/config.h b/platformer/src/config.h
index 35feb4c..86677ac 100644
--- a/platformer/src/config.h
+++ b/platformer/src/config.h
@@ -38,7 +38,7 @@
* PLAYER_MAX_SPEED 6.0f m/s at full stick tilt (kept modest on purpose:
* later moves will be the way to go faster)
* PLAYER_GROUND_ACCEL 8.4f m/s^2 speeding up toward target speed
- * PLAYER_GROUND_FRICTION 11.3f m/s^2 slowing down with no input (a short skid)
+ * PLAYER_GROUND_FRICTION 12.0f m/s^2 slowing down with no input (a short skid)
* PLAYER_TURN_RATE 12.0f rad/s max facing rotation on ground
* PLAYER_SNAP_TURN_DEG 90.0f degrees; from a standstill, stick pushes
* more than this away from facing turn
@@ -49,6 +49,9 @@
* PLAYER_AIR_ACCEL 9.0f m/s^2 speeding up toward target speed in air
* PLAYER_JUMP_VELOCITY 14.0f m/s upward at jump start (~2.4 m apex)
* PLAYER_JUMP_CUT 0.50f vel.y multiplier when A released while rising
+ * PLAYER_JUMP_BUFFER 0.1f s an A press is remembered: if a jump becomes
+ * possible within it (landing, grabbing a
+ * wall), it happens then
* PLAYER_JUMP_MAX_SPEED 7.0f m/s top horizontal speed during a normal
* jump (a little past the run speed)
* PLAYER_JUMP_BOOST_ACCEL 1.5f m/s^2 how slowly speed builds past the run
@@ -65,7 +68,7 @@
* 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
+ * PLAYER_SKID_ANGLE_DEG 130.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)
@@ -178,7 +181,7 @@
/* ---- Player: ground movement ------------------------------------------- */
#define PLAYER_MAX_SPEED 6.0f /* m/s at full stick tilt; higher = faster top run speed (kept modest: later moves go faster) */
#define PLAYER_GROUND_ACCEL 8.4f /* m/s^2 toward target speed; lower = heavier, slower starts */
-#define PLAYER_GROUND_FRICTION 11.3f /* m/s^2 slowdown with stick released; lower = longer skid to a stop */
+#define PLAYER_GROUND_FRICTION 12.0f /* m/s^2 slowdown with stick released; lower = longer skid to a stop */
#define PLAYER_TURN_RATE 12.0f /* rad/s max facing rotation on ground; higher = tighter turns */
#define PLAYER_SNAP_TURN_DEG 90.0f /* degrees; standing still, bigger turns than this are instant; lower = more snapping */
@@ -187,6 +190,7 @@
#define PLAYER_AIR_ACCEL 9.0f /* m/s^2 toward target speed in air; higher = more steerable jumps */
#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_JUMP_BUFFER 0.1f /* s an early A press is remembered (e.g. just before landing); higher = more forgiving */
#define PLAYER_JUMP_MAX_SPEED 7.0f /* m/s top horizontal speed in a normal jump; higher = bigger reward for jumping at full run */
#define PLAYER_JUMP_BOOST_ACCEL 1.5f /* m/s^2 build past run speed in a normal jump; higher = reaches PLAYER_JUMP_MAX_SPEED sooner */
#define PLAYER_OVERSPEED_DECEL 2.0f /* m/s^2 ease back down to run speed after landing (stick held); lower = keeps the extra speed longer */
@@ -196,7 +200,7 @@
#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_ANGLE_DEG 130.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 */
diff --git a/platformer/src/level.c b/platformer/src/level.c
index 2e3b862..12a0862 100644
--- a/platformer/src/level.c
+++ b/platformer/src/level.c
@@ -36,38 +36,44 @@
* is CLIFF_STRIP_COUNT strips whose north ends step
* down that diagonal. Rock, and HANGABLE: big terrain
* surfaces allow the wall hang; small platforms don't.
- * Rock pillars PILLAR_COUNT tall hangable pillars a few meters out
- * from the faces, so they form chimneys with the cliff.
+ * Rock pillars PILLAR_COUNT tall hangable pillars 4-6 m out from
+ * the faces, so they form chimneys with the cliff.
+ * Placed AFTER the route and never in its way.
+ * Height tiers every generated platform's top sits on a grid of
+ * TIER_HEIGHT (1.5 m) steps down from the cliff top, so
+ * one tier up is a single jump and two tiers up is a
+ * double (second) jump, and it's visible which is which.
* The climb a band that wraps the cliff: along the south face
* west to east, around the corner, north along the
* east face. Distance along it is `s`; distance out
- * from the face is `out`. On it:
- * - one ROUTE: big platforms (ROUTE_MIN_SIZE..MAX) placed
- * one hop after another (a normal jump or a second
- * jump, with the path between them clear) from the
- * ground to one hop below the top. It guarantees the
- * climb is possible but isn't marked: it wanders in
- * and out and takes switchbacks (hopping out to the
- * next row or back in instead of along). Hops in
- * toward the hangable face are normal jumps, so an
- * overshoot can't grab the face. Each row turns the
- * corner at its own spot (corner_s) so `s` is
- * continuous round the turn. The route paces itself
- * to arrive by ROUTE_ARRIVE_BY, stays beside the faces,
- * drifts in for its last hop, and when boxed in tries
- * easier hops, a scan of nearby spots, then (once) a
- * wider strip.
- * - the SCATTER: a few more platforms (SCATTER_COUNT) at
- * random spots and heights around the route: other ways up,
- * shortcuts, dead ends, and jumps that look possible
- * but aren't. That ambiguity is WANTED: the player
- * should have to judge which paths are feasible.
+ * from the face is `out`. Each row turns the corner at
+ * its own spot (corner_s), so `s` is continuous round
+ * the turn. On it:
+ * - one ROUTE of big platforms (4-6 m) from the ground to
+ * one tier below the top, ROUTE_MIN_OUT..ROUTE_WIDEST_OUT
+ * out from the face (not hugging it). Each step is a
+ * single jump (+1 tier), or now and then a double (+2),
+ * NEVER two doubles in a row, with the path between the
+ * two platforms clear. It isn't marked. It goes along
+ * the band or switches rows (out a row, or back in, at
+ * the same spot along the face), switching more when
+ * it's ahead of schedule so it arrives by
+ * ROUTE_ARRIVE_BY without running off the face. Hops in
+ * toward the face are singles; the last platforms drift
+ * in so the final hop is onto the top. When stuck it
+ * tries easier hops, then a scan of nearby spots.
+ * - the SCATTER: SCATTER_COUNT more platforms, each one
+ * single hop beside a route platform, level or one tier
+ * up or down: other ways up, shortcuts and dead ends.
+ * Some ambiguity is WANTED: the player should have to
+ * judge which paths work.
* The river overlapping water tiles along the diagonal, parallel
* to the cliff's third side.
- * The crossing the same idea (a route plus a couple of extras) on
- * the straight line from the
- * top's north-west corner over the river to the far
- * cliff (60 m, hangable), the finish.
+ * The crossing the same idea (a tiered route, mostly up or level,
+ * plus a couple of extras beside it) on the straight
+ * line from the top's north-west corner over the river
+ * to the far cliff (58 m: two tiers above the top,
+ * hangable), the finish.
* Some platforms move (ROUTE_MOVING_SHARE / SCATTER_MOVING_SHARE): they
* slide back and forth along the band, bob up and down, or loop round a
* flat or upright circle, each with its own period and phase. A moving
@@ -95,7 +101,7 @@
* DONE WHEN: The same seeds always build the same level, spawn is clear, the
* wall course is ahead of the starting view, and the cliff course
* behind it can be climbed and crossed with the moves in player.c,
- * using about 30-40 platforms in all.
+ * using about 30-40 platforms of 4-6 m, mostly single jumps apart.
* ============================================================
*/
@@ -274,8 +280,8 @@ static const Box WALL_COURSE[] = {
#define RIVER_TILE_COUNT 13
#define RIVER_DEPTH 0.05f /* m, the water is a thin sheet on the ground */
-/* The far cliff: the finish, 60 m up, across the river. */
-static const Box FAR_CLIFF = { { -70.0f, 0.0f, 80.0f }, { -32.0f, 60.0f, 98.0f }, true, BOX_STYLE_CLIFF };
+/* The far cliff: the finish, 58 m up (on the tier grid: two tiers above the top), across the river. */
+static const Box FAR_CLIFF = { { -70.0f, 0.0f, 80.0f }, { -32.0f, 58.0f, 98.0f }, true, BOX_STYLE_CLIFF };
/* The triangle cliff: strips side by side along the south face, each
* reaching one step further north than the last. Their row of north ends is
@@ -367,8 +373,8 @@ static bool is_clear(Box reach, bool moving)
* corner, and runs north along the east face. Each row (a distance `out` from
* the faces) turns the corner at its own spot, so s, the distance along that
* row, is continuous round the turn whether the row hugs the face or not. */
-#define BAND_START_X -34.0f /* m; the climb starts west of the south face */
-#define BAND_LENGTH 110.0f /* m along a row about 3 m out; rows further out run a bit longer */
+#define BAND_START_X -40.0f /* m; the climb starts west of the south face */
+#define BAND_LENGTH 116.0f /* m along a row about 3 m out; rows further out run a bit longer */
#define PLATFORM_THICKNESS 0.5f /* m, every generated platform is a slab this thick */
/* Where a row `out` from the faces (platforms `depth` deep) turns the corner. */
@@ -472,27 +478,40 @@ static bool try_add_platform(Box b, bool want_moving, Vec3 along, Vec3 out, bool
return true;
}
+/* ---- 3. The cliff course: height tiers --------------------------------- */
+
+/* Every generated platform's top sits on a grid of TIER_HEIGHT steps measured
+ * down from the cliff top. So a platform one tier up is a single jump, two
+ * tiers up is a double (second) jump, and the difference reads at a glance. */
+#define TIER_HEIGHT 1.5f /* m; a single jump's step (a full jump lifts the feet 2.33) */
+#define CLIMB_TOP (CLIFF_TOP - TIER_HEIGHT) /* the route's last platform: one tier below the top */
+
+/* The nearest height on the tier grid. */
+static float snap_to_tier(float y)
+{
+ return CLIFF_TOP - roundf((CLIFF_TOP - y) / TIER_HEIGHT) * TIER_HEIGHT;
+}
+
/* ---- 3. The cliff course: the route (one guaranteed way) --------------- */
/* How far apart two platforms may be for the route: a gap and a rise that a
* normal jump (or a second jump) makes comfortably. Checked by the headless
* course test with margin; these are deliberately cautious. */
-#define ROUTE_NORMAL_MAX_RISE 1.7f /* m; a full jump lifts the feet 2.33 */
-#define ROUTE_SECOND_MIN_RISE 2.0f /* m; second-jump steps rise at least this... */
-#define ROUTE_SECOND_MAX_RISE 2.9f /* m ...and at most this (it lifts the feet 4.08) */
#define ROUTE_SECOND_MAX_GAP 1.3f /* m; a second jump from standing covers less ground */
-#define ROUTE_MOVING_SHARE 0.4f /* chance a route platform moves (never two in a row) */
+#define ROUTE_DOUBLE_SHARE 0.3f /* usual chance a step is a double (two tiers); never two in a row */
+#define ROUTE_MOVING_SHARE 0.35f /* chance a route platform moves (never two in a row) */
#define ROUTE_TRIES 60 /* candidates per step before trying easier hops */
#define ROUTE_MAX 64
-#define ROUTE_WIDEST_OUT 9.0f /* m; how far out a boxed-in route may wander */
-#define ROUTE_MIN_SIZE 3.0f /* m; generated platforms are this big or bigger... */
-#define ROUTE_MAX_SIZE 4.5f /* m ...up to this: room to land and skid with the player's weight */
-#define ROUTE_SIDESTEP_SHARE 0.35f /* chance a route step switches rows (out/in) instead of going along */
+#define ROUTE_MIN_OUT 1.5f /* m; route platforms keep at least this far from the face... */
+#define ROUTE_WIDEST_OUT 13.0f /* m ...and at most this far (the last one comes in close) */
+#define ROUTE_MIN_SIZE 4.0f /* m; generated platforms are this big or bigger... */
+#define ROUTE_MAX_SIZE 6.0f /* m ...up to this: room to land and skid with the player's weight */
+#define ROUTE_SIDESTEP_SHARE 0.4f /* chance a route step switches rows (out/in) instead of going along */
#define HOP_HALF_WIDTH 0.45f /* m; the player's half width, plus a little, for the clear-path check */
#define HOP_CLEARANCE 2.2f /* m above the higher platform the hop's path must be open (body + arc) */
-#define ROUTE_ARRIVE_BY 80.0f /* m along the band the route aims to reach the top by (the face ends ~104) */
+#define ROUTE_ARRIVE_BY 86.0f /* m along the band the route aims to reach the top by (the face ends ~110) */
#define ROUTE_PULL_IN_HEIGHT 12.0f /* m below the top where the route starts drifting in to the face */
-#define CLIMB_TOP (CLIFF_TOP - 1.2f) /* the route's last platform: a hop below the top */
+#define ROUTE_SCHEDULE_SLACK 8.0f /* m ahead of schedule along the band that adds 40% to the row-switch chance */
/* How far a normal jump comfortably clears for a given rise. */
static float normal_max_gap(float rise)
@@ -500,29 +519,19 @@ static float normal_max_gap(float rise)
return 2.4f - 0.5f * fmaxf(rise, 0.0f);
}
-/* The route's height at each s, for placing the scatter around it. */
-static float s_route_s[ROUTE_MAX], s_route_top[ROUTE_MAX];
-static int s_route_count;
-
-static float route_height_at(float s)
-{
- if (s_route_count == 0 || s <= s_route_s[0])
- return 0.0f;
- for (int i = 1; i < s_route_count; i++) {
- if (s <= s_route_s[i]) {
- float t = (s - s_route_s[i - 1]) / (s_route_s[i] - s_route_s[i - 1]);
- return s_route_top[i - 1] + t * (s_route_top[i] - s_route_top[i - 1]);
- }
- }
- return s_route_top[s_route_count - 1];
-}
+/* The route's platforms, so the scatter can sit beside them. */
+typedef struct {
+ float s, out, along, depth; /* where it is on the band, and its size */
+ Box box;
+} RouteStep;
+static RouteStep s_route[ROUTE_MAX];
+static int s_route_count;
-static void remember_route_point(float s, float top)
+static void remember_route_step(float s, float out, float along, float depth, Box box)
{
if (s_route_count < ROUTE_MAX) {
- s_route_s[s_route_count] = s;
- s_route_top[s_route_count] = top;
- s_route_count++;
+ RouteStep step = { s, out, along, depth, box };
+ s_route[s_route_count++] = step;
}
}
@@ -583,139 +592,144 @@ typedef struct {
float depth; /* last platform's depth (out from the face) */
Box last; /* the last platform placed */
bool last_moving;
+ bool last_double; /* the last step was a double: the next one isn't */
float out_min, out_max; /* the strip of the band this route wanders in */
- bool remember; /* record its heights for placing the scatter */
} RouteWalk;
-/* Tries one random next platform for the route. `easy` asks for a short,
- * low hop close to the last platform: the fallback when normal picks keep
- * failing (at the corner, or next to a pillar). */
-static bool try_route_step(RouteWalk *w, float second_share, bool easy, float skip)
-{
- bool second = w->top > 0.0f && !easy && random_chance(second_share);
- float rise = second ? random_range(ROUTE_SECOND_MIN_RISE, ROUTE_SECOND_MAX_RISE)
- : easy ? random_range(0.2f, 1.0f)
- : random_range(0.9f, ROUTE_NORMAL_MAX_RISE);
- rise = fminf(rise, CLIMB_TOP - w->top);
- bool last = w->top + rise >= CLIMB_TOP - 0.01f;
- float gap = easy ? random_range(0.3f, 0.9f)
- : second ? random_range(0.6f, ROUTE_SECOND_MAX_GAP)
- : random_range(0.6f, normal_max_gap(rise));
+/* Places the route's next platform if it fits and is a comfortable hop. */
+static bool place_route_step(RouteWalk *w, float s, float out, float along, float depth,
+ float top, bool double_jump, bool may_move)
+{
+ Box b = band_box(s, out, along, depth, top - PLATFORM_THICKNESS, top);
+ if (past_the_faces(b) || (w->top > 0.0f && !route_hop_ok(w->last, b, double_jump)))
+ return false;
+ bool moving;
+ if (!try_add_platform(b, may_move, band_along(s, out, depth), band_out(s, out, depth), &moving))
+ return false;
+ w->s = s; w->top = top; w->out = out; w->along = along; w->depth = depth;
+ w->last = b; w->last_moving = moving; w->last_double = double_jump;
+ remember_route_step(s, out, along, depth, b);
+ return true;
+}
+
+/* Tries one random next platform for the route: one tier up (a single jump)
+ * or, now and then, two tiers up (a double jump, never twice in a row).
+ * `easy` asks for a short, level or one-tier hop close to the last platform:
+ * the fallback when normal picks keep failing. */
+static bool try_route_step(RouteWalk *w, float double_share, float sidestep_share, bool easy, float skip)
+{
+ bool double_jump = w->top > 0.0f && !easy && !w->last_double && random_chance(double_share);
+ int tiers = double_jump ? 2 : (easy && random_chance(0.5f)) ? 0 : 1;
+ float top = w->top > 0.0f ? fminf(w->top + (float)tiers * TIER_HEIGHT, CLIMB_TOP)
+ : snap_to_tier(TIER_HEIGHT); /* the first platform, up from the ground */
+ float rise = top - w->top;
+ bool last = top >= CLIMB_TOP - 0.01f;
+ float gap = easy ? random_range(0.4f, 0.9f)
+ : double_jump ? random_range(0.6f, ROUTE_SECOND_MAX_GAP)
+ : random_range(0.8f, normal_max_gap(rise));
float along = random_range(ROUTE_MIN_SIZE, ROUTE_MAX_SIZE);
float depth = random_range(ROUTE_MIN_SIZE, ROUTE_MAX_SIZE);
float out, s;
bool pulling_in = CLIMB_TOP - w->top < ROUTE_PULL_IN_HEIGHT;
- if (!last && !easy && random_chance(pulling_in ? 0.6f : ROUTE_SIDESTEP_SHARE)) {
+ bool sidestep = !easy && random_chance(pulling_in ? 0.6f : sidestep_share);
+ if (!last && sidestep) {
/* A switchback: hop out to the next row (or back in) instead of along,
* which climbs without using up the band. Near the top, always in. */
- bool outward = !pulling_in &&
- (w->out < 1.0f || (w->out + w->depth + gap + depth < w->out_max && random_chance(0.5f)));
- out = outward ? w->out + w->depth + gap : w->out - gap - depth;
- s = w->s + random_range(-1.0f, 1.0f);
- if (out < 0.0f || out > ROUTE_WIDEST_OUT)
+ /* Coming in toward the face, size the platform to fit the space left. */
+ float in_depth = fminf(depth, w->out - gap - ROUTE_MIN_OUT);
+ float out_row = w->out + w->depth + gap; /* the row further out... */
+ float in_row = w->out - gap - in_depth; /* ...or the row further in */
+ bool can_out = !pulling_in && out_row <= w->out_max;
+ bool can_in = in_depth >= ROUTE_MIN_SIZE && in_row >= w->out_min;
+ if (!can_out && !can_in)
return false;
- /* Hopping in toward the (hangable) face is a normal jump: a second
- * jump keeps heightened_movement on, so an overshoot would grab the
- * face instead of landing. */
- if (!outward && second)
+ bool outward = can_out && (!can_in || random_chance(0.5f));
+ out = outward ? out_row : in_row;
+ if (!outward)
+ depth = in_depth;
+ /* Same spot along the face (give or take a meter), in the new row.
+ * Each row turns the corner at its own s, so convert through the
+ * position along the face rather than reusing s. */
+ bool east = w->s >= corner_s(w->out, w->depth);
+ float spot = (east ? 0.5f * (w->last.min.z + w->last.max.z)
+ : 0.5f * (w->last.min.x + w->last.max.x)) + random_range(-1.0f, 1.0f);
+ s = east ? corner_s(out, depth) + spot - (CLIFF_SOUTH_Z - out - 0.5f * depth)
+ : spot - BAND_START_X;
+ /* Hopping in toward the (hangable) face is a single jump: it's lower
+ * and shorter than a double, so it's less likely to overshoot the
+ * ledge into the face. */
+ if (!outward && double_jump)
return false;
} else {
- /* Along the band, wandering in and out; the last platform hugs the face
- * so the top is a hop away. */
- out = last ? random_range(0.0f, 0.6f)
- : clampf(w->out + random_range(easy ? -1.0f : -2.5f, easy ? 1.0f : 2.5f), w->out_min, w->out_max);
+ /* Along the band, wandering in and out; the last platform comes in
+ * close to the face so the top is one hop away. */
+ out = last ? random_range(0.0f, 0.8f)
+ : clampf(w->out + random_range(easy ? -1.0f : -2.0f, easy ? 1.0f : 2.0f), w->out_min, w->out_max);
s = w->s + 0.5f * w->along + gap + 0.5f * along + skip;
}
- float top = w->top + rise;
- Box b = band_box(s, out, along, depth, top - PLATFORM_THICKNESS, top);
-
- if (past_the_faces(b) || (w->top > 0.0f && !route_hop_ok(w->last, b, second)))
- return false;
- bool moving;
- bool want_moving = !w->last_moving && !last && !easy && random_chance(ROUTE_MOVING_SHARE);
- if (!try_add_platform(b, want_moving, band_along(s, out, depth), band_out(s, out, depth), &moving))
- return false;
- w->s = s; w->top = top; w->out = out; w->along = along; w->depth = depth;
- w->last = b; w->last_moving = moving;
- if (w->remember)
- remember_route_point(s, top);
- return true;
+ bool may_move = !w->last_moving && !last && !easy && random_chance(ROUTE_MOVING_SHARE);
+ return place_route_step(w, s, out, along, depth, top, double_jump, may_move);
}
/* The last resort when random picks keep failing: scan nearby spots in order
- * (every row out from the face, a range of distances along the band, a few
- * rises) and take the first that is a comfortable hop and fits. */
+ * (every row out from the face, a range of distances along the band, one
+ * tier up or level) and take the first that is a comfortable hop and fits. */
static bool rescue_route_step(RouteWalk *w)
{
- static const float RISES[] = { 1.4f, 1.0f, 0.6f, 0.3f };
float size = 0.5f * (ROUTE_MIN_SIZE + ROUTE_MAX_SIZE);
- for (int r = 0; r < 4; r++) {
- float rise = fminf(RISES[r], CLIMB_TOP - w->top);
- bool last = w->top + rise >= CLIMB_TOP - 0.01f;
- for (float ds = -3.0f; ds <= 7.0f; ds += 1.0f) {
- for (float out = 0.0f; out <= (last ? 0.6f : ROUTE_WIDEST_OUT); out += (last ? 0.3f : 1.0f)) {
- float s = w->s + ds, top = w->top + rise;
- Box b = band_box(s, out, size, size, top - PLATFORM_THICKNESS, top);
- bool moving;
- if (past_the_faces(b) || !route_hop_ok(w->last, b, false) ||
- !try_add_platform(b, false, band_along(s, out, size), band_out(s, out, size), &moving))
- continue;
- w->s = s; w->top = top; w->out = out; w->along = size; w->depth = size;
- w->last = b; w->last_moving = false;
- if (w->remember)
- remember_route_point(s, top);
- return true;
- }
- }
+ for (int tiers = 1; tiers >= 0; tiers--) {
+ float top = w->top > 0.0f ? fminf(w->top + (float)tiers * TIER_HEIGHT, CLIMB_TOP)
+ : snap_to_tier(TIER_HEIGHT);
+ bool last = top >= CLIMB_TOP - 0.01f;
+ for (float ds = -4.0f; ds <= 8.0f; ds += 1.0f)
+ for (float out = last ? 0.0f : w->out_min; out <= (last ? 0.8f : ROUTE_WIDEST_OUT);
+ out += (last ? 0.4f : 1.0f))
+ if (place_route_step(w, w->s + ds, out, size, size, top, false, false))
+ return true;
}
return false;
}
-/* Walks the band from the ground to one hop below the top, placing one
- * platform per hop, wandering between out_min and out_max from the face.
- * Steps are mostly normal-jump rises, with second-jump rises mixed in as
- * often as the remaining height needs. When a step can't be placed, easier
- * hops are tried, then easy hops a little further along, then a scan of
- * nearby spots; the route never leaves a gap it can't cross. */
-static void add_climb_route(float out_min, float out_max, bool remember)
-{
- RouteWalk w = { 0.0f, 0.0f, out_min, 0.0f, 0.0f, band_box(0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f), false,
- out_min, out_max, remember };
- bool widened = false;
- if (remember) {
- s_route_count = 0;
- remember_route_point(0.0f, 0.0f);
- }
+/* Walks the band from the ground to one tier below the top, placing one
+ * platform per hop, ROUTE_MIN_OUT..ROUTE_WIDEST_OUT out from the face. Steps
+ * are mostly single jumps (one tier), with doubles (two tiers) mixed in as
+ * the remaining height needs, never two doubles in a row. When a step can't
+ * be placed, easier hops are tried, then easy hops a little further along,
+ * then a scan of nearby spots; the route never leaves a gap it can't cross. */
+static void add_climb_route(void)
+{
+ RouteWalk w = { 0.0f, 0.0f, ROUTE_MIN_OUT, 0.0f, 0.0f,
+ band_box(0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f), false, false,
+ ROUTE_MIN_OUT, ROUTE_WIDEST_OUT };
+ s_route_count = 0;
while (w.top < CLIMB_TOP - 0.01f) {
- /* Mix in more second jumps when there's a lot of height left per meter,
+ /* More doubles when there's a lot of height left per meter of band,
* aiming to arrive well before the east face ends. */
- float needed = (CLIMB_TOP - w.top) / fmaxf(ROUTE_ARRIVE_BY - w.s, 4.0f) * 3.8f;
+ float needed = (CLIMB_TOP - w.top) / fmaxf(ROUTE_ARRIVE_BY - w.s, 4.0f) * 4.0f;
+ float double_share = clampf((needed - TIER_HEIGHT) / TIER_HEIGHT, ROUTE_DOUBLE_SHARE, 0.45f);
+ /* Row switches climb without moving along the band. The route should
+ * be about this far along for its height: ahead of that, switch rows
+ * more; behind it, go along more. */
+ float on_schedule = ROUTE_ARRIVE_BY * w.top / CLIMB_TOP;
+ float ahead = w.s - on_schedule;
+ float sidestep_share = clampf(ROUTE_SIDESTEP_SHARE + ahead / ROUTE_SCHEDULE_SLACK * 0.4f, 0.15f, 0.85f);
/* Near the top, drift in toward the face: the last hop is onto the top. */
if (CLIMB_TOP - w.top < ROUTE_PULL_IN_HEIGHT) {
w.out_min = 0.0f;
- w.out_max = fmaxf(0.6f, w.out_max - 2.0f);
+ w.out_max = fmaxf(0.8f, w.out_max - 3.0f);
w.out = fminf(w.out, w.out_max);
}
- float second_share = clampf((needed - 1.2f) / 1.3f, 0.15f, 0.9f);
- bool placed = false;
+ bool placed = false;
for (int tries = 0; tries < ROUTE_TRIES && !placed; tries++)
- placed = try_route_step(&w, second_share, false, 0.0f);
+ placed = try_route_step(&w, double_share, sidestep_share, false, 0.0f);
for (int tries = 0; tries < ROUTE_TRIES && !placed; tries++)
- placed = try_route_step(&w, second_share, true, 0.0f);
+ placed = try_route_step(&w, double_share, sidestep_share, true, 0.0f);
for (float skip = 0.5f; skip <= 3.0f && !placed; skip += 0.5f)
for (int tries = 0; tries < ROUTE_TRIES && !placed; tries++)
- placed = try_route_step(&w, second_share, true, skip);
+ placed = try_route_step(&w, double_share, sidestep_share, true, skip);
if (!placed)
placed = rescue_route_step(&w);
- if (!placed && !widened) {
- /* Boxed in (by pillars or the other route): wander wider from here
- * on (once; near the top the pull-in narrows it again). */
- widened = true;
- w.out_min = 0.0f;
- w.out_max = ROUTE_WIDEST_OUT;
- continue;
- }
if (!placed)
return; /* truly stuck: the course test reports the top as unreachable */
}
@@ -724,39 +738,64 @@ static void add_climb_route(float out_min, float out_max, bool remember)
/* ---- 3. The cliff course: pillars and scatter -------------------------- */
#define PILLAR_COUNT 7
-#define SCATTER_COUNT 5
-#define SCATTER_MOVING_SHARE 0.8f
-#define SCATTER_TRIES 6000
+#define SCATTER_COUNT 6
+#define SCATTER_MOVING_SHARE 0.6f
+#define SCATTER_TRIES 400
+
+/* Does every hop along the climb route still have a clear path? */
+static bool route_paths_clear(void)
+{
+ for (int i = 1; i < s_route_count; i++)
+ if (!hop_path_clear(s_route[i - 1].box, s_route[i].box))
+ return false;
+ return true;
+}
/* Tall hangable rock pillars a few meters out from the face: each makes a
- * chimney with the cliff for wall jumps, and its top is a place to stand. */
+ * chimney with the cliff for wall jumps, and its top is a place to stand.
+ * They're fitted in AFTER the route, so they never block it: a pillar that
+ * would stand in the way of one of the route's hops is taken out again. */
static void add_rock_pillars(void)
{
- for (int i = 0, tries = 0; i < PILLAR_COUNT && tries < 200; tries++) {
+ for (int i = 0, tries = 0; i < PILLAR_COUNT && tries < 300; tries++) {
float s = random_range(8.0f, BAND_LENGTH - 8.0f);
float top = CLIFF_TOP * s / BAND_LENGTH + random_range(0.0f, 4.0f);
- Box b = band_box(s, random_range(3.5f, 5.0f), random_range(2.0f, 3.5f),
+ Box b = band_box(s, random_range(4.0f, 6.0f), random_range(2.0f, 3.5f),
random_range(2.0f, 3.0f), 0.0f, top);
b.allows_wall_hang = true;
b.style = BOX_STYLE_CLIFF;
- if (is_clear(b, false)) {
- add_box(b, no_motion());
+ if (!is_clear(b, false))
+ continue;
+ add_box(b, no_motion());
+ if (route_paths_clear())
i++;
- }
+ else
+ s_count--; /* it blocked a hop: take it back out */
}
}
-/* Extra platforms at random spots and heights around the route: other ways
- * up, shortcuts, and dead ends. */
+/* Extra platforms beside the route: each sits a single hop from a route
+ * platform (along the band, or out a row), usually level with it or one tier
+ * up or down. They're other ways up, shortcuts and dead ends, and most are
+ * reachable with a single jump. */
static void add_scatter(void)
{
- for (int i = 0, tries = 0; i < SCATTER_COUNT && tries < SCATTER_TRIES; tries++) {
- float s = random_range(0.0f, BAND_LENGTH);
- float top = clampf(route_height_at(s) + random_range(-3.0f, 2.5f), 0.8f, CLIFF_TOP - 0.5f);
- float out = random_range(0.0f, 10.0f);
+ for (int i = 0, tries = 0; i < SCATTER_COUNT && tries < SCATTER_TRIES && s_route_count > 1; tries++) {
+ RouteStep r = s_route[1 + (int)(next_random() % (uint32_t)(s_route_count - 1))];
+ int tiers = (int)(next_random() % 3) - 1; /* -1, 0 or +1 */
+ float top = r.box.max.y + (float)tiers * TIER_HEIGHT;
+ float gap = random_range(0.8f, 1.5f);
+ float along = random_range(ROUTE_MIN_SIZE, ROUTE_MAX_SIZE);
float depth = random_range(ROUTE_MIN_SIZE, ROUTE_MAX_SIZE);
- Box b = band_box(s, out, random_range(ROUTE_MIN_SIZE, ROUTE_MAX_SIZE), depth,
- top - PLATFORM_THICKNESS, top);
+ float s = r.s, out = r.out;
+ switch (next_random() % 3) {
+ case 0: s = r.s + 0.5f * (r.along + along) + gap; break; /* further along */
+ case 1: s = r.s - 0.5f * (r.along + along) - gap; break; /* back along */
+ default: out = r.out + r.depth + gap; break; /* out a row */
+ }
+ if (top < 1.0f || top > CLIMB_TOP || out < ROUTE_MIN_OUT || out > ROUTE_WIDEST_OUT)
+ continue;
+ Box b = band_box(s, out, along, depth, top - PLATFORM_THICKNESS, top);
bool moving;
if (try_add_platform(b, random_chance(SCATTER_MOVING_SHARE), band_along(s, out, depth),
band_out(s, out, depth), &moving))
@@ -771,7 +810,8 @@ static void add_scatter(void)
#define CROSS_END_X -32.0f /* m ...to the far cliff's nearest corner */
#define CROSS_END_Z 80.0f
#define CROSS_SCATTER 2
-#define CROSS_MOVING_SHARE 0.6f
+#define CROSS_MOVING_SHARE 0.5f
+#define CROSS_ROUTE_MAX 16
/* A square platform centered at (x, z) with the given top. */
static Box square_platform(float x, float z, float size, float top)
@@ -781,61 +821,74 @@ static Box square_platform(float x, float z, float size, float top)
return b;
}
-/* One guaranteed way across: hop by hop along the crossing line, keeping
- * between side_min and side_max of it, until the far cliff is one hop away.
- * Like the climb, it falls back to shorter hops rather than leaving a gap. */
-static void add_crossing_route(Vec3 start, Vec3 along, Vec3 side, float side_min, float side_max)
+/* The crossing's route: hop by hop along the line from the top's corner to
+ * the far cliff, on the tier grid (level, one tier up or down, or now and
+ * then a double), until the far cliff is one single hop away. Remembers its
+ * platforms in `route` for the extras. Returns how many it placed. */
+static int add_crossing_route(Vec3 start, Vec3 along, Vec3 side, Box *route)
{
Box prev = square_platform(CROSS_START_X + 2.5f, CROSS_START_Z - 2.5f, 5.0f, CLIFF_TOP); /* the corner of the top */
float t = 0.0f;
- bool prev_moving = false;
+ bool prev_moving = false, prev_double = false;
+ int placed_count = 0;
- for (int step = 0; step < 20; step++) {
- if (footprint_gap(prev, FAR_CLIFF) <= normal_max_gap(FAR_CLIFF.max.y - prev.max.y))
- return;
+ for (int step = 0; step < CROSS_ROUTE_MAX; step++) {
+ float to_finish = FAR_CLIFF.max.y - prev.max.y;
+ if (to_finish <= TIER_HEIGHT + 0.01f && footprint_gap(prev, FAR_CLIFF) <= normal_max_gap(to_finish))
+ return placed_count;
bool placed = false;
for (int tries = 0; tries < 2 * ROUTE_TRIES && !placed; tries++) {
- bool easy = tries >= ROUTE_TRIES; /* second half: short, low hops */
- bool second = !easy && random_chance(0.3f);
- float rise = second ? random_range(ROUTE_SECOND_MIN_RISE, 2.6f)
- : easy ? random_range(-0.5f, 0.8f) : random_range(-1.0f, 1.2f);
- float top = clampf(prev.max.y + rise, CLIFF_TOP, FAR_CLIFF.max.y - 0.5f);
- float size = random_range(ROUTE_MIN_SIZE + 0.2f, ROUTE_MAX_SIZE);
- float next_t = t + (easy ? random_range(2.5f, 4.0f) : random_range(4.0f, 5.5f));
+ bool easy = tries >= ROUTE_TRIES; /* second half: short hops */
+ bool double_jump = !easy && !prev_double && random_chance(0.25f);
+ /* Mostly up or level while there's height to gain, now and then down. */
+ float roll = random_range(0.0f, 1.0f);
+ int tiers = double_jump ? 2 : to_finish <= 0.01f ? (roll < 0.5f ? 0 : -1)
+ : roll < 0.55f ? 1 : roll < 0.9f ? 0 : -1;
+ float top = clampf(prev.max.y + (float)tiers * TIER_HEIGHT,
+ CLIFF_TOP - TIER_HEIGHT, FAR_CLIFF.max.y);
+ float size = random_range(ROUTE_MIN_SIZE, ROUTE_MAX_SIZE);
+ float next_t = t + (easy ? random_range(4.0f, 5.5f) : random_range(5.5f, 7.0f));
Vec3 c = vec3_add(vec3_add(start, vec3_scale(along, next_t)),
- vec3_scale(side, random_range(side_min, side_max)));
+ vec3_scale(side, random_range(-4.0f, 4.0f)));
Box b = square_platform(c.x, c.z, size, top);
- if (!route_hop_ok(prev, b, second && top - prev.max.y >= ROUTE_SECOND_MIN_RISE))
+ if (!route_hop_ok(prev, b, double_jump))
continue;
bool moving;
bool want_moving = !prev_moving && !easy && random_chance(CROSS_MOVING_SHARE);
if (!try_add_platform(b, want_moving, along, side, &moving))
continue;
- t = next_t; prev = b; prev_moving = moving;
+ t = next_t; prev = b; prev_moving = moving; prev_double = double_jump;
+ route[placed_count++] = b;
placed = true;
}
if (!placed)
- return; /* stuck: the course test reports the finish as unreachable */
+ return placed_count; /* stuck: the course test reports the finish as unreachable */
}
+ return placed_count;
}
/* The same idea as the climb on the straight line from the top's corner to
- * the far cliff: a guaranteed way across, plus a couple of extras. */
+ * the far cliff: a guaranteed way across, plus a couple of extras beside it. */
static void add_crossing(void)
{
Vec3 start = vec3_make(CROSS_START_X, 0.0f, CROSS_START_Z);
Vec3 line = vec3_sub(vec3_make(CROSS_END_X, 0.0f, CROSS_END_Z), start);
- float length = vec3_length(line);
- Vec3 along = vec3_scale(line, 1.0f / length);
+ Vec3 along = vec3_scale(line, 1.0f / vec3_length(line));
Vec3 side = vec3_make(-along.z, 0.0f, along.x);
-
- add_crossing_route(start, along, side, -3.0f, 3.0f);
-
- /* The scatter: more platforms either side of the line. */
- for (int i = 0, tries = 0; i < CROSS_SCATTER && tries < 300; tries++) {
- Vec3 c = vec3_add(vec3_add(start, vec3_scale(along, random_range(2.0f, length))),
- vec3_scale(side, random_range(-7.0f, 7.0f)));
- Box b = square_platform(c.x, c.z, random_range(ROUTE_MIN_SIZE, ROUTE_MAX_SIZE), random_range(54.5f, 61.0f));
+ Box route[CROSS_ROUTE_MAX];
+ int route_count = add_crossing_route(start, along, side, route);
+
+ /* The extras: a single hop to one side of a crossing platform, level with
+ * it or one tier up or down. */
+ for (int i = 0, tries = 0; i < CROSS_SCATTER && tries < 200 && route_count > 0; tries++) {
+ Box r = route[next_random() % (uint32_t)route_count];
+ float size = random_range(ROUTE_MIN_SIZE, ROUTE_MAX_SIZE);
+ float dir = random_chance(0.5f) ? 1.0f : -1.0f;
+ float gap = random_range(0.8f, 1.5f);
+ Vec3 c = vec3_add(vec3_make(0.5f * (r.min.x + r.max.x), 0.0f, 0.5f * (r.min.z + r.max.z)),
+ vec3_scale(side, dir * (0.5f * (r.max.x - r.min.x) + gap + 0.5f * size)));
+ float top = r.max.y + (float)((int)(next_random() % 3) - 1) * TIER_HEIGHT;
+ Box b = square_platform(c.x, c.z, size, top);
bool moving;
if (try_add_platform(b, random_chance(CROSS_MOVING_SHARE), along, side, &moving))
i++;
@@ -858,8 +911,8 @@ int level_build(LevelBox *out, int capacity)
add_triangle_cliff();
add_box(FAR_CLIFF, no_motion());
add_river();
+ add_climb_route();
add_rock_pillars();
- add_climb_route(0.0f, ROUTE_WIDEST_OUT, true);
add_scatter();
add_crossing();
return s_count;
diff --git a/platformer/src/player.c b/platformer/src/player.c
index 737a4c9..9f0e822 100644
--- a/platformer/src/player.c
+++ b/platformer/src/player.c
@@ -15,7 +15,7 @@
* - 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,
+ * heightened_movement = false, heightened_timer = 0, jump_buffer = 0,
* retreating_movement = false, jump_kind = JUMP_NONE, skid_time = 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,
@@ -74,7 +74,15 @@
* 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:
+ * C. Jump. An A press sets jump_buffer = PLAYER_JUMP_BUFFER; it counts down
+ * every tick. While it's > 0, the first moment a jump is possible, it
+ * happens and the buffer is spent (take_jump). On the ground that's
+ * the press itself; pressed just before landing, it's the landing tick
+ * (so an early press still gets the second jump); pressed just before
+ * grabbing a wall, it's the grab (an early wall jump). WHY: players
+ * press a hair early while still falling and felt their press eaten.
+ * Holding A still decides hop vs. full jump.
+ * Jumps start from IDLE / RUN / SKID on the 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 =
@@ -87,7 +95,8 @@
* 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_kind = JUMP_NORMAL.
+ * jump_kind = JUMP_NORMAL. heightened_movement stays OFF, so a
+ * first jump can't grab walls (tried; it made wall hangs too easy).
*
* C2. Landing: landing from a normal jump (JUMP_NORMAL, short hop or
* full) or a retreat jump (JUMP_RETREAT) turns heightened_movement on
@@ -135,13 +144,17 @@
* (walked off a platform edge) -> FALL.
*
* 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
+ * heightened_movement is on (second, retreat or wall jump); 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
+ * (facing_yaw vs. -wall_normal); and the player WOULDN'T CLEAR the
+ * wall's top anyway (their jump's apex, pos.y + vel.y^2 / 2g, is below
+ * it). WHY that last rule: hopping onto a ledge from just below its
+ * lip with a heightened jump would otherwise grab the face instead of
+ * landing on top. 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
@@ -154,7 +167,7 @@
* - 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
+ * - A (take_jump): 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,
@@ -181,7 +194,7 @@
* No magic numbers; everything comes from config.h.
*
* NOT IN SCOPE: Triple jump, long jump, wall kick, ground pound, skids
- * below full run speed, slopes, coyote time, jump buffering.
+ * below full run speed, slopes, coyote time.
* Leave a short comment at the state enum pointing to the
* README roadmap as the place to grow.
*
@@ -289,10 +302,21 @@ static void leave_ground(Player *p, float vel_y, JumpKind kind)
p->state = PLAYER_JUMP;
}
+/* C. Is there an A press waiting (pressed within PLAYER_JUMP_BUFFER)? If so,
+ * it's used up now: call this only where a jump can actually start. */
+static bool take_jump(Player *p)
+{
+ if (p->jump_buffer <= 0.0f)
+ return false;
+ p->jump_buffer = 0.0f;
+ return true;
+}
+
/* 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. */
+ * second jump. A plain first jump does NOT turn it on, so it can't grab
+ * walls: wall hangs off a single jump made them too easy. */
static void start_jump(Player *p)
{
if (p->heightened_movement)
@@ -389,7 +413,7 @@ static void update_ground(Player *p, const Input *in, Wish wish, float dt)
}
p->state = grounded_state(p);
- if (in->jump_pressed)
+ if (take_jump(p))
start_jump(p);
}
@@ -414,7 +438,7 @@ static void update_skid(Player *p, const Input *in, Wish wish, float dt)
if (wish.mag > 0.0f)
p->facing_yaw = wish.yaw;
- if (in->jump_pressed) {
+ if (take_jump(p)) {
start_retreat_jump(p);
return;
}
@@ -456,17 +480,28 @@ static bool same_wall_as_last(const Player *p)
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). */
+/* F. Will this jump carry the feet above the wall's top anyway? Then the
+ * player is hopping ONTO it (a ledge just above), not clinging to it. */
+static bool would_clear_top(const Player *p)
+{
+ const Body *b = &p->body;
+ float apex = b->pos.y;
+ if (b->vel.y > 0.0f)
+ apex += b->vel.y * b->vel.y / (2.0f * GRAVITY);
+ return apex > b->wall.max.y;
+}
+
+/* F. Can the player grab the wall they just hit? Only in a heightened move
+ * (second, retreat or wall jump), only on walls the level marks as hangable, not the wall they
+ * just jumped off, not one they're about to clear, 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))
+ if (same_wall_as_last(p) || would_clear_top(p))
return false;
float head_on_yaw = atan2f(-b->wall_normal.x, -b->wall_normal.z); /* straight into the wall */
@@ -535,7 +570,7 @@ static void update_wall_hang(Player *p, const Input *in, Wish wish, float dt)
return;
}
- if (in->jump_pressed) {
+ if (take_jump(p)) {
start_wall_jump(p);
return;
}
@@ -646,6 +681,7 @@ void player_init(Player *p, Vec3 spawn)
p->skid_time = 0.0f;
p->skid_speed = 0.0f;
p->skid_yaw = 0.0f;
+ p->jump_buffer = 0.0f;
p->wall_time = 0.0f;
p->wall_jump_yaw = 0.0f;
p->has_last_wall = false;
@@ -667,6 +703,8 @@ 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);
+ if (in->jump_pressed)
+ p->jump_buffer = PLAYER_JUMP_BUFFER; /* C: remembered until a jump can happen */
/* IDLE and RUN share one helper: they differ only in speed. */
switch (p->state) {
@@ -677,6 +715,7 @@ void player_update(Player *p, const Input *in, float camera_yaw, float dt)
case PLAYER_JUMP: update_jump(p, in, wish, dt); break;
case PLAYER_FALL: update_fall(p, in, wish, dt); break;
}
+ p->jump_buffer = fmaxf(p->jump_buffer - dt, 0.0f);
/* 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
diff --git a/platformer/src/player.h b/platformer/src/player.h
index e6fd033..035a344 100644
--- a/platformer/src/player.h
+++ b/platformer/src/player.h
@@ -37,8 +37,8 @@
* PlayerState state;
* bool heightened_movement; on for PLAYER_SECOND_JUMP_WINDOW s after
* landing a normal or retreat jump, and for the
- * whole second jump, retreat jump, wall hang
- * and every wall jump. On the ground, A gives the second
+ * whole second jump, retreat jump, wall hang and
+ * every wall jump (NOT a plain first jump). On the ground, A gives the second
* jump; in the air, it lets the player wall hang.
* float heightened_timer; s left before heightened_movement ends
* (not counted down during the second jump)
@@ -51,6 +51,8 @@
* 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)
+ * float jump_buffer; s left on the last A press: while > 0, the first
+ * moment a jump is possible, it happens
* float wall_time; s since the current wall hang began
* float wall_jump_yaw; where a wall jump from this hang launches: the
* approach direction mirrored off the wall
@@ -113,13 +115,14 @@ typedef struct Player {
float facing_yaw; /* radians; facing vector = (sin(yaw), 0, cos(yaw)) */
float forward_speed; /* ground speed along facing_yaw, m/s (>= 0) */
PlayerState state;
- bool heightened_movement; /* second jump available (or in progress); later moves read it too */
+ bool heightened_movement; /* second-jump window, and on through second/retreat/wall jumps and hangs */
float heightened_timer; /* s left before heightened_movement ends; paused during the second jump */
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 */
+ float jump_buffer; /* s left on the last A press; a jump fires as soon as one is possible */
float wall_time; /* s since the current wall hang began */
float wall_jump_yaw; /* a wall jump's launch direction: the approach mirrored off the wall */
bool has_last_wall; /* a wall was jumped off since the last landing... */