Make collision work horizontally

This commit is contained in:
crumblingstatue 2023-04-03 18:33:11 +02:00
parent f07bd9c713
commit 413875909e
2 changed files with 26 additions and 2 deletions

View file

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

View file

@ -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 {