bitsy-parser/src/tile.rs

126 lines
3.2 KiB
Rust
Raw Normal View History

2020-04-18 15:58:30 +00:00
use crate::{from_base36, optional_data_line, AnimationFrames, Image, ToBase36};
2020-04-12 11:53:11 +00:00
#[derive(Debug, Eq, PartialEq)]
pub struct Tile {
pub id: u64,
2020-04-12 11:53:11 +00:00
pub name: Option<String>,
pub wall: Option<bool>, // this is "optional" in that a tile can have `WAL true|false` or neither
2020-04-12 11:53:11 +00:00
pub animation_frames: Vec<Image>,
pub colour_id: Option<u64>,
2020-04-12 11:53:11 +00:00
}
2020-04-13 18:25:49 +00:00
impl Tile {
fn name_line(&self) -> String {
optional_data_line("NAME", self.name.as_ref())
}
fn wall_line(&self) -> String {
if self.wall.is_some() {
format!("\nWAL {}", self.wall.unwrap())
} else {
"".to_string()
}
}
fn colour_line(&self) -> String {
if self.colour_id.is_some() {
format!("\nCOL {}", self.colour_id.unwrap())
} else {
"".to_string()
}
}
2020-04-13 18:25:49 +00:00
}
2020-04-12 11:53:11 +00:00
impl From<String> for Tile {
2020-04-13 12:30:55 +00:00
#[inline]
2020-04-12 11:53:11 +00:00
fn from(string: String) -> Tile {
let mut lines: Vec<&str> = string.lines().collect();
let id = from_base36(&lines[0].replace("TIL ", ""));
2020-04-12 11:53:11 +00:00
let mut wall = None;
let mut name = None;
let mut colour_id = None;
2020-04-12 11:53:11 +00:00
loop {
let last_line = lines.pop().unwrap();
if last_line.starts_with("WAL") {
wall = Some(last_line.ends_with("true"));
} else if last_line.starts_with("NAME") {
name = Some(last_line.replace("NAME ", "").to_string());
} else if last_line.starts_with("COL") {
colour_id = Some(last_line.replace("COL ", "").parse().unwrap());
} else {
2020-04-12 11:53:11 +00:00
lines.push(last_line);
break;
2020-04-12 11:53:11 +00:00
}
}
2020-04-12 11:53:11 +00:00
let animation_frames = lines[1..].join("");
let animation_frames: Vec<&str> = animation_frames.split(">").collect();
2020-04-18 15:58:30 +00:00
let animation_frames: Vec<Image> = animation_frames
.iter()
.map(|&frame| Image::from(frame.to_string()))
.collect();
Tile {
id,
name,
wall,
animation_frames,
colour_id,
}
2020-04-12 11:53:11 +00:00
}
}
impl ToString for Tile {
#[inline]
fn to_string(&self) -> String {
format!(
"TIL {}\n{}{}{}{}",
self.id.to_base36(),
2020-04-12 11:53:11 +00:00
self.animation_frames.to_string(),
2020-04-13 18:25:49 +00:00
self.name_line(),
self.wall_line(),
self.colour_line(),
2020-04-12 11:53:11 +00:00
)
}
}
#[test]
fn test_tile_from_string() {
2020-04-13 23:17:40 +00:00
let output = Tile::from(include_str!("test-resources/tile").to_string());
2020-04-12 11:53:11 +00:00
let expected = Tile {
id: 35,
2020-04-12 11:53:11 +00:00
name: Some("concrete 1".to_string()),
wall: Some(true),
2020-04-18 15:58:30 +00:00
animation_frames: vec![Image {
pixels: vec![1; 64],
}],
colour_id: None,
2020-04-12 11:53:11 +00:00
};
assert_eq!(output, expected);
}
#[test]
fn test_tile_to_string() {
let output = Tile {
id: 262,
2020-04-12 11:53:11 +00:00
name: Some("chequers".to_string()),
wall: None,
2020-04-12 11:53:11 +00:00
animation_frames: vec![
crate::mock::image::chequers_1(),
crate::mock::image::chequers_2(),
],
2020-04-18 15:58:30 +00:00
colour_id: None,
}
.to_string();
2020-04-12 11:53:11 +00:00
2020-04-13 23:17:40 +00:00
let expected = include_str!("test-resources/tile-chequers").to_string();
2020-04-12 11:53:11 +00:00
assert_eq!(output, expected);
}