2021-05-18 22:09:08 +01:00
|
|
|
use serde_derive::{Serialize, Deserialize};
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::fs::read_to_string;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
|
|
|
pub struct Tile {
|
|
|
|
|
pub name: String,
|
|
|
|
|
/// these will animate
|
|
|
|
|
/// todo should there be animation options? reverse, random, etc?
|
|
|
|
|
/// todo do we need a "current frame" property or leave that up to the player implementation?
|
|
|
|
|
pub images: Vec<String>,
|
2021-05-19 10:26:37 +01:00
|
|
|
pub wall: bool,
|
2021-05-18 22:09:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Tile {
|
|
|
|
|
pub fn from_file(path: PathBuf) -> Tile {
|
|
|
|
|
let name = path.file_stem().unwrap().to_str().unwrap().into();
|
|
|
|
|
|
|
|
|
|
let intermediate: IntermediateTile = toml::from_str(
|
|
|
|
|
&read_to_string(path).unwrap()
|
|
|
|
|
).unwrap();
|
|
|
|
|
|
2021-05-19 10:26:37 +01:00
|
|
|
Tile { name, images: intermediate.images, wall: intermediate.wall }
|
2021-05-18 22:09:08 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
|
|
|
struct IntermediateTile {
|
|
|
|
|
images: Vec<String>,
|
2021-05-19 10:26:37 +01:00
|
|
|
wall: bool,
|
2021-05-18 22:09:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use crate::tile::Tile;
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-05-18 22:26:39 +01:00
|
|
|
fn from_file() {
|
2021-05-19 10:26:37 +01:00
|
|
|
let path = PathBuf::from("src/test-resources/basic/tiles/block.toml");
|
2021-05-18 22:09:08 +01:00
|
|
|
let output = Tile::from_file(path);
|
2021-05-19 10:26:37 +01:00
|
|
|
|
|
|
|
|
let expected = Tile {
|
|
|
|
|
name: "block".into(),
|
|
|
|
|
images: vec!["block".to_string()],
|
|
|
|
|
wall: false
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-18 22:09:08 +01:00
|
|
|
assert_eq!(output, expected);
|
|
|
|
|
}
|
|
|
|
|
}
|