2020-04-13 18:08:21 +00:00
|
|
|
use crate::{AnimationFrames, Image, Position, mock, from_base36, ToBase36, optional_data_line};
|
2020-04-12 12:35:17 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
pub struct Sprite {
|
2020-04-13 13:43:23 +00:00
|
|
|
pub id: u64,
|
2020-04-12 16:13:08 +00:00
|
|
|
pub name: Option<String>,
|
|
|
|
pub animation_frames: Vec<Image>,
|
2020-04-13 13:43:23 +00:00
|
|
|
pub dialogue_id: Option<String>,
|
|
|
|
pub room_id: u64,
|
2020-04-12 16:13:08 +00:00
|
|
|
pub position: Position,
|
2020-04-12 12:35:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:08:21 +00:00
|
|
|
impl Sprite {
|
|
|
|
fn name_line(&self) -> String {
|
|
|
|
optional_data_line("NAME", self.name.as_ref())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dialogue_line(&self) -> String {
|
|
|
|
optional_data_line("DLG", self.dialogue_id.as_ref())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 12:35:17 +00:00
|
|
|
impl From<String> for Sprite {
|
|
|
|
fn from(string: String) -> Sprite {
|
|
|
|
let mut lines: Vec<&str> = string.lines().collect();
|
|
|
|
|
2020-04-13 13:43:23 +00:00
|
|
|
let id = from_base36(&lines[0].replace("SPR ", ""));
|
2020-04-12 12:35:17 +00:00
|
|
|
let mut name = None;
|
2020-04-13 13:43:23 +00:00
|
|
|
let mut dialogue_id: Option<String> = None;
|
|
|
|
let mut room_id: Option<u64> = None;
|
2020-04-12 12:35:17 +00:00
|
|
|
let mut position: Option<Position> = 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") {
|
2020-04-13 13:43:23 +00:00
|
|
|
dialogue_id = Some(last_line.replace("DLG ", "").to_string());
|
2020-04-12 12:35:17 +00:00
|
|
|
} else if last_line.starts_with("POS") {
|
|
|
|
let last_line = last_line.replace("POS ", "");
|
|
|
|
let room_position: Vec<&str> = last_line.split(' ').collect();
|
2020-04-13 13:43:23 +00:00
|
|
|
room_id = Some(from_base36(&room_position[0]));
|
2020-04-12 12:35:17 +00:00
|
|
|
position = Some(Position::from(room_position[1].to_string()));
|
|
|
|
} else {
|
|
|
|
lines.push(last_line);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 13:43:23 +00:00
|
|
|
let room_id = room_id.unwrap();
|
2020-04-12 12:35:17 +00:00
|
|
|
let position = position.unwrap();
|
|
|
|
|
|
|
|
// todo dedupe
|
|
|
|
let animation_frames = lines[1..].join("");
|
|
|
|
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();
|
|
|
|
|
2020-04-13 13:43:23 +00:00
|
|
|
Sprite { id, name, animation_frames, dialogue_id, room_id, position }
|
2020-04-12 12:35:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for Sprite {
|
|
|
|
#[inline]
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
format!(
|
|
|
|
"SPR {}\n{}{}{}\nPOS {} {}",
|
2020-04-13 13:43:23 +00:00
|
|
|
self.id.to_base36(),
|
2020-04-12 12:35:17 +00:00
|
|
|
self.animation_frames.to_string(),
|
2020-04-13 18:08:21 +00:00
|
|
|
self.name_line(),
|
|
|
|
self.dialogue_line(),
|
2020-04-13 13:43:23 +00:00
|
|
|
self.room_id.to_base36(),
|
2020-04-12 12:35:17 +00:00
|
|
|
self.position.to_string(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sprite_from_string() {
|
|
|
|
let output = Sprite::from(
|
2020-04-13 23:17:40 +00:00
|
|
|
include_str!("test-resources/sprite").to_string()
|
2020-04-12 12:35:17 +00:00
|
|
|
);
|
|
|
|
|
2020-04-12 12:50:07 +00:00
|
|
|
let expected = mock::sprite();
|
2020-04-12 12:35:17 +00:00
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sprite_to_string() {
|
2020-04-13 23:17:40 +00:00
|
|
|
assert_eq!(mock::sprite().to_string(), include_str!("test-resources/sprite").to_string());
|
2020-04-12 12:35:17 +00:00
|
|
|
}
|