From e86625bbc852fdc7b18f6cdb4394cfeb5ee7b7c1 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 6 Apr 2020 13:21:59 +0100 Subject: [PATCH] exit functions; ending and room stub functions --- README.md | 2 ++ src/main.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/README.md b/README.md index 11e2c3b..01613b1 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ a library for parsing Bitsy game data. * sprite to * item from * item to +* exit from +* exit to ## todo diff --git a/src/main.rs b/src/main.rs index abb4255..9033fa0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -569,6 +569,90 @@ fn test_item_to_string() { assert_eq!(output, expected); } +fn exit_from_string(string: String) -> Exit { + // e.g. "4 3,3" + let room_position: Vec<&str> = string.split(' ').collect(); + let room = room_position[0].to_string(); + let position = position_from_string(room_position[1].to_string()); + + Exit { room, position } +} + +#[test] +fn test_exit_from_string() { + assert_eq!( + exit_from_string("a 12,13".to_string()), + Exit { room: "a".to_string(), position: Position { x: 12, y: 13}} + ); +} + +fn exit_to_string(exit: Exit) -> String { + format!("{} {}", exit.room, position_to_string(exit.position)) +} + +#[test] +fn test_exit_to_string() { + assert_eq!( + exit_to_string(Exit { room: "8".to_string(), position: Position { x: 5, y: 6 }}), + "8 5,6".to_string() + ); +} + +fn ending_from_string(string: String) -> Ending { + Ending { contents: "".to_string() } +} + +fn ending_to_string(ending: Ending) -> String { + "".to_string() +} + +fn room_from_string(string: String) -> Room { + // todo handle room_format? + let mut lines: Vec<&str> = string.split("\n").collect(); + let id = lines[0].replace("ROOM ", ""); + let mut name = None; + let mut palette = "0".to_string(); + let mut items: HashMap = HashMap::new(); + let exits: HashMap = HashMap::new(); + let endings: HashMap = HashMap::new(); + + loop { + let last_line = lines.pop().unwrap(); + + if last_line.starts_with("NAME") { + name = Some(last_line.replace("NAME ", "").to_string()); + } else if last_line.starts_with("PAL") { + palette = last_line.replace("PAL ", "").to_string(); + } else if last_line.starts_with("ITM") { + let last_line = last_line.replace("ITM ", ""); + let item_pos: Vec<&str> = last_line.split(' ').collect(); + let item_id = item_pos[0]; + let pos = item_pos[1]; + + items.insert( + position_from_string(pos.to_string()), + item_id.to_string() + ); + } else if last_line.starts_with("EXT") { + + } else if last_line.starts_with("END") { + + } else { + lines.push(last_line); break; + } + } + + Room { + id, + palette, + name, + tiles: vec![], + items: Default::default(), + exits: Default::default(), + endings: Default::default() + } +} + // fn game_from_string(game: String ) -> Game { // // probably needs to split the game data into different segments starting from the end // // e.g. VAR... then END... then DLG...