diff --git a/src/game.rs b/src/game.rs index a6f2fe3..4fb118a 100644 --- a/src/game.rs +++ b/src/game.rs @@ -9,13 +9,15 @@ pub struct Game { impl Game { pub fn move_player(&mut self, dx: i16, dy: i16) { // check if move is allowed first - let player = &self.player; - let player_pos = player.get_position(); + let player_pos = self.player.get_position(); let level = &self.levels[player_pos.get_level()]; let new_x: i16 = player_pos.get_x() as i16 + dx; let new_y: i16 = player_pos.get_y() as i16 + dy; let can_go: bool = match level.get_element(new_x, new_y) { (None, _, _) => { false } + (_, _, Some(e)) => { + self.levels[player_pos.get_level()].attack(new_x, new_y, &mut self.player) + } (Some(t), _, _) => { match t { StructureElement::Wall => { false } @@ -52,12 +54,13 @@ impl Game { } _ => {} } - match tmp.2 { - Some(enemy) => { - todo!("implement fighting!") - } - _ => {} - } + // match tmp.2 { + // Some(enemy) => { + // // todo!("implement fighting!"); + // self.levels[player_pos.get_level()].attack(new_x, new_y, &mut self.player); + // } + // _ => {} + // } } } } \ No newline at end of file diff --git a/src/level.rs b/src/level.rs index eb05dbe..bcf0836 100644 --- a/src/level.rs +++ b/src/level.rs @@ -1,4 +1,5 @@ use std::cmp::{max, min}; +use crate::player::Player; use crate::position::Position; pub const LEVEL_WIDTH: usize = 50; @@ -52,6 +53,24 @@ impl Level { end_y: 1, } } + pub(crate) fn attack(&self, x: i16, y: i16, player: &mut Player) -> bool { + if x < 0 || y < 0 { + return false; + } + let x = x as usize; + let y = y as usize; + if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT { + return false; + } + + match self.enemies[x][y] { + Some(enemy) => { + player.change_life(-1); + } + _ => {} + } + false + } pub fn remove_artifact(&mut self, x: i16, y: i16) { if x < 0 || y < 0 { return; @@ -72,9 +91,10 @@ impl Level { if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT { return (None, None, None); } - if !self.discovered[x][y] { - return (Some(StructureElement::Unknown), None, None); - } + // if !self.discovered[x][y] { + // if !self.discovered[x][y] { + // return (Some(StructureElement::Unknown), None, None); + // } return (Some(self.structure[x][y]), self.artifacts[x][y], self.enemies[x][y]); } /// discover the area with in the level around the given position