From 219e32ad793e66a3111465ef02f3eb677f70277d Mon Sep 17 00:00:00 2001 From: Guillaume Pasquet Date: Thu, 2 Jan 2020 17:28:15 +0000 Subject: Fix for invisible enemies - Don't double-count visibility - Set enemies visibility with LOS - Set dirty flag when enemies are revealed --- .gitignore | 1 + src/entities.rs | 10 +++++----- src/state.rs | 25 +++++++++++++++++++++++++ src/world.rs | 9 +++++---- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index cce2d15..5542972 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target **/*.rs.bk +/log *.log diff --git a/src/entities.rs b/src/entities.rs index 78e5ab9..98c8443 100644 --- a/src/entities.rs +++ b/src/entities.rs @@ -41,7 +41,6 @@ pub struct Character { luck: i32, xp: i32, tile: Tile, - visible: bool, } pub trait Enemy { @@ -114,11 +113,14 @@ impl Entity for Character { } fn visibility(&mut self, visible: bool) { - self.visible = visible; + if visible != self.is_visible() { + self.dirty = true; + } + self.tile.visibility(visible) } fn is_visible(&self) -> bool { - self.visible + self.tile.is_visible() } } @@ -146,7 +148,6 @@ impl Enemy for Character { previous_location: location, tile: Tile::from(TileType::Character(tile_str)), dirty: false, - visible: false, } } @@ -182,7 +183,6 @@ impl Player for Character { false, ), dirty: false, - visible: true, } } diff --git a/src/state.rs b/src/state.rs index 776f2bf..0badaf0 100644 --- a/src/state.rs +++ b/src/state.rs @@ -46,6 +46,10 @@ impl State { &self.dungeon.levels[self.level] } + pub fn current_level_mut(&mut self) -> &mut Level { + &mut self.dungeon.levels[self.level] + } + fn can_step_on(tile: &Tile) -> bool { match tile.get_type() { TileType::Floor => true, @@ -55,6 +59,27 @@ impl State { } } + fn clear_los(&mut self) { + { + let grid = self.grid.as_mut().unwrap(); + grid.clear_fog_of_war(self.player.location(), PLAYER_SIGHT); + } + + for i in 0..self.current_level().entities.len() { + let loc = *self.current_level().entities[i].location(); + if self + .grid + .as_ref() + .unwrap() + .block_at(loc.0, loc.1) + .is_visible() + && !self.current_level().entities[i].is_visible() + { + self.current_level_mut().entities[i].visibility(true); + } + } + } + pub fn move_player(&mut self, dir: Movement) -> Result<(), String> { let grid = match &self.grid { Some(g) => g, diff --git a/src/world.rs b/src/world.rs index af72513..c577680 100644 --- a/src/world.rs +++ b/src/world.rs @@ -430,16 +430,17 @@ impl Generatable for Level { let room = &self.rooms[rng.gen_range(0, self.rooms.len() - 1)]; // Create the enemy + let enemy_coords = ( + room.start.0 + rng.gen_range(0, room.width - 1) + 1, + room.start.1 + rng.gen_range(0, room.height - 1) + 1, + ); self.entities.push(Box::::new(Enemy::new( String::from("snake"), 2 * self.depth as i32, (2.0 * self.depth as f32 * 0.6).round() as i32, (20.0 * self.depth as f32 * 0.2).max(80.0).round() as i32, 0, - ( - room.start.0 + rng.gen_range(0, room.width), - room.start.1 + rng.gen_range(0, room.height), - ), + enemy_coords, "s", ))); } -- cgit v1.2.3