diff --git a/README.md b/README.md index a9a2eff..f6a1b2d 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ some more practical uses would be things like: * "unquote" function for dialogues, game title, anything else that supports """ * add Bitsy HD and Bitsy 7.0 test data * fix variables/endings becoming "DLG DLG"? -* dedupe "animation frames from string" functionality +* replace Image with Vec or something. seems like a pointless abstraction ### tidy up diff --git a/src/image.rs b/src/image.rs index a46e63e..09343ec 100644 --- a/src/image.rs +++ b/src/image.rs @@ -45,13 +45,24 @@ impl ToString for Image { } } +#[inline] +pub(crate) fn animation_frames_from_string(string: String) -> Vec { + print!("animation frames: \n{}", string); + let frames: Vec<&str> = string.split(">").collect(); + + frames.iter().map(|&frame| Image::from(frame.to_string())).collect() +} + #[cfg(test)] mod test { - use crate::image::Image; + use crate::image::{Image, animation_frames_from_string}; + use crate::mock; #[test] fn test_image_from_string() { - let output = Image::from(include_str!("test-resources/image").to_string()); + let output = Image::from( + include_str!("test-resources/image").to_string() + ); let expected = Image { pixels: vec![ @@ -71,8 +82,19 @@ mod test { #[test] fn test_image_to_string() { - let output = crate::mock::image::chequers_1().to_string(); + let output = mock::image::chequers_1().to_string(); let expected = include_str!("test-resources/image-chequers-1").to_string(); assert_eq!(output, expected); } + + #[test] + fn test_animation_frames_from_string() { + let output = animation_frames_from_string( + include_str!("test-resources/animation_frames").to_string() + ); + + let expected = mock::image::animation_frames(); + + assert_eq!(output, expected); + } } diff --git a/src/item.rs b/src/item.rs index 09578ff..33b737d 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1,4 +1,5 @@ use crate::{from_base36, optional_data_line, AnimationFrames, Image, ToBase36}; +use crate::image::animation_frames_from_string; #[derive(Debug, Eq, PartialEq)] pub struct Item { @@ -51,13 +52,9 @@ impl From for Item { } } - // todo dedupe - let animation_frames = lines[1..].join(""); - let animation_frames: Vec<&str> = animation_frames.split(">").collect(); - let animation_frames: Vec = animation_frames - .iter() - .map(|&frame| Image::from(frame.to_string())) - .collect(); + let animation_frames = animation_frames_from_string( + lines[1..].join("\n") + ); Item { id, diff --git a/src/mock.rs b/src/mock.rs index 86f244c..e12770e 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -25,6 +25,60 @@ pub mod image { ], } } + + #[inline] + pub fn animation_frames() -> Vec { + vec![ + Image { + pixels: vec![ + 1,0,0,1,1,0,0,1, + 0,0,1,1,0,0,1,1, + 0,1,1,0,0,1,1,0, + 1,1,0,0,1,1,0,0, + 1,0,0,1,1,0,0,1, + 0,0,1,1,0,0,1,1, + 0,1,1,0,0,1,1,0, + 1,1,0,0,1,1,0,0, + ] + }, + Image { + pixels: vec![ + 1,1,0,0,1,1,0,0, + 1,0,0,1,1,0,0,1, + 0,0,1,1,0,0,1,1, + 0,1,1,0,0,1,1,0, + 1,1,0,0,1,1,0,0, + 1,0,0,1,1,0,0,1, + 0,0,1,1,0,0,1,1, + 0,1,1,0,0,1,1,0, + ] + }, + Image { + pixels: vec![ + 0,1,1,0,0,1,1,0, + 1,1,0,0,1,1,0,0, + 1,0,0,1,1,0,0,1, + 0,0,1,1,0,0,1,1, + 0,1,1,0,0,1,1,0, + 1,1,0,0,1,1,0,0, + 1,0,0,1,1,0,0,1, + 0,0,1,1,0,0,1,1, + ] + }, + Image { + pixels: vec![ + 0,0,1,1,0,0,1,1, + 0,1,1,0,0,1,1,0, + 1,1,0,0,1,1,0,0, + 1,0,0,1,1,0,0,1, + 0,0,1,1,0,0,1,1, + 0,1,1,0,0,1,1,0, + 1,1,0,0,1,1,0,0, + 1,0,0,1,1,0,0,1, + ] + }, + ] + } } #[inline] diff --git a/src/sprite.rs b/src/sprite.rs index bdd4a74..22df26b 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -1,4 +1,5 @@ use crate::{from_base36, optional_data_line, AnimationFrames, Image, Position, ToBase36}; +use crate::image::animation_frames_from_string; #[derive(Debug, Eq, PartialEq)] pub struct Sprite { @@ -94,13 +95,9 @@ impl From for Sprite { items.reverse(); - // todo dedupe - let animation_frames = lines[1..].join(""); - let animation_frames: Vec<&str> = animation_frames.split(">").collect(); - let animation_frames: Vec = animation_frames - .iter() - .map(|&frame| Image::from(frame.to_string())) - .collect(); + let animation_frames = animation_frames_from_string( + lines[1..].join("\n") + ); Sprite { id, diff --git a/src/test-resources/animation_frames b/src/test-resources/animation_frames new file mode 100644 index 0000000..c44bb89 --- /dev/null +++ b/src/test-resources/animation_frames @@ -0,0 +1,35 @@ +10011001 +00110011 +01100110 +11001100 +10011001 +00110011 +01100110 +11001100 +> +11001100 +10011001 +00110011 +01100110 +11001100 +10011001 +00110011 +01100110 +> +01100110 +11001100 +10011001 +00110011 +01100110 +11001100 +10011001 +00110011 +> +00110011 +01100110 +11001100 +10011001 +00110011 +01100110 +11001100 +10011001 diff --git a/src/tile.rs b/src/tile.rs index 060c903..cc5801d 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,4 +1,5 @@ use crate::{from_base36, optional_data_line, AnimationFrames, Image, ToBase36}; +use crate::image::animation_frames_from_string; #[derive(Debug, Eq, PartialEq)] pub struct Tile { @@ -60,12 +61,9 @@ impl From for Tile { } } - let animation_frames = lines[1..].join(""); - let animation_frames: Vec<&str> = animation_frames.split(">").collect(); - let animation_frames: Vec = animation_frames - .iter() - .map(|&frame| Image::from(frame.to_string())) - .collect(); + let animation_frames = animation_frames_from_string( + lines[1..].join("\n") + ); Tile { id,