diff --git a/src/avatar.rs b/src/avatar.rs new file mode 100644 index 0000000..341f1bf --- /dev/null +++ b/src/avatar.rs @@ -0,0 +1,54 @@ +use crate::{AnimationFrames, Image, Position}; +use crate::mocks; + +/// avatar is a "sprite" in the game data but with a specific id +#[derive(Debug, Eq, PartialEq)] +pub struct Avatar { + pub(crate) animation_frames: Vec, + pub(crate) room: String, /// room id + pub(crate) position: Position, +} + +impl From for Avatar { + fn from(string: String) -> Avatar { + let string = string.replace("SPR A\n", ""); + let mut lines: Vec<&str> = string.lines().collect(); + let room_pos = lines.pop().unwrap().replace("POS ", ""); + let room_pos: Vec<&str> = room_pos.split_whitespace().collect(); + let room = room_pos[0].to_string(); + let position = Position::from(room_pos[1].to_string()); + let animation_frames: String = lines.join("\n"); + let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect(); + let animation_frames: Vec = animation_frames.iter().map(|&frame| { + Image::from(frame.to_string()) + }).collect(); + + Avatar { animation_frames, room, position } + } +} + +impl ToString for Avatar { + #[inline] + fn to_string(&self) -> String { + format!( + "SPR A\n{}\nPOS {} {}", + self.animation_frames.to_string(), + self.room, + self.position.to_string() + ) + } +} + +#[test] +fn test_avatar_from_string() { + let output = Avatar::from( + include_str!("../test/resources/avatar").to_string() + ); + + assert_eq!(output, mocks::avatar()); +} + +#[test] +fn test_avatar_to_string() { + assert_eq!(mocks::avatar().to_string(), include_str!("../test/resources/avatar")); +} diff --git a/src/lib.rs b/src/lib.rs index 8ca5c38..e5f5060 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod avatar; pub mod colour; pub mod dialogue; pub mod palette; @@ -7,6 +8,7 @@ pub mod position; pub mod sprite; pub mod tile; +use avatar::Avatar; use colour::Colour; use dialogue::Dialogue; use palette::Palette; @@ -27,14 +29,6 @@ pub struct ExitInstance { exit: Exit, } -/// avatar is a "sprite" in the game data but with a specific id -#[derive(Debug, Eq, PartialEq)] -pub struct Avatar { - animation_frames: Vec, - room: String, /// room id - position: Position, -} - #[derive(Debug, Eq, PartialEq)] pub struct Item { id: String, @@ -324,50 +318,6 @@ impl AnimationFrames for Vec { } } -impl From for Avatar { - fn from(string: String) -> Avatar { - let string = string.replace("SPR A\n", ""); - let mut lines: Vec<&str> = string.lines().collect(); - let room_pos = lines.pop().unwrap().replace("POS ", ""); - let room_pos: Vec<&str> = room_pos.split_whitespace().collect(); - let room = room_pos[0].to_string(); - let position = Position::from(room_pos[1].to_string()); - let animation_frames: String = lines.join("\n"); - let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect(); - let animation_frames: Vec = animation_frames.iter().map(|&frame| { - Image::from(frame.to_string()) - }).collect(); - - Avatar { animation_frames, room, position } - } -} - -#[test] -fn test_avatar_from_string() { - let output = Avatar::from( - include_str!("../test/resources/avatar").to_string() - ); - - assert_eq!(output, mocks::avatar()); -} - -impl ToString for Avatar { - #[inline] - fn to_string(&self) -> String { - format!( - "SPR A\n{}\nPOS {} {}", - self.animation_frames.to_string(), - self.room, - self.position.to_string() - ) - } -} - -#[test] -fn test_avatar_to_string() { - assert_eq!(mocks::avatar().to_string(), include_str!("../test/resources/avatar")); -} - impl From for Item { fn from(string: String) -> Item { let mut lines: Vec<&str> = string.lines().collect();