From 11382a293ef45a1e19cd14649de79ad5b340b1ee Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 17 May 2021 20:50:32 +0100 Subject: [PATCH] rework Room and rename to Scene --- src/lib.rs | 7 +++-- src/mock.rs | 46 ++++++++++++++++++++++++++++++ src/room.rs | 42 --------------------------- src/scene.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 45 deletions(-) delete mode 100644 src/room.rs create mode 100644 src/scene.rs diff --git a/src/lib.rs b/src/lib.rs index 1bd8d01..134fce7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,19 +1,20 @@ use std::fs; use std::path::PathBuf; use serde_derive::{Serialize, Deserialize}; +use crate::config::Config; mod colour; mod config; mod image; mod mock; mod palette; -mod room; +mod scene; pub use colour::Colour; pub use palette::Palette; -use crate::config::Config; +pub use scene::Scene; -#[derive(Eq, Hash, PartialEq)] +#[derive(Debug, Eq, Hash, PartialEq)] pub struct Position { x: u8, y: u8, diff --git a/src/mock.rs b/src/mock.rs index 5c4e914..13fd3c6 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -100,3 +100,49 @@ pub(crate) mod palette { } } } + +pub(crate) mod scenes { + use crate::Scene; + + pub(crate) fn zero() -> Scene { + Scene { + name: "zero".into(), + background: vec![ + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), None, + None, Some("block".into()), None, None, None, None, None, None, None, None, None, None, None, None, Some("block".into()), None, + None, Some("block".into()), None, None, None, None, None, None, None, None, None, None, None, None, Some("block".into()), None, + None, Some("block".into()), None, None, None, None, None, None, None, None, None, None, None, None, Some("block".into()), None, + None, Some("block".into()), None, None, None, None, None, None, None, None, None, None, None, None, Some("block".into()), None, + None, Some("block".into()), None, None, None, None, None, None, None, None, None, None, None, None, Some("block".into()), None, + None, Some("block".into()), None, None, None, None, None, None, None, None, None, None, None, None, Some("block".into()), None, + None, Some("block".into()), None, None, None, None, None, None, None, None, None, None, None, None, Some("block".into()), None, + None, Some("block".into()), None, None, None, None, None, None, None, None, None, None, None, None, Some("block".into()), None, + None, Some("block".into()), None, None, None, None, None, None, None, None, None, None, None, None, Some("block".into()), None, + None, Some("block".into()), None, None, None, None, None, None, None, None, None, None, None, None, Some("block".into()), None, + None, Some("block".into()), None, None, None, None, None, None, None, None, None, None, None, None, Some("block".into()), None, + None, Some("block".into()), None, None, None, None, None, None, None, None, None, None, None, None, Some("block".into()), None, + None, Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), Some("block".into()), None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + ], + foreground: vec![ + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, Some("avatar".into()), None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, Some("cat".into()), None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, + ] + } + } +} diff --git a/src/room.rs b/src/room.rs deleted file mode 100644 index d93db5b..0000000 --- a/src/room.rs +++ /dev/null @@ -1,42 +0,0 @@ -// use std::collections::HashMap; - -use serde_derive::{Serialize, Deserialize}; - -// use crate::Position; - -// pub struct Room { -// pub name: String, -// pub width: u8, -// pub height: u8, -// /// thing names and their positions -// pub background: HashMap, -// pub foreground: HashMap, -// } - -/// todo &str? -#[derive(Serialize, Deserialize)] -struct IntermediateRoom { - name: String, - 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![] -// } -// } -// } diff --git a/src/scene.rs b/src/scene.rs new file mode 100644 index 0000000..a503d84 --- /dev/null +++ b/src/scene.rs @@ -0,0 +1,80 @@ +use std::path::PathBuf; +use serde_derive::{Serialize, Deserialize}; +use crate::Position; +use std::fs::read_to_string; + +#[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); + } +}