From 7e9784f23d1920640152d1bc99a0725f6f22d529 Mon Sep 17 00:00:00 2001 From: troido Date: Fri, 21 Feb 2020 20:01:24 +0100 Subject: visible component now also has a name --- src/assemblage.rs | 5 +++++ src/components.rs | 3 ++- src/componentwrapper.rs | 5 +++-- src/defaultencyclopedia.rs | 9 ++++++--- src/playerstate.rs | 2 +- 5 files changed, 17 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/assemblage.rs b/src/assemblage.rs index dd84837..bdd6c25 100644 --- a/src/assemblage.rs +++ b/src/assemblage.rs @@ -74,8 +74,10 @@ impl Assemblage { 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")?)? }; + // visible component is so common that shortcuts are very helpful if let Some(spritename) = val.get("sprite") { let height = val.get("height").ok_or("defining a sprite requires also defining a height")?; + let name = val.get("name").unwrap_or(spritename); assemblage.components.push(( ComponentType::Visible, hashmap!( @@ -84,6 +86,9 @@ impl Assemblage { ), "height".to_string() => ComponentParameter::Constant( Parameter::Float(height.as_f64().ok_or("height not a float")?) + ), + "name".to_string() => ComponentParameter::Constant( + Parameter::String(name.as_str().ok_or("name not a string")?.to_string()) ) ) )); diff --git a/src/components.rs b/src/components.rs index 2adddaa..9d387f1 100644 --- a/src/components.rs +++ b/src/components.rs @@ -29,7 +29,8 @@ impl Component for Position { #[derive(Debug, Clone)] pub struct Visible { pub sprite: Sprite, - pub height: f64 + pub height: f64, + pub name: String } impl Component for Visible { type Storage = FlaggedStorage>; diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 8ee4e0b..5b9673f 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -84,10 +84,11 @@ macro_rules! components { } components!( - Visible (sprite: String, height: Float) { + Visible (name: String, sprite: String, height: Float) { Visible { sprite: Sprite{name: sprite}, - height + height, + name } }; Blocking () {Blocking}; diff --git a/src/defaultencyclopedia.rs b/src/defaultencyclopedia.rs index f15f443..9f93e40 100644 --- a/src/defaultencyclopedia.rs +++ b/src/defaultencyclopedia.rs @@ -36,7 +36,8 @@ pub fn default_encyclopedia() -> Encyclopedia { ["string", "grass3"], ["string", "ground"] ]], - "height": ["float", 0.1] + "height": ["float", 0.1], + "name": ["string", "grass"] }], "Floor" ] @@ -49,7 +50,8 @@ pub fn default_encyclopedia() -> Encyclopedia { ["string", "grass2"], ["string", "grass3"] ]], - "height": ["float", 0.1] + "height": ["float", 0.1], + "name": ["string", "grass"] }], "Floor" ] @@ -95,7 +97,8 @@ pub fn default_encyclopedia() -> Encyclopedia { "components": [ ["Visible", { "sprite": ["string", "player"], - "height": ["float", 1.0] + "height": ["float", 1.0], + "name": ["arg", "name"] }], ["Player", { "name": ["arg", "name"] diff --git a/src/playerstate.rs b/src/playerstate.rs index 2103f22..150b9eb 100644 --- a/src/playerstate.rs +++ b/src/playerstate.rs @@ -87,7 +87,7 @@ impl PlayerState { pub fn construct(&self) -> PreEntity { vec![ - ComponentWrapper::Visible(Visible{sprite: Sprite{name: "player".to_string()}, height: 1.0}), + ComponentWrapper::Visible(Visible{sprite: Sprite{name: "player".to_string()}, height: 1.0, name: self.id.name.clone()}), ComponentWrapper::Player(Player::new(self.id.clone())), ComponentWrapper::Inventory(Inventory{ items: self.inventory.iter().map( -- cgit