bitsy-parser/src/sprite.rs

150 lines
4.2 KiB
Rust
Raw Normal View History

2020-04-18 15:58:30 +00:00
use crate::{from_base36, optional_data_line, AnimationFrames, Image, Position, ToBase36};
2020-04-29 07:27:35 +00:00
use crate::image::animation_frames_from_string;
2020-04-29 17:33:22 +00:00
use std::str::FromStr;
2020-04-12 12:35:17 +00:00
#[derive(Debug, Eq, PartialEq)]
pub struct Sprite {
pub id: u64,
pub name: Option<String>,
pub animation_frames: Vec<Image>,
pub dialogue_id: Option<String>,
pub room_id: Option<u64>,
pub position: Option<Position>,
pub colour_id: Option<u64>,
pub items: Vec<String>,
2020-04-12 12:35:17 +00:00
}
impl Sprite {
#[inline]
fn name_line(&self) -> String {
optional_data_line("NAME", self.name.as_ref())
}
#[inline]
fn dialogue_line(&self) -> String {
optional_data_line("DLG", self.dialogue_id.as_ref())
}
#[inline]
fn room_position_line(&self) -> String {
if self.room_id.is_some() && self.position.is_some() {
2020-04-18 15:58:30 +00:00
format!(
"\nPOS {} {}",
self.room_id.unwrap().to_base36(),
self.position.as_ref().unwrap().to_string()
)
} else {
"".to_string()
}
}
#[inline]
fn colour_line(&self) -> String {
optional_data_line("COL", self.colour_id.as_ref())
}
#[inline]
fn item_lines(&self) -> String {
if self.items.len() == 0 {
"".to_string()
} else {
let lines: Vec<String> = self.items.iter().map(|item| format!("ITM {}", item)).collect();
format!("\n{}", lines.join("\n"))
}
}
}
2020-04-12 12:35:17 +00:00
impl From<String> for Sprite {
#[inline]
2020-04-12 12:35:17 +00:00
fn from(string: String) -> Sprite {
let mut lines: Vec<&str> = string.lines().collect();
let id = from_base36(&lines[0].replace("SPR ", ""));
2020-04-12 12:35:17 +00:00
let mut name = None;
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;
let mut colour_id: Option<u64> = None;
let mut items: Vec<String> = Vec::new();
2020-04-12 12:35:17 +00:00
loop {
2020-04-12 12:35:17 +00:00
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") {
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();
room_id = Some(from_base36(&room_position[0]));
if room_position.len() < 2 {
panic!("Bad room/position for sprite: {}", string);
}
2020-04-29 17:33:22 +00:00
position = Some(Position::from_str(room_position[1]).unwrap());
} else if last_line.starts_with("COL") {
colour_id = Some(last_line.replace("COL ", "").parse().unwrap());
} else if last_line.starts_with("ITM") {
items.push(last_line.replace("ITM ", ""));
2020-04-12 12:35:17 +00:00
} else {
lines.push(last_line);
break;
}
}
items.reverse();
2020-04-29 07:27:35 +00:00
let animation_frames = animation_frames_from_string(
lines[1..].join("\n")
);
2020-04-18 15:58:30 +00:00
Sprite {
id,
name,
animation_frames,
dialogue_id,
room_id,
position,
colour_id,
items
2020-04-18 15:58:30 +00:00
}
2020-04-12 12:35:17 +00:00
}
}
impl ToString for Sprite {
#[inline]
fn to_string(&self) -> String {
format!(
"SPR {}\n{}{}{}{}{}{}",
self.id.to_base36(),
2020-04-12 12:35:17 +00:00
self.animation_frames.to_string(),
self.name_line(),
self.dialogue_line(),
self.room_position_line(),
self.colour_line(),
self.item_lines(),
2020-04-12 12:35:17 +00:00
)
}
}
2020-04-19 07:13:55 +00:00
#[cfg(test)]
mod test {
use crate::mock;
use crate::sprite::Sprite;
2020-04-12 12:35:17 +00:00
2020-04-19 07:13:55 +00:00
#[test]
fn test_sprite_from_string() {
let output = Sprite::from(include_str!("test-resources/sprite").to_string());
let expected = mock::sprite();
2020-04-12 12:35:17 +00:00
2020-04-19 07:13:55 +00:00
assert_eq!(output, expected);
}
2020-04-12 12:35:17 +00:00
2020-04-19 07:13:55 +00:00
#[test]
fn test_sprite_to_string() {
assert_eq!(mock::sprite().to_string(), include_str!("test-resources/sprite").to_string());
}
2020-04-12 12:35:17 +00:00
}