diff --git a/README.md b/README.md index fb929d2..11e37d2 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,12 @@ a library for parsing Bitsy game data. * position to * sprite from * sprite to +* item from ## todo ### functions -* item from * item to * room from * room to diff --git a/src/main.rs b/src/main.rs index 08e2da4..9472dfb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -110,7 +110,7 @@ struct Sprite { position: Position, } -/// avatar is a "sprite" in the game data but with a specific ID +/// avatar is a "sprite" in the game data but with a specific id #[derive(Debug, Eq, PartialEq)] struct Avatar { animation_frames: Vec, @@ -122,7 +122,7 @@ struct Item { id: String, animation_frames: Vec, name: Option, - dialogue: Option, + dialogue: Option, // dialogue id } #[derive(Debug, Eq, PartialEq)] @@ -491,12 +491,69 @@ fn test_sprite_to_string() { assert_eq!(output, expected); } +fn item_from_string(string: String) -> Item { + let mut lines: Vec<&str> = string.split("\n").collect(); + + let id = lines[0].replace("ITM ", ""); + let mut name = None; + let mut dialogue = None; + + for _ in 0..2 { + 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("DLG") { + dialogue = Some(last_line.replace("DLG ", "").to_string()); + } else { + lines.push(last_line); break; + } + } + + let animation_frames = lines[1..].join(""); + // print!("{}", animation_frames); + let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect(); + let animation_frames: Vec = animation_frames.iter().map(|&frame| { + image_from_string(frame.to_string()) + }).collect(); + + Item { id, name, animation_frames, dialogue} +} + +#[test] +fn test_item_from_string() { + let output = item_from_string("ITM 6\n01000000\n00000000\n00000000\n00000100\n00100000\n00000000\n00000000\n00000010\nNAME door\nDLG ITM_2".to_string()); + let expected = Item { + id: "6".to_string(), + animation_frames: vec![ + Image { + pixels: vec![ + 0,1,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,1,0,0, + 0,0,1,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,1,0, + ] + } + ], + name: Some("door".to_string()), + dialogue: Some("ITM_2".to_string()) + }; + + assert_eq!(output, expected); +} + // 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... // // then split all these up into their individual items -// } // +// // no - let's try going from the beginning and popping off elements from the start? +// } + // fn game_to_string(game: Game) -> String { // // }