2020-06-23 15:26:08 +00:00
|
|
|
use crate::{optional_data_line, AnimationFrames, Image};
|
2020-04-29 07:27:35 +00:00
|
|
|
use crate::image::animation_frames_from_string;
|
2020-04-12 11:53:11 +00:00
|
|
|
|
2020-06-29 23:32:59 +00:00
|
|
|
#[derive(Clone, Debug, Eq)]
|
2020-04-12 11:53:11 +00:00
|
|
|
pub struct Tile {
|
2020-06-18 16:47:54 +00:00
|
|
|
pub id: String,
|
2020-04-12 11:53:11 +00:00
|
|
|
pub name: Option<String>,
|
2020-04-13 23:40:14 +00:00
|
|
|
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>,
|
2020-04-13 23:40:14 +00:00
|
|
|
pub colour_id: Option<u64>,
|
2020-04-12 11:53:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 23:32:59 +00:00
|
|
|
impl PartialEq for Tile {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.wall == other.wall
|
|
|
|
&&
|
|
|
|
self.animation_frames == other.animation_frames
|
|
|
|
&&
|
|
|
|
self.colour_id == other.colour_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:25:49 +00:00
|
|
|
impl Tile {
|
|
|
|
fn name_line(&self) -> String {
|
|
|
|
optional_data_line("NAME", self.name.as_ref())
|
|
|
|
}
|
2020-04-13 23:40:14 +00:00
|
|
|
|
|
|
|
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-07-20 18:52:31 +00:00
|
|
|
|
|
|
|
// todo refactor
|
|
|
|
|
|
|
|
pub fn flip(&mut self) {
|
|
|
|
self.animation_frames = self.animation_frames.iter().map(|frame: &Image| {
|
|
|
|
let mut image = frame.clone();
|
|
|
|
image.flip();
|
|
|
|
image
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mirror(&mut self) {
|
|
|
|
self.animation_frames = self.animation_frames.iter().map(|frame: &Image| {
|
|
|
|
let mut image = frame.clone();
|
|
|
|
image.mirror();
|
|
|
|
image
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rotate(&mut self) {
|
|
|
|
self.animation_frames = self.animation_frames.iter().map(|frame: &Image| {
|
|
|
|
let mut image = frame.clone();
|
|
|
|
image.rotate();
|
|
|
|
image
|
|
|
|
}).collect()
|
|
|
|
}
|
2020-04-13 18:25:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 11:53:11 +00:00
|
|
|
impl From<String> for Tile {
|
|
|
|
fn from(string: String) -> Tile {
|
|
|
|
let mut lines: Vec<&str> = string.lines().collect();
|
|
|
|
|
2020-06-18 16:47:54 +00:00
|
|
|
let id = lines[0].replace("TIL ", "");
|
2020-04-12 11:53:11 +00:00
|
|
|
|
2020-04-13 23:40:14 +00:00
|
|
|
let mut wall = None;
|
|
|
|
let mut name = None;
|
|
|
|
let mut colour_id = None;
|
2020-04-12 11:53:11 +00:00
|
|
|
|
2020-04-13 23:40:14 +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);
|
2020-04-13 23:40:14 +00:00
|
|
|
break;
|
2020-04-12 11:53:11 +00:00
|
|
|
}
|
2020-04-13 23:40:14 +00:00
|
|
|
}
|
2020-04-12 11:53:11 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
Tile {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
wall,
|
|
|
|
animation_frames,
|
|
|
|
colour_id,
|
|
|
|
}
|
2020-04-12 11:53:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for Tile {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
format!(
|
2020-04-13 23:40:14 +00:00
|
|
|
"TIL {}\n{}{}{}{}",
|
2020-06-18 16:47:54 +00:00
|
|
|
self.id,
|
2020-04-12 11:53:11 +00:00
|
|
|
self.animation_frames.to_string(),
|
2020-04-13 18:25:49 +00:00
|
|
|
self.name_line(),
|
2020-04-13 23:40:14 +00:00
|
|
|
self.wall_line(),
|
|
|
|
self.colour_line(),
|
2020-04-12 11:53:11 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::tile::Tile;
|
|
|
|
use crate::image::Image;
|
2020-04-29 15:59:00 +00:00
|
|
|
use crate::mock;
|
2020-04-19 07:13:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tile_from_string() {
|
|
|
|
let output = Tile::from(include_str!("test-resources/tile").to_string());
|
|
|
|
|
|
|
|
let expected = Tile {
|
2020-06-18 16:47:54 +00:00
|
|
|
id: "z".to_string(),
|
2020-04-19 07:13:55 +00:00
|
|
|
name: Some("concrete 1".to_string()),
|
|
|
|
wall: Some(true),
|
|
|
|
animation_frames: vec![Image {
|
|
|
|
pixels: vec![1; 64],
|
|
|
|
}],
|
|
|
|
colour_id: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
2020-04-18 15:58:30 +00:00
|
|
|
}
|
2020-04-12 11:53:11 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_tile_to_string() {
|
|
|
|
let output = Tile {
|
2020-06-18 16:47:54 +00:00
|
|
|
id: "7a".to_string(),
|
2020-04-19 07:13:55 +00:00
|
|
|
name: Some("chequers".to_string()),
|
|
|
|
wall: None,
|
|
|
|
animation_frames: vec![
|
2020-04-29 15:59:00 +00:00
|
|
|
mock::image::chequers_1(),
|
|
|
|
mock::image::chequers_2(),
|
2020-04-19 07:13:55 +00:00
|
|
|
],
|
|
|
|
colour_id: None,
|
|
|
|
}
|
|
|
|
.to_string();
|
2020-04-12 11:53:11 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
let expected = include_str!("test-resources/tile-chequers").to_string();
|
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
2020-06-29 23:32:59 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_partial_eq() {
|
|
|
|
let tile_a = crate::mock::tile_default();
|
|
|
|
let mut tile_b = crate::mock::tile_default();
|
|
|
|
tile_b.id = "0".to_string();
|
|
|
|
assert_eq!(tile_a, tile_b);
|
2020-07-19 08:45:46 +00:00
|
|
|
tile_b.name = None;
|
|
|
|
assert_eq!(tile_a, tile_b);
|
2020-06-29 23:32:59 +00:00
|
|
|
}
|
2020-04-12 11:53:11 +00:00
|
|
|
}
|