/* * ============================================================ * FILE: world.h * * PURPOSE: Public interface for "the level" while the game runs: a flat * square of ground with OBJECTS on it (solid boxes: platforms, * terrain, walls; some of them moving) and ZONES (non-solid * regions that change behavior inside them, even when box-shaped). * "Box" here means an object unless it's a zone's bounds. This API is how other modules ask * questions about the level's geometry. * What's IN the level is decided by level.c (level.h). * * FITS IN: physics.c asks what's under the feet, what's above the head, * and what box a body overlaps. render.c asks for the extent and * the boxes so it can draw them, and for the floor under the * player for the shadow. main.c asks for the spawn point and calls * world_update() once per tick to move the moving boxes. * * INTERFACE: Use these exact names: * * typedef enum BoxStyle { how render.c colors a box (nothing else reads it) * BOX_STYLE_PLATFORM, the default: a color from the platform palette * BOX_STYLE_CLIFF, rock: big terrain masses like the cliffs * BOX_STYLE_WATER the river (still solid underfoot) * } BoxStyle; * * typedef struct Box { * Vec3 min, max; axis-aligned corners, meters (min.y = bottom, max.y = top) * bool allows_wall_hang; its sides can be wall-hung (see player.c). FALSE * by default: only boxes a level marks count. * BoxStyle style; how it's drawn; BOX_STYLE_PLATFORM (0) by default * } Box; * * typedef enum ZoneKind { * ZONE_LOCKED_PAN the camera doesn't swing behind a player in it * } ZoneKind; * typedef struct Zone { Box bounds; ZoneKind kind; } Zone; * A zone is NOT solid: the player walks in and out freely, and no * collision query ever sees it. It only changes how things behave * while the player is inside. * * void world_init(void); * Builds the level (level_build() in level.c). * void world_update(float dt); * Once per tick, BEFORE the player moves: moves every moving box * along its loop. * Vec3 world_box_moved(int i); * How far box i moved in the last world_update (0 for still ones). * physics.c uses it to carry a body standing on a moving box. * int world_box_count(void); * Box world_box(int i); 0 <= i < world_box_count(); where it is now * * A "footprint" below is the square [x +- half_width] x [z +- half_width] * around a body's center. Touching edges don't count as overlapping. * * float world_floor_below(Vec3 feet, float half_width, int *box); * The highest surface under the footprint whose top is at or just * below feet.y (a box top, or the ground at 0). This is what the * feet land on, and where the shadow goes. If box isn't NULL, *box * = that box's index, or -1 for the ground. * float world_ceiling_above(Vec3 head, float half_width); * The lowest box bottom over the footprint that is at or just above * head.y, or WORLD_NO_CEILING if there's none. * bool world_find_overlap(Vec3 feet, float half_width, float height, Box *hit); * True (and *hit = that box) if a body standing at `feet` with this * size overlaps any box. physics.c uses it to push bodies out of walls. * Vec3 world_clamp_to_bounds(Vec3 p); * Returns p with x and z clamped to [-WORLD_HALF_EXTENT, * +WORLD_HALF_EXTENT]. y is untouched. * Vec3 world_spawn_point(void); * Where the player starts / resets to (level_spawn_point()). The * area around it is kept clear of platforms. * bool world_object_at(Vec3 point, float radius, Box *hit); * True (and *hit = that object) if the point is within `radius` of * the inside of an OBJECT (zones never count). The camera uses it * to keep the eye out of objects. * int world_zone_count(void); * Zone world_zone(int i); 0 <= i < world_zone_count() (render.c draws them) * bool world_in_zone(Vec3 point, ZoneKind kind); * True if the point is inside any zone of that kind (edges count). * float world_half_extent(void); * Returns WORLD_HALF_EXTENT (so render.c doesn't need config * knowledge of the world's shape). * * #define WORLD_NO_CEILING 1e9f (far above anything) * * Include and "math3d.h". * * CONVENTIONS: Include guard: WORLD_H. * * NOT IN SCOPE: Level files, slopes, rotating boxes, non-box shapes. * * DONE WHEN: physics.c and render.c can be written using only this header. * ============================================================ */ #ifndef WORLD_H #define WORLD_H #include #include "math3d.h" #define WORLD_NO_CEILING 1e9f /* returned when nothing is overhead */ /* How render.c colors a box. Purely visual: collision ignores it. */ typedef enum BoxStyle { BOX_STYLE_PLATFORM, /* the default: a color from the platform palette */ BOX_STYLE_CLIFF, /* rock: big terrain masses like the cliffs */ BOX_STYLE_WATER /* the river (still solid underfoot; there's no swimming) */ } BoxStyle; typedef struct Box { Vec3 min, max; /* axis-aligned corners, meters (min.y = bottom, max.y = top) */ bool allows_wall_hang; /* its sides can be wall-hung; false unless the level marks it */ BoxStyle style; /* how it's drawn; BOX_STYLE_PLATFORM unless the level says otherwise */ } Box; /* What a zone does to a player inside it. */ typedef enum ZoneKind { ZONE_LOCKED_PAN /* the camera doesn't swing behind the player (it still tilts) */ } ZoneKind; /* A region of the level that changes behavior inside it. NOT solid: the * player passes in and out freely, and collision never sees it. */ typedef struct Zone { Box bounds; ZoneKind kind; } Zone; void world_init(void); void world_update(float dt); Vec3 world_box_moved(int i); int world_box_count(void); Box world_box(int i); float world_floor_below(Vec3 feet, float half_width, int *box); float world_ceiling_above(Vec3 head, float half_width); bool world_find_overlap(Vec3 feet, float half_width, float height, Box *hit); Vec3 world_clamp_to_bounds(Vec3 p); Vec3 world_spawn_point(void); bool world_object_at(Vec3 point, float radius, Box *hit); int world_zone_count(void); Zone world_zone(int i); bool world_in_zone(Vec3 point, ZoneKind kind); float world_half_extent(void); #endif /* WORLD_H */