Check if move would end in a wall first
This commit is contained in:
parent
62040642eb
commit
3830f26e92
22
src/game.rs
22
src/game.rs
|
@ -1,14 +1,30 @@
|
|||
use crate::level::Level;
|
||||
use crate::level::{Level, LevelElement};
|
||||
use crate::player::Player;
|
||||
|
||||
pub struct Game {
|
||||
pub player: Player,
|
||||
pub levels: [Level; 25]
|
||||
pub levels: [Level; 25],
|
||||
}
|
||||
|
||||
impl Game {
|
||||
pub fn move_player(&mut self, dx: i8, dy: i8) {
|
||||
// check if move is allowed first
|
||||
self.player.change_position(dx, dy);
|
||||
let player = &self.player;
|
||||
let player_pos = player.get_position();
|
||||
let level = &self.levels[player_pos.get_level() as usize];
|
||||
let new_x = ((player_pos.get_x() as i16) + dx as i16) as u16;
|
||||
let new_y = ((player_pos.get_y() as i16) + dy as i16) as u16;
|
||||
let can_go: bool = match level.get_element(new_x as i16, new_y as i16) {
|
||||
None => { false }
|
||||
Some(t) => {
|
||||
match t {
|
||||
LevelElement::Wall => { false }
|
||||
_ => { true }
|
||||
}
|
||||
}
|
||||
};
|
||||
if can_go {
|
||||
self.player.change_position(dx, dy);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue