diff --git a/README.md b/README.md index ff000f9..80e89ce 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,12 @@ a library for parsing Bitsy game data. * tile to * position from * position to +* sprite from ## todo ### functions -* sprite from * sprite to * item from * item to diff --git a/src/main.rs b/src/main.rs index 999ac09..a47c381 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,7 +49,7 @@ struct Sprite { id: String, // lowercase base36 name: Option, animation_frames: Vec, - dialogue: Option, + dialogue: Option, /// dialogue id position: Position, } @@ -396,9 +396,73 @@ fn test_position_to_string() { ) } -// fn get_avatar(game: Game) -> Sprite { -// // get the sprite with an id of A -// } +fn sprite_from_string(string: String) -> Sprite { + let mut lines: Vec<&str> = string.split("\n").collect(); + + let id = lines[0].replace("SPR ", ""); + let mut name = None; + let mut dialogue = None; + let mut position: Option = None; + + for _ in 0..3 { + 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 if last_line.starts_with("POS") { + position = Some(position_from_string( + last_line.replace("POS ", "").to_string() + )); + } else { + lines.push(last_line); break; + } + } + + let position = position.unwrap(); + + // todo dedupe + 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(); + + Sprite { id, name, animation_frames, dialogue, position } +} + +#[test] +fn test_sprite_from_string() { + let output = sprite_from_string("SPR a\n00000000\n01111000\n01001000\n00111100\n00111100\n01011110\n01011110\n01101111\nNAME hatch\nDLG SPR_0\nPOS 4 9,7".to_string()); + let expected = Sprite { + id: "a".to_string(), + name: Some("hatch".to_string()), + animation_frames: vec![ + Image { + pixels: vec![ + 0,0,0,0,0,0,0,0, + 0,1,1,1,1,0,0,0, + 0,1,0,0,1,0,0,0, + 0,0,1,1,1,1,0,0, + 0,0,1,1,1,1,0,0, + 0,1,0,1,1,1,1,0, + 0,1,0,1,1,1,1,0, + 0,1,1,0,1,1,1,1, + ] + } + ], + dialogue: Some("SPR_0".to_string()), + position: Position { + room: "4".to_string(), + x: 9, + y: 7 + } + }; + + 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