tile struct

This commit is contained in:
Max Bradbury 2021-05-18 22:09:08 +01:00
parent f2d23a54d2
commit d6ae708a5c
3 changed files with 45 additions and 0 deletions

View File

@ -8,6 +8,7 @@ mod mock;
mod music;
mod palette;
mod scene;
mod tile;
pub use colour::Colour;
pub use config::Config;

View File

@ -0,0 +1 @@
images = ["avatar"]

43
src/tile.rs Normal file
View File

@ -0,0 +1,43 @@
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);
}
}