bitsy-parser/src/tile.rs

134 lines
3.3 KiB
Rust

use crate::{from_base36, optional_data_line, AnimationFrames, Image, ToBase36};
use crate::image::animation_frames_from_string;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Tile {
pub id: String,
pub name: Option<String>,
pub wall: Option<bool>, // this is "optional" in that a tile can have `WAL true|false` or neither
pub animation_frames: Vec<Image>,
pub colour_id: Option<u64>,
}
impl Tile {
#[inline]
fn name_line(&self) -> String {
optional_data_line("NAME", self.name.as_ref())
}
#[inline]
fn wall_line(&self) -> String {
if self.wall.is_some() {
format!("\nWAL {}", self.wall.unwrap())
} else {
"".to_string()
}
}
#[inline]
fn colour_line(&self) -> String {
if self.colour_id.is_some() {
format!("\nCOL {}", self.colour_id.unwrap())
} else {
"".to_string()
}
}
}
impl From<String> for Tile {
#[inline]
fn from(string: String) -> Tile {
let mut lines: Vec<&str> = string.lines().collect();
let id = lines[0].replace("TIL ", "");
let mut wall = None;
let mut name = None;
let mut colour_id = None;
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 {
lines.push(last_line);
break;
}
}
let animation_frames = animation_frames_from_string(
lines[1..].join("\n")
);
Tile {
id,
name,
wall,
animation_frames,
colour_id,
}
}
}
impl ToString for Tile {
#[inline]
fn to_string(&self) -> String {
format!(
"TIL {}\n{}{}{}{}",
self.id,
self.animation_frames.to_string(),
self.name_line(),
self.wall_line(),
self.colour_line(),
)
}
}
#[cfg(test)]
mod test {
use crate::tile::Tile;
use crate::image::Image;
use crate::mock;
#[test]
fn test_tile_from_string() {
let output = Tile::from(include_str!("test-resources/tile").to_string());
let expected = Tile {
id: "z".to_string(),
name: Some("concrete 1".to_string()),
wall: Some(true),
animation_frames: vec![Image {
pixels: vec![1; 64],
}],
colour_id: None,
};
assert_eq!(output, expected);
}
#[test]
fn test_tile_to_string() {
let output = Tile {
id: "7a".to_string(),
name: Some("chequers".to_string()),
wall: None,
animation_frames: vec![
mock::image::chequers_1(),
mock::image::chequers_2(),
],
colour_id: None,
}
.to_string();
let expected = include_str!("test-resources/tile-chequers").to_string();
assert_eq!(output, expected);
}
}