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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
use macroquad::math::{vec2, Rect, Vec2};

use std::collections::HashSet;

pub struct StaticTiledLayer {
    static_colliders: Vec<bool>,
    tile_width: f32,
    tile_height: f32,
    width: usize,
    tag: u8,
}

pub struct World {
    static_tiled_layers: Vec<StaticTiledLayer>,
    solids: Vec<(Solid, Collider)>,
    actors: Vec<(Actor, Collider)>,
}

#[derive(Clone, Debug)]
struct Collider {
    collidable: bool,
    squished: bool,
    pos: Vec2,
    width: i32,
    height: i32,
    x_remainder: f32,
    y_remainder: f32,
    squishers: HashSet<Solid>,
}

impl Collider {
    pub fn rect(&self) -> Rect {
        Rect::new(
            self.pos.x,
            self.pos.y,
            self.width as f32,
            self.height as f32,
        )
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub struct Actor(usize);

#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub struct Solid(usize);

impl World {
    pub fn new() -> World {
        World {
            static_tiled_layers: vec![],
            actors: vec![],
            solids: vec![],
        }
    }

    pub fn add_static_tiled_layer(
        &mut self,
        static_colliders: Vec<bool>,
        tile_width: f32,
        tile_height: f32,
        width: usize,
        tag: u8,
    ) {
        self.static_tiled_layers.push(StaticTiledLayer {
            static_colliders,
            tile_width,
            tile_height,
            width,
            tag,
        });
    }
    pub fn add_actor(&mut self, pos: Vec2, width: i32, height: i32) -> Actor {
        let actor = Actor(self.actors.len());

        self.actors.push((
            actor,
            Collider {
                collidable: true,
                squished: false,
                pos,
                width,
                height,
                x_remainder: 0.,
                y_remainder: 0.,
                squishers: HashSet::new(),
            },
        ));

        actor
    }

    pub fn add_solid(&mut self, pos: Vec2, width: i32, height: i32) -> Solid {
        let solid = Solid(self.solids.len());

        self.solids.push((
            solid,
            Collider {
                collidable: true,
                squished: false,
                pos,
                width,
                height,
                x_remainder: 0.,
                y_remainder: 0.,
                squishers: HashSet::new(),
            },
        ));

        solid
    }

    pub fn set_actor_position(&mut self, actor: Actor, pos: Vec2) {
        let collider = &mut self.actors[actor.0].1;

        collider.x_remainder = 0.0;
        collider.y_remainder = 0.0;
        collider.pos = pos;
    }

    pub fn move_v(&mut self, actor: Actor, dy: f32) -> bool {
        let id = actor.0;
        let mut collider = self.actors[id].1.clone();

        collider.y_remainder += dy;

        let mut move_ = collider.y_remainder.round() as i32;
        if move_ != 0 {
            collider.y_remainder -= move_ as f32;
            let sign = move_.signum();

            while move_ != 0 {
                if self.collide_solids(
                    collider.pos + vec2(0., sign as f32),
                    collider.width,
                    collider.height,
                ) == false
                {
                    collider.pos.y += sign as f32;
                    move_ -= sign;
                } else {
                    self.actors[id].1 = collider;

                    return false;
                }
            }
        }
        self.actors[id].1 = collider;
        true
    }

    pub fn move_h(&mut self, actor: Actor, dy: f32) -> bool {
        let id = actor.0;
        let mut collider = self.actors[id].1.clone();
        collider.x_remainder += dy;

        let mut move_ = collider.x_remainder.round() as i32;
        if move_ != 0 {
            collider.x_remainder -= move_ as f32;
            let sign = move_.signum();

            while move_ != 0 {
                if self.collide_solids(
                    collider.pos + vec2(sign as f32, 0.),
                    collider.width,
                    collider.height,
                ) == false
                {
                    collider.pos.x += sign as f32;
                    move_ -= sign;
                } else {
                    self.actors[id].1 = collider;
                    return false;
                }
            }
        }
        self.actors[id].1 = collider;
        true
    }

    pub fn solid_move(&mut self, solid: Solid, dx: f32, dy: f32) {
        let collider = &mut self.solids[solid.0].1;

        collider.x_remainder += dx;
        collider.y_remainder += dy;
        let move_x = collider.x_remainder.round() as i32;
        let move_y = collider.y_remainder.round() as i32;

        let mut riding_actors = vec![];
        let mut pushing_actors = vec![];

        let riding_rect = Rect::new(
            collider.pos.x,
            collider.pos.y - 1.0,
            collider.width as f32,
            1.0,
        );
        let pushing_rect = Rect::new(
            collider.pos.x + move_x as f32,
            collider.pos.y,
            collider.width as f32 - 1.0,
            collider.height as f32,
        );

        for (actor, actor_collider) in &mut self.actors {
            let rider_rect = Rect::new(
                actor_collider.pos.x,
                actor_collider.pos.y + actor_collider.height as f32 - 1.0,
                actor_collider.width as f32,
                1.0,
            );

            if riding_rect.overlaps(&rider_rect) {
                riding_actors.push(*actor);
            } else if pushing_rect.overlaps(&actor_collider.rect())
                && actor_collider.squished == false
            {
                pushing_actors.push(*actor);
            }

            if pushing_rect.overlaps(&actor_collider.rect()) == false {
                actor_collider.squishers.remove(&solid);
                if actor_collider.squishers.len() == 0 {
                    actor_collider.squished = false;
                }
            }
        }

        self.solids[solid.0].1.collidable = false;
        for actor in riding_actors {
            self.move_h(actor, move_x as f32);
        }
        for actor in pushing_actors {
            let squished = !self.move_h(actor, move_x as f32);
            if squished {
                self.actors[actor.0].1.squished = true;
                self.actors[actor.0].1.squishers.insert(solid);
            }
        }
        self.solids[solid.0].1.collidable = true;

        let collider = &mut self.solids[solid.0].1;
        if move_x != 0 {
            collider.x_remainder -= move_x as f32;
            collider.pos.x += move_x as f32;
        }
        if move_y != 0 {
            collider.y_remainder -= move_y as f32;
            collider.pos.y += move_y as f32;
        }
    }

    pub fn solid_at(&self, pos: Vec2) -> bool {
        self.tag_at(pos, 1)
    }

    pub fn tag_at(&self, pos: Vec2, tag: u8) -> bool {
        for StaticTiledLayer {
            tile_width,
            tile_height,
            width,
            static_colliders,
            tag: layer_tag,
        } in &self.static_tiled_layers
        {
            let y = (pos.y / tile_width) as i32;
            let x = (pos.x / tile_height) as i32;
            let ix = y * (*width as i32) + x;

            if ix >= 0 && ix < static_colliders.len() as i32 && static_colliders[ix as usize] {
                return *layer_tag == tag;
            }
        }

        self.solids
            .iter()
            .any(|solid| solid.1.collidable && solid.1.rect().contains(pos))
    }

    pub fn collide_solids(&self, pos: Vec2, width: i32, height: i32) -> bool {
        self.collide_tag(1, pos, width, height)
            || self.solids.iter().any(|solid| {
                solid.1.collidable
                    && solid.1.rect().overlaps(&Rect::new(
                        pos.x,
                        pos.y,
                        width as f32,
                        height as f32,
                    ))
            })
    }

    pub fn collide_tag(&self, tag: u8, pos: Vec2, width: i32, height: i32) -> bool {
        for StaticTiledLayer {
            tile_width,
            tile_height,
            width: layer_width,
            static_colliders,
            tag: layer_tag,
        } in &self.static_tiled_layers
        {
            let check = |pos: Vec2| {
                let y = (pos.y / tile_width) as i32;
                let x = (pos.x / tile_height) as i32;
                let ix = y * (*layer_width as i32) + x;
                if ix >= 0 && ix < static_colliders.len() as i32 && static_colliders[ix as usize] {
                    return *layer_tag == tag;
                }
                return false;
            };

            if check(pos)
                || check(pos + vec2(width as f32 - 1.0, 0.0))
                || check(pos + vec2(width as f32 - 1.0, height as f32 - 1.0))
                || check(pos + vec2(0.0, height as f32 - 1.0))
            {
                return true;
            }

            if width > *tile_width as i32 {
                let mut x = pos.x;

                while {
                    x += tile_width;
                    x < pos.x + width as f32 - 1.
                } {
                    if check(vec2(x, pos.y)) || check(vec2(x, pos.y + height as f32 - 1.0)) {
                        return true;
                    }
                }
            }

            if height > *tile_height as i32 {
                let mut y = pos.y;

                while {
                    y += tile_height;
                    y < pos.y + height as f32 - 1.
                } {
                    if check(vec2(pos.x, y)) || check(vec2(pos.x + width as f32 - 1., y)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    pub fn squished(&self, actor: Actor) -> bool {
        self.actors[actor.0].1.squished
    }

    pub fn actor_pos(&self, actor: Actor) -> Vec2 {
        self.actors[actor.0].1.pos
    }

    pub fn solid_pos(&self, solid: Solid) -> Vec2 {
        self.solids[solid.0].1.pos
    }

    pub fn collide_check(&self, collider: Actor, pos: Vec2) -> bool {
        let collider = &self.actors[collider.0];

        self.collide_solids(pos, collider.1.width, collider.1.height)
    }
}