diff options
author | Guillaume Pasquet <dev@etenil.net> | 2020-01-02 17:28:15 +0000 |
---|---|---|
committer | Guillaume Pasquet <dev@etenil.net> | 2020-01-02 17:28:15 +0000 |
commit | 518ad5caf2cbf313cb784d9fd1a51632fe600fae (patch) | |
tree | dca06c8733f24550155af486d96846423f5ac657 /src/state.rs | |
parent | f14e0005f082b5d4cbca0f2e0d501ad15da3c856 (diff) |
Fix for invisible enemies
- Don't double-count visibility
- Set enemies visibility with LOS
- Set dirty flag when enemies are revealed
Diffstat (limited to 'src/state.rs')
-rw-r--r-- | src/state.rs | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/src/state.rs b/src/state.rs index be32228..6f91d27 100644 --- a/src/state.rs +++ b/src/state.rs @@ -29,6 +29,7 @@ impl State { self.dungeon.generate(); self.switch_level(0); self.player.place(self.current_level().start_point()); + self.clear_los() } pub fn switch_level(&mut self, num_level: usize) { @@ -76,11 +77,6 @@ impl State { pub fn render_player(&mut self) { self.render_entity(&self.player); - - self.grid - .as_mut() - .unwrap() - .clear_fog_of_war(self.player.location(), PLAYER_SIGHT); } fn ui_state_position(&self) -> MoveTo { @@ -120,6 +116,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, @@ -129,6 +129,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, @@ -140,7 +161,11 @@ impl State { if !State::can_step_on(grid.block_at(loc.0, loc.1)) { return Err(String::from("Can't move entity!")); } - self.player.move_by(dir) + self.player.move_by(dir)?; + + self.clear_los(); + + Ok(()) } pub fn down_stairs(&mut self) -> Result<(), String> { |