diff options
| author | troido <troido@protonmail.com> | 2020-02-07 19:10:13 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-07 19:10:13 +0100 |
| commit | 940b1c762bb98a56dddc6e3e7f208867abb3ebe5 (patch) | |
| tree | f0535ace5a5a9643fb735f1970fa1b338ec9fea9 | |
| parent | 3d0d1279627aeee984fa29546aeee6855c9ef2c9 (diff) | |
added roomtemplate
| -rw-r--r-- | src/main.rs | 5 | ||||
| -rw-r--r-- | src/pos.rs | 13 | ||||
| -rw-r--r-- | src/resources.rs | 4 | ||||
| -rw-r--r-- | src/room.rs | 2 | ||||
| -rw-r--r-- | src/roomtemplate.rs | 76 | ||||
| -rw-r--r-- | src/systems/view.rs | 2 | ||||
| -rw-r--r-- | src/template.rs | 4 | ||||
| -rw-r--r-- | src/worldmessages.rs | 4 |
8 files changed, 97 insertions, 13 deletions
diff --git a/src/main.rs b/src/main.rs index 1f1d217..410a9aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ mod assemblage; mod componentparameter; mod encyclopedia; mod template; +mod roomtemplate; use self::gameserver::GameServer; use self::server::unixserver::UnixServer; @@ -31,7 +32,7 @@ use self::room::Room; use self::util::ToJson; use self::encyclopedia::Encyclopedia; use self::template::Template; -use self::pos::Pos; +pub use self::pos::Pos; @@ -64,7 +65,7 @@ fn main() { } } -fn gen_room<'a, 'b>(width: i32, height: i32) -> Room<'a, 'b> { +fn gen_room<'a, 'b>(width: i64, height: i64) -> Room<'a, 'b> { let assemblages = default_assemblages(); let mut room = Room::new(assemblages.clone(), (width, height)); let wall = &Template::empty("wall"); @@ -7,14 +7,14 @@ use super::util::{clamp, ToJson}; #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] pub struct Pos { - pub x: i32, - pub y: i32 + pub x: i64, + pub y: i64 } impl Pos { - pub fn new(x: i32, y: i32) -> Pos { + pub fn new(x: i64, y: i64) -> Pos { Pos {x, y} } @@ -24,6 +24,13 @@ impl Pos { y: clamp(self.y, smaller.y, larger.y) } } + + pub fn from_json(val: &Value) -> Option<Self>{ + Some(Pos { + x: val.get(0)?.as_i64()?, + y: val.get(1)?.as_i64()? + }) + } } diff --git a/src/resources.rs b/src/resources.rs index b1c193c..ced9897 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -21,8 +21,8 @@ pub struct Output { #[derive(Default)] pub struct Size { - pub width: i32, - pub height: i32 + pub width: i64, + pub height: i64 } #[derive(Default)] diff --git a/src/room.rs b/src/room.rs index a953a04..5daad29 100644 --- a/src/room.rs +++ b/src/room.rs @@ -41,7 +41,7 @@ pub struct Room<'a, 'b> { impl <'a, 'b>Room<'a, 'b> { - pub fn new(encyclopedia: Encyclopedia, size: (i32, i32)) -> Room<'a, 'b> { + pub fn new(encyclopedia: Encyclopedia, size: (i64, i64)) -> Room<'a, 'b> { let (width, height) = size; let mut world = World::new(); world.insert(Size{width, height}); diff --git a/src/roomtemplate.rs b/src/roomtemplate.rs new file mode 100644 index 0000000..2a71341 --- /dev/null +++ b/src/roomtemplate.rs @@ -0,0 +1,76 @@ + +use std::collections::HashMap; +use serde_json::Value; +use crate::Pos; +use crate::template::Template; + +struct RoomTemplate { + size: (i64, i64), + spawn: Pos, + field: Vec<Vec<Template>> +} + +impl RoomTemplate { + + pub fn from_json(jsonroom: &Value) -> Result<RoomTemplate, &'static str>{ + let size = ( + jsonroom.get("width").ok_or("no with")?.as_i64().ok_or("with not a number")?, + jsonroom.get("height").ok_or("no height")?.as_i64().ok_or("height not a number")? + ); + let spawn = Pos::from_json(jsonroom.get("spawn").ok_or("no spawn")?).ok_or("spawn not a pos")?; + + let mut mapping = HashMap::new(); + for (key, value) in jsonroom.get("mapping").ok_or("no mapping")?.as_object().ok_or("mapping not a json object")?.iter() { + let mut templates: Vec<Template> = Vec::new(); + if value.is_array() { + for template in value.as_array().ok_or("weird")? { + templates.push(Template::from_json(template).ok_or("not a valid template")?); + } + } else { + templates.push(Template::from_json(value).ok_or("not a valid template")?); + } + mapping.insert(key.chars().next().ok_or("mapping key is empty string")?, templates); + } + + let mut field = Vec::new(); + field.resize((size.0 * size.1) as usize, Vec::new()); + let jsonfield: &Vec<Value> = jsonroom.get("field").ok_or("no field")?.as_array().ok_or("field not an array")?; + for (y, row) in jsonfield.iter().enumerate() { + for (x, ch) in row.as_str().ok_or("field row not a string")?.chars().enumerate() { + field[x + y * (size.0 as usize)] = mapping.get(&ch).ok_or("char not found in mapping")?.clone(); + } + } + Ok(RoomTemplate { + size, + spawn, + field + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn simple_from_json() { + RoomTemplate::from_json(&json!({ + "width": 6, + "height": 5, + "spawn": [1, 1], + "field": [ + "######", + "#,,,,#", + "#,,,,#", + "#....#", + "######" + ], + "mapping": { + "#": ["wall"], + ",": "grass", + ".": {"type": "grass", "args": [], "kwargs": {}} + } + })).unwrap(); + } +} diff --git a/src/systems/view.rs b/src/systems/view.rs index 9a4dbf9..9ab6faf 100644 --- a/src/systems/view.rs +++ b/src/systems/view.rs @@ -103,7 +103,7 @@ impl <'a> System<'a> for View { } } -fn draw_room(cells: HashMap<Pos, Vec<Visible>>, (width, height): (i32, i32)) -> (Vec<usize>, Vec<Vec<String>>){ +fn draw_room(cells: HashMap<Pos, Vec<Visible>>, (width, height): (i64, i64)) -> (Vec<usize>, Vec<Vec<String>>){ let size = width * height; let mut values :Vec<usize> = Vec::with_capacity(size as usize); diff --git a/src/template.rs b/src/template.rs index 24e672b..4de02f6 100644 --- a/src/template.rs +++ b/src/template.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; use serde_json::{json, Value}; use crate::parameter::Parameter; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Template { pub name: String, pub args: Vec<Parameter>, @@ -26,7 +26,7 @@ impl Template { Self::new(name, HashMap::new()) } - pub fn from_json(val: Value) -> Option<Template> { + pub fn from_json(val: &Value) -> Option<Template> { if val.is_string(){ return Some(Self::empty(val.as_str()?)); } diff --git a/src/worldmessages.rs b/src/worldmessages.rs index 8d9d8c8..79cd400 100644 --- a/src/worldmessages.rs +++ b/src/worldmessages.rs @@ -35,8 +35,8 @@ impl ToJson for WorldUpdate { #[derive(Clone, Serialize)] pub struct FieldMessage { - pub width: i32, - pub height: i32, + pub width: i64, + pub height: i64, pub field: Vec<usize>, pub mapping: Vec<Vec<String>> } |
