diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | src/entities.rs | 10 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/state.rs | 37 | ||||
-rw-r--r-- | src/world.rs | 9 |
5 files changed, 43 insertions, 16 deletions
@@ -1,2 +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/main.rs b/src/main.rs index d00db53..f88a4b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,7 +57,6 @@ fn main() { // Main loop, dispatches events and calls rendering routines. Don't // add any game logic here. loop { - state.render_player(); state.render_level(); state.render_entities(); state.render_player(); diff --git a/src/state.rs b/src/state.rs index 8561486..40c66f6 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> { 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::<Character>::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", ))); } |