use std::fs::read_to_string;
use std::path::PathBuf;
use serde_derive::{Serialize, Deserialize};
#[derive(Debug, Eq, PartialEq)]
pub struct Scene {
pub name: String,
// rename to "tiles"?
pub background: Vec>,
// rename to "things"?
pub foreground: Vec >,
}
impl Scene {
pub fn from_file(path: PathBuf) -> Self {
let name = path.file_stem().unwrap().to_str().unwrap().into();
let data = read_to_string(path).unwrap();
let intermediate: IntermediateScene = toml::from_str(&data).unwrap();
let mut background = Vec::new();
let mut foreground = Vec::new();
for name in intermediate.background.iter() {
match name.as_ref() {
"" => background.push(None),
_ => background.push(Some(name.to_owned())),
}
}
for name in intermediate.foreground.iter() {
match name.as_ref() {
"" => foreground.push(None),
_ => foreground.push(Some(name.to_owned())),
}
}
Self { name, background, foreground }
}
}
#[derive(Serialize, Deserialize)]
struct IntermediateScene {
background: Vec,
foreground: Vec,
}
// impl IntermediateRoom {
// fn from(_room: Room) -> IntermediateRoom {
// fn hashmap_to_vec(_hash: HashMap, width: u8, height: u8) -> Vec {
// let mut thing_ids = Vec::new();
//
// while thing_ids.len() < (width * height) as usize {
// thing_ids.push(String::new());
// }
//
// thing_ids
// }
//
// IntermediateRoom {
// name: "".to_string(),
// background: vec![],
// foreground: vec![]
// }
// }
// }
#[cfg(test)]
mod test {
use std::path::PathBuf;
use crate::scene::Scene;
#[test]
fn scene_from_file() {
let path = PathBuf::from("src/test-resources/basic/scenes/zero.toml");
let output = Scene::from_file(path);
let expected = crate::mock::scenes::zero();
assert_eq!(output, expected);
}
}