1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/*
* ============================================================
* FILE: world.h
*
* PURPOSE: Public interface for "the level": a flat square of ground with
* a scatter of box-shaped platforms on it. This API is how other
* modules ask questions about the level's geometry.
*
* 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.
*
* INTERFACE: Use these exact names:
*
* typedef struct Box {
* Vec3 min, max; axis-aligned corners, meters (min.y = bottom, max.y = top)
* } Box;
*
* void world_init(void);
* Builds the level. Platforms are placed "randomly" from
* WORLD_SEED, so the same seed always gives the same layout.
* int world_box_count(void);
* Box world_box(int i); 0 <= i < world_box_count()
*
* 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);
* 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.
* 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: (0, 0, 0). The area around
* it is kept clear of platforms.
* 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 <stdbool.h> and "math3d.h".
*
* CONVENTIONS: Include guard: WORLD_H.
*
* NOT IN SCOPE: Level files, slopes, moving platforms, non-box shapes.
*
* DONE WHEN: physics.c and render.c can be written using only this header.
* ============================================================
*/
#ifndef WORLD_H
#define WORLD_H
#include <stdbool.h>
#include "math3d.h"
#define WORLD_NO_CEILING 1e9f /* returned when nothing is overhead */
typedef struct Box {
Vec3 min, max; /* axis-aligned corners, meters (min.y = bottom, max.y = top) */
} Box;
void world_init(void);
int world_box_count(void);
Box world_box(int i);
float world_floor_below(Vec3 feet, float half_width);
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);
float world_half_extent(void);
#endif /* WORLD_H */
|