From d6ae708a5c07cac7545725fb88e285169af887c7 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Tue, 18 May 2021 22:09:08 +0100 Subject: [PATCH] tile struct --- src/lib.rs | 1 + src/test-resources/basic/tiles/avatar.toml | 1 + src/tile.rs | 43 ++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 src/test-resources/basic/tiles/avatar.toml create mode 100644 src/tile.rs diff --git a/src/lib.rs b/src/lib.rs index 3ef2f56..c58fdff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ mod mock; mod music; mod palette; mod scene; +mod tile; pub use colour::Colour; pub use config::Config; diff --git a/src/test-resources/basic/tiles/avatar.toml b/src/test-resources/basic/tiles/avatar.toml new file mode 100644 index 0000000..3ac6a9c --- /dev/null +++ b/src/test-resources/basic/tiles/avatar.toml @@ -0,0 +1 @@ +images = ["avatar"] diff --git a/src/tile.rs b/src/tile.rs new file mode 100644 index 0000000..9c77387 --- /dev/null +++ b/src/tile.rs @@ -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, +} + +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, +} + +#[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); + } +}