summaryrefslogtreecommitdiff
path: root/platformer/src/world.c
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2026-09-29 21:02:22 -0400
committerSchark <jordan@schark.online>2026-09-29 21:02:22 -0400
commit21404c26d3907cefa524878fb031e7a2022fb3d7 (patch)
tree38b2bb26d6d0c05447213edca4911ccc3887f474 /platformer/src/world.c
parent5f04f3ae87fb307e055d39e81f7c0c841e8a9af8 (diff)
downloadgamedev-21404c26d3907cefa524878fb031e7a2022fb3d7.tar.gz
gamedev-21404c26d3907cefa524878fb031e7a2022fb3d7.zip
Game world test with updated movement
Diffstat (limited to 'platformer/src/world.c')
-rw-r--r--platformer/src/world.c168
1 files changed, 59 insertions, 109 deletions
diff --git a/platformer/src/world.c b/platformer/src/world.c
index fb7a840..7bbde8e 100644
--- a/platformer/src/world.c
+++ b/platformer/src/world.c
@@ -2,149 +2,94 @@
* ============================================================
* FILE: world.c
*
- * PURPOSE: The test world: a flat square of ground with a scatter of box
- * platforms to run around and jump on. It's still a test space,
- * like the one SM64's team tuned Mario in, just with things to
- * jump onto now.
+ * PURPOSE: Where the level lives while the game runs: it stores the boxes
+ * level.c builds, moves the moving ones every tick, and answers
+ * geometry questions about them (floors, ceilings, overlaps).
*
- * FITS IN: Implements world.h. Has no dependencies besides config.h and
- * math3d.h. physics.c is its main caller; render.c draws its boxes.
+ * FITS IN: Implements world.h. Calls level_build() once and
+ * level_motion_offset() every tick (level.h). physics.c is its
+ * main caller; render.c draws its boxes; main.c calls
+ * world_update() once per tick.
*
* IMPLEMENT:
- * - Level data: a static array of WORLD_PLATFORM_COUNT boxes, filled by
- * world_init().
- *
- * - world_init(): generate the platforms with a tiny xorshift32 random
- * generator seeded from WORLD_SEED. WHY our own instead of rand(): rand()
- * differs between C libraries, so the "same" seed could give a different
- * level on another machine. Two kinds of platform (ranges are named
- * constants at the top of this file):
- * block stands on the ground (bottom 0), top low enough to jump onto
- * from the ground (a full jump lifts the feet ~2.3 m)
- * floating a thin slab higher up, reached by jumping from a block
- * Each gets a random footprint and a random spot within WORLD_PLATFORM_AREA
- * of spawn (x and z). Re-roll the spot if the footprint comes within
- * SPAWN_CLEAR_RADIUS of spawn, so the player never starts inside a platform.
- * Platforms may overlap each other; that's fine for both collision and
- * drawing.
+ * - Storage: the built LevelBox list (each box at offset 0 plus its motion),
+ * the boxes where they are NOW, and how far each moved last tick.
+ * - world_init(): level_build(), then place every box at its time-0 spot.
+ * - world_update(dt): advance the clock; move each moving box to its built
+ * position + level_motion_offset(motion, time), remembering how far it
+ * moved (world_box_moved) so physics can carry whatever stands on it.
*
* - Queries, all over the body's FOOTPRINT (a square of +-half_width, the
* same shape as the player's box), with edges that only touch NOT counting:
* world_floor_below: the highest top <= feet.y + SURFACE_EPSILON among
* boxes whose footprint overlaps, or 0 (the ground). The epsilon lets
- * a body standing exactly on a top still find it.
+ * a body standing exactly on a top still find it. Also reports WHICH
+ * box that is (-1 for the ground), for riding moving platforms.
* world_ceiling_above: the lowest bottom >= head.y - SURFACE_EPSILON, or
* WORLD_NO_CEILING.
* world_find_overlap: the first box that overlaps the body's full volume
* (footprint AND height range).
- * - world_clamp_to_bounds, world_spawn_point, world_half_extent: as before.
+ * - world_clamp_to_bounds, world_spawn_point (level_spawn_point()),
+ * world_half_extent.
*
* CONVENTIONS: Y up. Boxes are axis-aligned. The ground surface is y = 0.
*
- * NOT IN SCOPE: Level files, guaranteeing every platform is reachable, slopes,
- * moving platforms.
+ * NOT IN SCOPE: Deciding what's in the level (level.c), rotating boxes,
+ * spatial partitioning (a few hundred boxes is fine to loop over).
*
- * DONE WHEN: It compiles warning-free, the same seed always builds the same
- * level, spawn is clear, and the player can't leave the square.
+ * DONE WHEN: physics.c and render.c see every box where it is this tick, and
+ * moving boxes report how far they moved.
* ============================================================
*/
-#include <stdint.h>
-
#include "config.h"
+#include "level.h"
#include "world.h"
-/* Platform generation ranges (level design, not movement feel). */
-#define PLATFORM_MIN_SIZE 2.0f /* m, shortest footprint side */
-#define PLATFORM_MAX_SIZE 6.0f /* m, longest footprint side */
-#define BLOCK_MIN_TOP 0.6f /* m, lowest ground block */
-#define BLOCK_MAX_TOP 2.0f /* m, highest ground block (full jump reaches ~2.3) */
-#define FLOATING_MIN_TOP 2.6f /* m, lowest floating slab top */
-#define FLOATING_MAX_TOP 4.2f /* m, highest floating slab top (reachable from a block) */
-#define FLOATING_THICKNESS 0.5f /* m, floating slab thickness */
-#define FLOATING_CHANCE 0.35f /* fraction of platforms that float */
-#define SPAWN_CLEAR_RADIUS 4.0f /* m around spawn kept free of platforms */
-#define MAX_PLACEMENT_TRIES 50 /* re-rolls before giving up on a platform */
-
#define SURFACE_EPSILON 0.01f /* m, "standing exactly on it" tolerance */
#define OVERLAP_EPSILON 1e-4f /* m, edges closer than this only touch */
-static Box s_boxes[WORLD_PLATFORM_COUNT];
+static LevelBox s_level[LEVEL_MAX_BOXES]; /* as built: each box at offset 0, and how it moves */
+static Box s_boxes[LEVEL_MAX_BOXES]; /* where each box is now */
+static Vec3 s_moved[LEVEL_MAX_BOXES]; /* how far each box moved in the last world_update */
static int s_box_count;
-static uint32_t s_rng_state;
-
-/* ---- Random numbers ---------------------------------------------------- */
-
-/* xorshift32: three shifts and XORs give a decent, fully repeatable sequence. */
-static uint32_t next_random(void)
-{
- s_rng_state ^= s_rng_state << 13;
- s_rng_state ^= s_rng_state >> 17;
- s_rng_state ^= s_rng_state << 5;
- return s_rng_state;
-}
+static float s_time; /* s since world_init */
-/* A float in [lo, hi]. */
-static float random_range(float lo, float hi)
+/* Where box i is at the current time. */
+static Box box_now(int i)
{
- return lo + (hi - lo) * ((float)next_random() / 4294967295.0f);
-}
-
-/* ---- Level generation -------------------------------------------------- */
-
-/* True if the footprint comes within SPAWN_CLEAR_RADIUS of spawn. Uses the
- * closest point of the footprint to spawn, so big platforms can't sneak in. */
-static bool too_close_to_spawn(Box b)
-{
- Vec3 spawn = world_spawn_point();
- float dx = clampf(spawn.x, b.min.x, b.max.x) - spawn.x;
- float dz = clampf(spawn.z, b.min.z, b.max.z) - spawn.z;
- return dx * dx + dz * dz < SPAWN_CLEAR_RADIUS * SPAWN_CLEAR_RADIUS;
+ Vec3 offset = level_motion_offset(s_level[i].motion, s_time);
+ Box b = s_level[i].box;
+ b.min = vec3_add(b.min, offset);
+ b.max = vec3_add(b.max, offset);
+ return b;
}
-/* One platform with a random size and height (but no position yet). */
-static Box random_platform_shape(void)
+void world_init(void)
{
- float half_x = 0.5f * random_range(PLATFORM_MIN_SIZE, PLATFORM_MAX_SIZE);
- float half_z = 0.5f * random_range(PLATFORM_MIN_SIZE, PLATFORM_MAX_SIZE);
- float bottom, top;
- if (random_range(0.0f, 1.0f) < FLOATING_CHANCE) {
- top = random_range(FLOATING_MIN_TOP, FLOATING_MAX_TOP);
- bottom = top - FLOATING_THICKNESS;
- } else {
- top = random_range(BLOCK_MIN_TOP, BLOCK_MAX_TOP);
- bottom = 0.0f;
+ s_box_count = level_build(s_level, LEVEL_MAX_BOXES);
+ s_time = 0.0f;
+ for (int i = 0; i < s_box_count; i++) {
+ s_boxes[i] = box_now(i);
+ s_moved[i] = vec3_make(0.0f, 0.0f, 0.0f);
}
- Box b = { { -half_x, bottom, -half_z }, { half_x, top, half_z } };
- return b;
}
-/* Moves a box so its footprint is centered on (x, z). */
-static Box place_box(Box b, float x, float z)
+void world_update(float dt)
{
- Vec3 offset = vec3_make(x, 0.0f, z);
- b.min = vec3_add(b.min, offset);
- b.max = vec3_add(b.max, offset);
- return b;
+ s_time += dt;
+ for (int i = 0; i < s_box_count; i++) {
+ if (s_level[i].motion.kind == MOTION_NONE)
+ continue; /* s_moved stays 0 */
+ Box now = box_now(i);
+ s_moved[i] = vec3_sub(now.min, s_boxes[i].min);
+ s_boxes[i] = now;
+ }
}
-void world_init(void)
+Vec3 world_box_moved(int i)
{
- s_rng_state = WORLD_SEED ? WORLD_SEED : 1u; /* xorshift must not start at 0 */
- s_box_count = 0;
-
- for (int i = 0; i < WORLD_PLATFORM_COUNT; i++) {
- Box shape = random_platform_shape();
- for (int tries = 0; tries < MAX_PLACEMENT_TRIES; tries++) {
- Box placed = place_box(shape,
- random_range(-WORLD_PLATFORM_AREA, WORLD_PLATFORM_AREA),
- random_range(-WORLD_PLATFORM_AREA, WORLD_PLATFORM_AREA));
- if (!too_close_to_spawn(placed)) {
- s_boxes[s_box_count++] = placed;
- break;
- }
- }
- }
+ return s_moved[i];
}
int world_box_count(void)
@@ -169,15 +114,20 @@ static bool footprint_overlaps(Box b, float x, float z, float half_width)
z - half_width < b.max.z - OVERLAP_EPSILON;
}
-float world_floor_below(Vec3 feet, float half_width)
+float world_floor_below(Vec3 feet, float half_width, int *box)
{
float floor_y = 0.0f; /* the ground */
+ int floor_box = -1;
for (int i = 0; i < s_box_count; i++) {
Box b = s_boxes[i];
if (footprint_overlaps(b, feet.x, feet.z, half_width) &&
- b.max.y <= feet.y + SURFACE_EPSILON && b.max.y > floor_y)
- floor_y = b.max.y;
+ b.max.y <= feet.y + SURFACE_EPSILON && b.max.y > floor_y) {
+ floor_y = b.max.y;
+ floor_box = i;
+ }
}
+ if (box)
+ *box = floor_box;
return floor_y;
}
@@ -217,7 +167,7 @@ Vec3 world_clamp_to_bounds(Vec3 p)
Vec3 world_spawn_point(void)
{
- return vec3_make(0.0f, 0.0f, 0.0f);
+ return level_spawn_point();
}
float world_half_extent(void)