Files
peachy/src/tile.rs

52 lines
1.3 KiB
Rust
Raw Permalink Normal View History

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>,
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();
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>,
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() {
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);
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);
}
}