tile struct
This commit is contained in:
parent
f2d23a54d2
commit
d6ae708a5c
|
@ -8,6 +8,7 @@ mod mock;
|
|||
mod music;
|
||||
mod palette;
|
||||
mod scene;
|
||||
mod tile;
|
||||
|
||||
pub use colour::Colour;
|
||||
pub use config::Config;
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
images = ["avatar"]
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue