summaryrefslogtreecommitdiff
path: root/platformer/src/world.c
blob: c999f4123a99b5fc9f5a1a38db8098584b6368c5 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * ============================================================
 * FILE:        world.c
 *
 * 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. 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:
 *   - 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. 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).
 *   - Zones (level_build_zones()): stored separately from the boxes, so no
 *     collision query ever sees them. world_in_zone(point, kind): is the point
 *     inside (edges count) any zone of that kind.
 *   - 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: Deciding what's in the level (level.c), rotating boxes,
 *              spatial partitioning (a few hundred boxes is fine to loop over).
 *
 * DONE WHEN:   physics.c and render.c see every box where it is this tick, and
 *              moving boxes report how far they moved.
 * ============================================================
 */

#include "config.h"
#include "level.h"
#include "world.h"

#define SURFACE_EPSILON      0.01f   /* m, "standing exactly on it" tolerance */
#define OVERLAP_EPSILON      1e-4f   /* m, edges closer than this only touch */

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 float    s_time;                     /* s since world_init */
static Zone     s_zones[LEVEL_MAX_ZONES];   /* non-solid regions, kept apart from the boxes */
static int      s_zone_count;

/* Where box i is at the current time. */
static Box box_now(int i)
{
    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;
}

void world_init(void)
{
    s_box_count  = level_build(s_level, LEVEL_MAX_BOXES);
    s_zone_count = level_build_zones(s_zones, LEVEL_MAX_ZONES);
    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);
    }
}

void world_update(float dt)
{
    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;
    }
}

Vec3 world_box_moved(int i)
{
    return s_moved[i];
}

int world_box_count(void)
{
    return s_box_count;
}

Box world_box(int i)
{
    return s_boxes[i];
}

/* ---- Geometry queries -------------------------------------------------- */

/* Does the footprint around (x, z) overlap the box's footprint? Edges that
 * only touch don't count, so a body pushed flush against a wall isn't "in" it. */
static bool footprint_overlaps(Box b, float x, float z, float half_width)
{
    return x + half_width > b.min.x + OVERLAP_EPSILON &&
           x - half_width < b.max.x - OVERLAP_EPSILON &&
           z + half_width > b.min.z + OVERLAP_EPSILON &&
           z - half_width < b.max.z - OVERLAP_EPSILON;
}

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;
            floor_box = i;
        }
    }
    if (box)
        *box = floor_box;
    return floor_y;
}

float world_ceiling_above(Vec3 head, float half_width)
{
    float ceiling_y = WORLD_NO_CEILING;
    for (int i = 0; i < s_box_count; i++) {
        Box b = s_boxes[i];
        if (footprint_overlaps(b, head.x, head.z, half_width) &&
            b.min.y >= head.y - SURFACE_EPSILON && b.min.y < ceiling_y)
            ceiling_y = b.min.y;
    }
    return ceiling_y;
}

bool world_find_overlap(Vec3 feet, float half_width, float height, Box *hit)
{
    for (int i = 0; i < s_box_count; i++) {
        Box b = s_boxes[i];
        bool vertical = feet.y < b.max.y - OVERLAP_EPSILON &&
                        feet.y + height > b.min.y + OVERLAP_EPSILON;
        if (vertical && footprint_overlaps(b, feet.x, feet.z, half_width)) {
            *hit = b;
            return true;
        }
    }
    return false;
}

/* Keeps x and z on the ground square; y (height) is untouched. */
Vec3 world_clamp_to_bounds(Vec3 p)
{
    p.x = clampf(p.x, -WORLD_HALF_EXTENT, WORLD_HALF_EXTENT);
    p.z = clampf(p.z, -WORLD_HALF_EXTENT, WORLD_HALF_EXTENT);
    return p;
}

Vec3 world_spawn_point(void)
{
    return level_spawn_point();
}

int world_zone_count(void)
{
    return s_zone_count;
}

Zone world_zone(int i)
{
    return s_zones[i];
}

bool world_in_zone(Vec3 point, ZoneKind kind)
{
    for (int i = 0; i < s_zone_count; i++) {
        Box b = s_zones[i].bounds;
        if (s_zones[i].kind == kind &&
            point.x >= b.min.x && point.x <= b.max.x &&
            point.y >= b.min.y && point.y <= b.max.y &&
            point.z >= b.min.z && point.z <= b.max.z)
            return true;
    }
    return false;
}

float world_half_extent(void)
{
    return WORLD_HALF_EXTENT;
}