Fog of War can be disabled in dev builds

This commit is contained in:
Joachim Lusiardi 2024-01-19 10:10:52 +01:00
parent 119a3d314a
commit 32fffd492d
4 changed files with 46 additions and 28 deletions

View File

@ -27,6 +27,7 @@ pub struct Game {
/// the levels of the game
levels: Vec<Level>,
pub messages: Vec<String>,
use_fog_of_war: bool,
}
impl Game {
@ -39,6 +40,7 @@ impl Game {
player: p,
levels: v,
messages: Vec::with_capacity(10),
use_fog_of_war: true,
};
let start = {
g.get_level(0).start
@ -74,6 +76,8 @@ impl Game {
pub fn get_player(&self) -> &Player {
&self.player
}
pub fn toggle_fog_of_war(&mut self) { self.use_fog_of_war = !self.use_fog_of_war; }
pub fn uses_fog_of_war(&self) -> bool { self.use_fog_of_war }
pub fn get_mutable_player(&mut self) -> &mut Player {
&mut self.player
}
@ -105,7 +109,7 @@ impl Game {
let new_y: i16 = player_pos.get_y() as i16 + dy;
let player_level = player_pos.get_level();
let level = &mut self.levels[player_level];
level.get_element(new_x, new_y).0
level.get_element(new_x, new_y, self.use_fog_of_war).0
}
/// move the game's player into the given direction. If on the target field there is a stair,
@ -155,7 +159,7 @@ impl Game {
let player_pos = &self.player.get_immutable_position();
let player_level = player_pos.get_level();
let level = &mut self.levels[player_level];
let (_, m, _) = level.get_element(player_pos.get_x() as i16, player_pos.get_y() as i16);
let (_, m, _) = level.get_element(player_pos.get_x() as i16, player_pos.get_y() as i16, true);
match m {
None => {}
Some(m) => {
@ -186,7 +190,7 @@ impl Game {
let player_pos = &self.player.get_immutable_position();
let player_level = player_pos.get_level();
let level = &mut self.levels[player_level];
let (_, _, a) = level.get_element(player_pos.get_x() as i16, player_pos.get_y() as i16);
let (_, _, a) = level.get_element(player_pos.get_x() as i16, player_pos.get_y() as i16, true);
match a {
None => {}
Some(a) => {

View File

@ -38,7 +38,7 @@ pub struct Level {
}
impl Level {
pub fn get_element(&mut self, x: i16, y: i16) -> (Option<StructureElement>, Option<&mut Box<(dyn Monster + 'static)>>, Option<&mut Box<(dyn Artifact + 'static)>>) {
pub fn get_element(&mut self, x: i16, y: i16, use_fog_of_war: bool) -> (Option<StructureElement>, Option<&mut Box<(dyn Monster + 'static)>>, Option<&mut Box<(dyn Artifact + 'static)>>) {
if x < 0 || y < 0 {
return (None, None, None);
}
@ -47,7 +47,7 @@ impl Level {
if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT {
return (None, None, None);
}
if !self.discovered[x][y] {
if use_fog_of_war && !self.discovered[x][y] {
return (Some(StructureElement::Unknown), None, None);
}
let search_pos = &Position::new(self.level, x, y);
@ -239,23 +239,23 @@ impl Level {
#[test]
fn test_get_element() {
let mut l = Level::new(0);
assert_eq!(l.get_element(-1, -1).0, None);
assert_eq!(l.get_element(0, 0).0.unwrap(), StructureElement::Unknown);
assert_eq!(l.get_element(LEVEL_WIDTH as i16 - 1, LEVEL_HEIGHT as i16 - 1).0.unwrap(), StructureElement::Unknown);
assert_eq!(l.get_element(LEVEL_WIDTH as i16, LEVEL_HEIGHT as i16).0, None);
assert_eq!(l.get_element(-1, -1, true).0, None);
assert_eq!(l.get_element(0, 0, true).0.unwrap(), StructureElement::Unknown);
assert_eq!(l.get_element(LEVEL_WIDTH as i16 - 1, LEVEL_HEIGHT as i16 - 1, true).0.unwrap(), StructureElement::Unknown);
assert_eq!(l.get_element(LEVEL_WIDTH as i16, LEVEL_HEIGHT as i16, true).0, None);
}
#[test]
fn test_discover_get_element() {
let mut l = Level::new(0);
assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Unknown);
assert_eq!(l.get_element(10, 10, true).0.unwrap(), StructureElement::Unknown);
let p = Position::new(0, 10, 10);
l.discover(&p);
assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(9, 10).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(10, 9).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(11, 10).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(10, 11).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(10, 10, true).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(9, 10, true).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(10, 9, true).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(11, 10, true).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(10, 11, true).0.unwrap(), StructureElement::Floor);
}
#[test]
@ -296,20 +296,20 @@ fn test_discover_get_monster() {
let mut l = Level::new(0);
let p = Position::new(0, 10, 10);
l.discover(&p);
assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Floor);
assert!(l.get_element(10, 10).1.is_none());
assert_eq!(l.get_element(10, 10, true).0.unwrap(), StructureElement::Floor);
assert!(l.get_element(10, 10, true).1.is_none());
let m = Rat::new_with_position(Position::new(0, 10, 10));
assert_eq!(l.add_monster(m), Ok(()));
let elem = l.get_element(10, 10);
let elem = l.get_element(10, 10, true);
assert_eq!(elem.0.unwrap(), StructureElement::Floor);
assert!(elem.1.is_some());
let m = elem.1.unwrap();
assert_eq!(m.get_life(), 3);
m.decrease_life(3);
assert_eq!(l.get_element(10, 10).1.unwrap().get_life(), 0);
assert_eq!(l.get_element(10, 10, true).1.unwrap().get_life(), 0);
}
#[test]
@ -317,13 +317,13 @@ fn test_discover_get_artifact() {
let mut l = Level::new(0);
let p = Position::new(0, 10, 10);
l.discover(&p);
assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Floor);
assert!(l.get_element(10, 10).2.is_none());
assert_eq!(l.get_element(10, 10, true).0.unwrap(), StructureElement::Floor);
assert!(l.get_element(10, 10, true).2.is_none());
let a = Chest::new(Position::new(0, 10, 10));
assert_eq!(l.add_artifact(a), Ok(()));
let elem = l.get_element(10, 10);
let elem = l.get_element(10, 10, true);
assert_eq!(elem.0.unwrap(), StructureElement::Floor);
assert!(elem.2.is_some());
}
@ -337,12 +337,12 @@ fn test_discover_get_monster_can_move() {
let m = Rat::new_with_position(Position::new(0, 10, 10));
l.add_monster(m).expect("Panic because of");
let m = l.get_element(10, 10).1.unwrap();
let m = l.get_element(10, 10, true).1.unwrap();
m.get_position().change(1, 1);
let m = l.get_element(10, 10).1;
let m = l.get_element(10, 10, true).1;
assert!(m.is_none());
let m = l.get_element(11, 11).1;
let m = l.get_element(11, 11, true).1;
assert!(m.is_some());
assert_eq!(m.unwrap().get_life(), 3);
}

View File

@ -9,9 +9,16 @@ use crate::position::HasPosition;
const FG_BROWN: Color = Color::Rgb(186, 74, 0);
pub struct LevelWidget {}
pub struct LevelWidget {
use_fog_of_war: bool,
}
impl LevelWidget {
pub fn new(use_fog_of_war: bool) -> Self {
LevelWidget {
use_fog_of_war
}
}
fn set_cell(&self, buf: &mut Buffer, x: u16, y: u16, symbol: &str, fg: Color, bg: Color) {
buf.
get_mut(x, y).
@ -39,7 +46,7 @@ impl StatefulWidget for LevelWidget {
let level = state.get_mutable_level(player_level);
let level_x = (x - al) as i16;
let level_y = (y - at) as i16;
match level.get_element(level_x, level_y) {
match level.get_element(level_x, level_y, self.use_fog_of_war) {
(Some(structure_element), None, None) => {
match structure_element {
StructureElement::Start => {

View File

@ -83,7 +83,7 @@ fn main() -> Result<()> {
width: level::LEVEL_WIDTH as u16,
height: level::LEVEL_HEIGHT as u16,
};
frame.render_stateful_widget(LevelWidget {}, map_area, &mut game);
frame.render_stateful_widget(LevelWidget::new(game.uses_fog_of_war()), map_area, &mut game);
let stats_area = Rect {
x: area.x + 50,
@ -137,6 +137,13 @@ fn main() -> Result<()> {
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('v') {
game.messages.insert(0, format!("You are playing version '{}'.", env!("GIT_HASH")).to_string());
}
// disabling the d key (disable fog of war) in release builds!
#[cfg(debug_assertions)]
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('d') {
game.messages.insert(0, "toggle fog of war!".to_string());
game.toggle_fog_of_war();
}
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
break;
}