avatar module
This commit is contained in:
parent
fa6300d798
commit
e0907d529f
|
@ -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<Image>,
|
||||
pub(crate) room: String, /// room id
|
||||
pub(crate) position: Position,
|
||||
}
|
||||
|
||||
impl From<String> 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<Image> = 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"));
|
||||
}
|
54
src/lib.rs
54
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<Image>,
|
||||
room: String, /// room id
|
||||
position: Position,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct Item {
|
||||
id: String,
|
||||
|
@ -324,50 +318,6 @@ impl AnimationFrames for Vec<Image> {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<String> 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<Image> = 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<String> for Item {
|
||||
fn from(string: String) -> Item {
|
||||
let mut lines: Vec<&str> = string.lines().collect();
|
||||
|
|
Loading…
Reference in New Issue