make walls vec optional

This commit is contained in:
Max Bradbury 2020-10-18 15:59:56 +01:00
parent 889328f9a9
commit fb290f07f4
2 changed files with 9 additions and 9 deletions

View File

@ -529,7 +529,7 @@ pub fn room() -> Room {
position: Position { x: 8, y: 7 }, position: Position { x: 8, y: 7 },
id: "undefined".to_string(), id: "undefined".to_string(),
}], }],
walls: vec![], walls: None,
} }
} }
@ -828,7 +828,7 @@ pub fn game_default() -> Game {
items: vec![], items: vec![],
exits: vec![], exits: vec![],
endings: vec![], endings: vec![],
walls: vec![], walls: None,
}], }],
tiles: vec![self::tile_default()], tiles: vec![self::tile_default()],
sprites: vec![ sprites: vec![

View File

@ -23,7 +23,7 @@ pub struct Room {
pub exits: Vec<ExitInstance>, pub exits: Vec<ExitInstance>,
pub endings: Vec<Instance>, pub endings: Vec<Instance>,
/// old method of handling walls - a comma-separated list of tile IDs /// old method of handling walls - a comma-separated list of tile IDs
pub walls: Vec<String>, pub walls: Option<Vec<String>>,
} }
impl Room { impl Room {
@ -32,10 +32,10 @@ impl Room {
} }
fn wall_line(&self) -> String { fn wall_line(&self) -> String {
if self.walls.is_empty() { if let Some(walls) = &self.walls {
"".to_string() optional_data_line("WAL", Some(walls.join(",")))
} else { } else {
optional_data_line("WAL", Some(self.walls.join(","))) "".to_string()
} }
} }
@ -58,7 +58,7 @@ impl From<String> for Room {
let mut items: Vec<Instance> = Vec::new(); let mut items: Vec<Instance> = Vec::new();
let mut exits: Vec<ExitInstance> = Vec::new(); let mut exits: Vec<ExitInstance> = Vec::new();
let mut endings: Vec<Instance> = Vec::new(); let mut endings: Vec<Instance> = Vec::new();
let mut walls: Vec<String> = Vec::new(); let mut walls = None;
loop { loop {
let last_line = lines.pop().unwrap(); let last_line = lines.pop().unwrap();
@ -66,7 +66,7 @@ impl From<String> for Room {
if last_line.starts_with("WAL") { if last_line.starts_with("WAL") {
let last_line = last_line.replace("WAL ", ""); let last_line = last_line.replace("WAL ", "");
let ids: Vec<&str> = last_line.split(',').collect(); let ids: Vec<&str> = last_line.split(',').collect();
walls = ids.iter().map(|&id| id.to_string()).collect(); walls = Some(ids.iter().map(|&id| id.to_string()).collect());
} else if last_line.starts_with("NAME") { } else if last_line.starts_with("NAME") {
name = Some(last_line.replace("NAME ", "").to_string()); name = Some(last_line.replace("NAME ", "").to_string());
} else if last_line.starts_with("PAL") { } else if last_line.starts_with("PAL") {
@ -258,6 +258,6 @@ mod test {
fn room_walls_array() { fn room_walls_array() {
let output = Room::from(include_str!("test-resources/room-with-walls").to_string()); let output = Room::from(include_str!("test-resources/room-with-walls").to_string());
assert_eq!(output.walls, vec!["a".to_string(), "f".to_string()]); assert_eq!(output.walls, Some(vec!["a".to_string(), "f".to_string()]));
} }
} }