44 lines
1.2 KiB
Rust
44 lines
1.2 KiB
Rust
|
|
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>,
|
||
|
|
}
|
||
|
|
|
||
|
|
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();
|
||
|
|
|
||
|
|
Tile { name, images: intermediate.images }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||
|
|
struct IntermediateTile {
|
||
|
|
images: Vec<String>,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod test {
|
||
|
|
use std::path::PathBuf;
|
||
|
|
use crate::tile::Tile;
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn tile_from_file() {
|
||
|
|
let path = PathBuf::from("src/test-resources/basic/tiles/avatar.toml");
|
||
|
|
let output = Tile::from_file(path);
|
||
|
|
let expected = Tile { name: "avatar".into(), images: vec!["avatar".to_string()] };
|
||
|
|
assert_eq!(output, expected);
|
||
|
|
}
|
||
|
|
}
|