From 7821febc8ee4c89ca1825054e0baf39eea3a0380 Mon Sep 17 00:00:00 2001 From: troido Date: Tue, 11 Feb 2020 12:22:15 +0100 Subject: shortcut for defining visibles --- src/assemblage.rs | 17 +++++++++++- src/main.rs | 81 +++++++++++++++++++++---------------------------------- 2 files changed, 47 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/assemblage.rs b/src/assemblage.rs index 3433a76..f84037c 100644 --- a/src/assemblage.rs +++ b/src/assemblage.rs @@ -4,6 +4,7 @@ use serde_json::{Value, json}; use super::componentparameter::ComponentParameter; use super::parameter::{Parameter, ParameterType}; use super::componentwrapper::{ComponentWrapper, ComponentType}; +use crate::hashmap; type ArgumentDef = (String, ParameterType, Option); @@ -63,10 +64,24 @@ impl Assemblage { } pub fn from_json(val: &Value) -> Result{ - let assemblage = Self { + let mut assemblage = Self { arguments: Self::parse_definition_arguments(val.get("arguments").unwrap_or(&json!([])))?, components: Self::parse_definition_components(val.get("components").ok_or("property 'components' not found")?)? }; + if let Some(spritename) = val.get("sprite") { + let height = val.get("height").ok_or("defining a sprite requires also defining a height")?; + assemblage.components.push(( + ComponentType::Visible, + hashmap!( + "sprite".to_string() => ComponentParameter::Constant( + Parameter::String(spritename.as_str().ok_or("sprite not a string")?.to_string()) + ), + "height".to_string() => ComponentParameter::Constant( + Parameter::Float(height.as_f64().ok_or("height not a float")?) + ) + ) + )); + } assemblage.validate()?; Ok(assemblage) } diff --git a/src/main.rs b/src/main.rs index 15171e9..4afcd0a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,39 +123,31 @@ fn default_assemblages() -> Encyclopedia { Encyclopedia::from_json(json!({ "wall": { "components": [ - ["Blocking", {}], - ["Visible", { - "sprite": ["string", "wall"], - "height": ["float", 2.0] - }] - ] + ["Blocking", {}] + ], + "sprite": "wall", + "height": 2 }, "rock": { "components": [ - ["Blocking", {}], - ["Visible", { - "sprite": ["string", "rock"], - "height": ["float", 10.0] - }] - ] + ["Blocking", {}] + ], + "sprite": "rock", + "height": 10 }, "tree": { "components": [ - ["Blocking", {}], - ["Visible", { - "sprite": ["string", "tree"], - "height": ["float", 3.0] - }] - ] + ["Blocking", {}] + ], + "sprite": "tree", + "height": 3 }, "fence": { "components": [ - ["Blocking", {}], - ["Visible", { - "sprite": ["string", "fence"], - "height": ["float", 1.0] - }] - ] + ["Blocking", {}] + ], + "sprite": "fence", + "height": 1 }, "grass": { "components": [ @@ -189,47 +181,36 @@ fn default_assemblages() -> Encyclopedia { }, "ground": { "components": [ - ["Visible", { - "sprite": ["string", "ground"], - "height": ["float", 0.1] - }], ["Floor", {}] - ] + ], + "sprite": "ground", + "height": 0.1 }, "floor": { "components": [ - ["Visible", { - "sprite": ["string", "floor"], - "height": ["float", 0.1] - }], ["Floor", {}] - ] + ], + "sprite": "floor", + "height": 0.1 }, "bridge": { "components": [ - ["Visible", { - "sprite": ["string", "bridge"], - "height": ["float", 0.1] - }], ["Floor", {}] - ] + ], + "sprite": "bridge", + "height": 0.1 }, "water": { - "components": [ - ["Visible", { - "sprite": ["string", "water"], - "height": ["float", 0.1] - }] - ] + "components": [], + "sprite": "water", + "height": 0.1 }, "pebble": { "components": [ - ["Visible", { - "sprite": ["string", "pebble"], - "height": ["float", 0.4] - }], ["Item", {"ent": ["template", "pebble"], "name": ["string", "pebble"]}] - ] + ], + "sprite": "pebble", + "height": 0.4 }, "player": { "arguments": [["name", "string", null]], -- cgit