summaryrefslogtreecommitdiff
path: root/platformer/src/world.h
blob: 6cd1ce0f0d7f0f2aadfa75591cf75cb035bf1b75 (plain) (blame)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * ============================================================
 * FILE:        world.h
 *
 * PURPOSE:     Public interface for "the level" while the game runs: a flat
 *              square of ground with box-shaped platforms and terrain on it
 *              (some of them moving). 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;
 *
 *   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.
 *   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, 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 <stdbool.h>

#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;

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);
float world_half_extent(void);

#endif /* WORLD_H */