From 413875909ecf88b3f0baf622e155bfd32159163f Mon Sep 17 00:00:00 2001 From: crumblingstatue Date: Mon, 3 Apr 2023 18:33:11 +0200 Subject: [PATCH] Make collision work horizontally --- src/app.rs | 26 ++++++++++++++++++++++++-- src/game/player.rs | 2 ++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/app.rs b/src/app.rs index 738ee4d..8bef99f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -76,11 +76,12 @@ impl App { } else { 4.0 }; + self.game.player.hspeed = 0.; if self.input.down(Key::Left) { - self.game.player.col_en.move_x(-spd, |_, _| false); + self.game.player.hspeed = -spd; } if self.input.down(Key::Right) { - self.game.player.col_en.move_x(spd, |_, _| false); + self.game.player.hspeed = spd; } if self.input.pressed(Key::Up) { self.game.player.vspeed = -14.0; @@ -107,6 +108,27 @@ impl App { }); col }); + self.game + .player + .col_en + .move_x(self.game.player.hspeed, |player_en, off| { + let mut col = false; + for_each_tile_on_screen(self.game.camera_offset, |tp, _sp| { + let tid = self.game.world.tile_at_mut(tp).id; + if tid == Tile::AIR { + return; + } + let tsize = TILE_SIZE as i32; + let x = tp.x as i32 * TILE_SIZE as i32; + let y = tp.y as i32 * TILE_SIZE as i32; + let en = s2dc::Entity::from_rect_corners(x, y, x + tsize, y + tsize); + if player_en.would_collide(&en, off) { + col = true; + self.game.player.hspeed = 0.; + } + }); + col + }); self.game.player.vspeed += 1.0; let (x, y, _w, _h) = self.game.player.col_en.en.xywh(); self.game.camera_offset.x = (x - NATIVE_RESOLUTION.w as i32 / 2) as u32; diff --git a/src/game/player.rs b/src/game/player.rs index af5b0ae..a89291e 100644 --- a/src/game/player.rs +++ b/src/game/player.rs @@ -8,6 +8,7 @@ use crate::{ pub struct Player { pub col_en: MobileEntity, pub vspeed: f32, + pub hspeed: f32, } impl Player { @@ -15,6 +16,7 @@ impl Player { Self { col_en: MobileEntity::from_pos_and_bb(vec2(pos.x as i32, pos.y as i32), vec2(15, 24)), vspeed: 0.0, + hspeed: 0.0, } } pub fn center_tp(&self) -> TilePos {