From 7f6fa9df82346bcea3fb7622b97a8cfd5bb846c2 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 6 Apr 2020 07:23:20 +0100 Subject: [PATCH] animation frames to string; sprite to string --- src/main.rs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index a47c381..39d2f9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -172,6 +172,21 @@ fn test_image_to_string() { assert_eq!(output, expected); } +fn animation_frames_to_string(animation_frames: Vec) -> String { + let mut string = String::new(); + let last_frame = animation_frames.len() - 1; + + for (i, frame) in animation_frames.into_iter().enumerate() { + string.push_str(&image_to_string(frame)); + + if i < last_frame { + string.push_str(&"\n>\n".to_string()); + } + } + + string +} + fn tile_from_string(string: String) -> Tile { let mut lines: Vec<&str> = string.split("\n").collect(); @@ -223,21 +238,10 @@ fn test_tile_from_string() { } fn tile_to_string(tile: Tile) -> String { - let mut animation_frames = String::new(); - let last_frame = tile.animation_frames.len() - 1; - - for (i, frame) in tile.animation_frames.into_iter().enumerate() { - animation_frames.push_str(&image_to_string(frame)); - - if i < last_frame { - animation_frames.push_str(&"\n>\n".to_string()); - } - } - format!( "TIL {}\n{}{}{}", tile.id, - animation_frames, + animation_frames_to_string(tile.animation_frames), if tile.name.is_some() {format!("\nNAME {}", tile.name.unwrap())} else {"".to_string()}, if tile.wall {"\nWAL true"} else {""} ) @@ -464,6 +468,17 @@ fn test_sprite_from_string() { assert_eq!(output, expected); } +fn sprite_to_string(sprite: Sprite) -> String { + format!( + "SPR {}\n{}\n{}{}\nPOS {}", + sprite.id, + animation_frames_to_string(sprite.animation_frames), + if sprite.name.is_some() {format!("NAME {}", sprite.name.unwrap())} else {"".to_string()}, + if sprite.dialogue.is_some() {format!("DLG {}", sprite.dialogue.unwrap())} else {"".to_string()}, + position_to_string(sprite.position), + ) +} + // 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...