From e71c87ad4c95907b7fe9e720bad8cf4b4bdfb3bd Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sat, 18 Apr 2020 14:39:17 +0100 Subject: [PATCH] handle room wall array --- README.md | 1 - src/game.rs | 9 ++------- src/mock.rs | 4 +++- src/room.rs | 31 ++++++++++++++++++++++++++++--- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0245153..8f38c6d 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,6 @@ some more practical uses would be things like: * test for bitsy 7.0 and implement new features * implement Result return types on ::from functions so we can handle errors * check multi-line endings/dialogues -* handle room wall array * handle old out-of-bounds editor errors (extra image pixels, exits/endings with position -1,-0 etc.) * it's ok if the output of these does not match the input as we're fixing it diff --git a/src/game.rs b/src/game.rs index fe74165..04a1315 100644 --- a/src/game.rs +++ b/src/game.rs @@ -353,16 +353,11 @@ fn test_bitsy_omnibus() { // fails because of sprite colours but also because a tile contains "NaN" "src/test-resources/omnibus/DA88C287.bitsy.txt".to_string(), - // fails because room wall array is not implemented - // (this game uses room_format 1 - I thought it'd be using 0...) - "src/test-resources/omnibus/76EB6E4A.bitsy.txt".to_string(), - "src/test-resources/omnibus/DC053B1A.bitsy.txt".to_string(), - "src/test-resources/omnibus/CD0609B6.bitsy.txt".to_string(), - "src/test-resources/omnibus/E45F178D.bitsy.txt".to_string(), // not sure about this one but it uses room wall array "src/test-resources/omnibus/748F77B5.bitsy.txt".to_string(), + // this also has a "SET" instead of a room - what? todo investigate + // and the SET has no palette? is this allowed? "src/test-resources/omnibus/1998508E.bitsy.txt".to_string(), - "src/test-resources/omnibus/AB368CEC.bitsy.txt".to_string(), // bad game data "src/test-resources/omnibus/14C48FA0.bitsy.txt".to_string(), diff --git a/src/mock.rs b/src/mock.rs index f2af151..ed1f17f 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -182,6 +182,7 @@ pub fn room() -> Room { endings: vec![ Instance{position: Position { x: 8, y: 7 }, id: "undefined".to_string()}, ], + walls: vec![], } } @@ -229,7 +230,8 @@ pub fn game_default() -> Game { ], items: vec![], exits: vec![], - endings: vec![] + endings: vec![], + walls: vec![] } ], tiles: vec![ diff --git a/src/room.rs b/src/room.rs index 7faa69c..f8a0ad1 100644 --- a/src/room.rs +++ b/src/room.rs @@ -9,12 +9,22 @@ pub struct Room { pub items: Vec, pub exits: Vec, pub endings: Vec, + pub walls: Vec, // old way of handling walls... } impl Room { fn name_line(&self) -> String { optional_data_line("NAME", self.name.as_ref()) } + + fn wall_line(&self) -> String { + if self.walls.len() > 0 { + let ids: Vec = self.walls.iter().map(|&id| id.to_base36()).collect(); + optional_data_line("WAL", Some(ids.join(","))) + } else { + "".to_string() + } + } } impl From for Room { @@ -27,11 +37,16 @@ impl From for Room { let mut items: Vec = Vec::new(); let mut exits: Vec = Vec::new(); let mut endings: Vec = Vec::new(); + let mut walls: Vec = Vec::new(); loop { let last_line = lines.pop().unwrap(); - if last_line.starts_with("NAME") { + if last_line.starts_with("WAL") { + let last_line = last_line.replace("WAL ", ""); + let ids: Vec<&str> = last_line.split(",").collect(); + walls = ids.iter().map(|&id| from_base36(id)).collect(); + } else if last_line.starts_with("NAME") { name = Some(last_line.replace("NAME ", "").to_string()); } else if last_line.starts_with("PAL") { palette_id = from_base36(&last_line.replace("PAL ", "")); @@ -80,7 +95,7 @@ impl From for Room { exits.reverse(); endings.reverse(); - Room { id, palette_id, name, tiles, items, exits, endings } + Room { id, palette_id, name, tiles, items, exits, endings, walls } } } @@ -132,10 +147,11 @@ impl ToString for Room { } format!( - "ROOM {}\n{}{}{}{}{}\nPAL {}", + "ROOM {}\n{}{}{}{}{}{}\nPAL {}", self.id.to_base36(), tiles, self.name_line(), + self.wall_line(), items, exits, endings, @@ -148,3 +164,12 @@ impl ToString for Room { fn test_room_to_string() { assert_eq!(crate::mock::room().to_string(), include_str!("test-resources/room").to_string()); } + +#[test] +fn test_room_walls_array() { + let output = Room::from( + include_str!("test-resources/room-with-walls").to_string() + ); + + assert_eq!(output.walls, vec![10, 15]); +}