monsters can be attack but do not die
This commit is contained in:
parent
7b521ef6bd
commit
d02ef5e046
19
src/game.rs
19
src/game.rs
|
@ -9,13 +9,15 @@ pub struct Game {
|
||||||
impl Game {
|
impl Game {
|
||||||
pub fn move_player(&mut self, dx: i16, dy: i16) {
|
pub fn move_player(&mut self, dx: i16, dy: i16) {
|
||||||
// check if move is allowed first
|
// check if move is allowed first
|
||||||
let player = &self.player;
|
let player_pos = self.player.get_position();
|
||||||
let player_pos = player.get_position();
|
|
||||||
let level = &self.levels[player_pos.get_level()];
|
let level = &self.levels[player_pos.get_level()];
|
||||||
let new_x: i16 = player_pos.get_x() as i16 + dx;
|
let new_x: i16 = player_pos.get_x() as i16 + dx;
|
||||||
let new_y: i16 = player_pos.get_y() as i16 + dy;
|
let new_y: i16 = player_pos.get_y() as i16 + dy;
|
||||||
let can_go: bool = match level.get_element(new_x, new_y) {
|
let can_go: bool = match level.get_element(new_x, new_y) {
|
||||||
(None, _, _) => { false }
|
(None, _, _) => { false }
|
||||||
|
(_, _, Some(e)) => {
|
||||||
|
self.levels[player_pos.get_level()].attack(new_x, new_y, &mut self.player)
|
||||||
|
}
|
||||||
(Some(t), _, _) => {
|
(Some(t), _, _) => {
|
||||||
match t {
|
match t {
|
||||||
StructureElement::Wall => { false }
|
StructureElement::Wall => { false }
|
||||||
|
@ -52,12 +54,13 @@ impl Game {
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
match tmp.2 {
|
// match tmp.2 {
|
||||||
Some(enemy) => {
|
// Some(enemy) => {
|
||||||
todo!("implement fighting!")
|
// // todo!("implement fighting!");
|
||||||
}
|
// self.levels[player_pos.get_level()].attack(new_x, new_y, &mut self.player);
|
||||||
_ => {}
|
// }
|
||||||
}
|
// _ => {}
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
26
src/level.rs
26
src/level.rs
|
@ -1,4 +1,5 @@
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
|
use crate::player::Player;
|
||||||
use crate::position::Position;
|
use crate::position::Position;
|
||||||
|
|
||||||
pub const LEVEL_WIDTH: usize = 50;
|
pub const LEVEL_WIDTH: usize = 50;
|
||||||
|
@ -52,6 +53,24 @@ impl Level {
|
||||||
end_y: 1,
|
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) {
|
pub fn remove_artifact(&mut self, x: i16, y: i16) {
|
||||||
if x < 0 || y < 0 {
|
if x < 0 || y < 0 {
|
||||||
return;
|
return;
|
||||||
|
@ -72,9 +91,10 @@ impl Level {
|
||||||
if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT {
|
if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT {
|
||||||
return (None, None, None);
|
return (None, None, None);
|
||||||
}
|
}
|
||||||
if !self.discovered[x][y] {
|
// if !self.discovered[x][y] {
|
||||||
return (Some(StructureElement::Unknown), None, None);
|
// 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]);
|
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
|
/// discover the area with in the level around the given position
|
||||||
|
|
Loading…
Reference in New Issue