From 323cd679cd29a8475c3b7486ce54ecd37620dbea Mon Sep 17 00:00:00 2001 From: troido Date: Tue, 4 Feb 2020 22:23:17 +0100 Subject: tried to implement deserialisation of entities --- src/compwrapper.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/compwrapper.rs (limited to 'src/compwrapper.rs') diff --git a/src/compwrapper.rs b/src/compwrapper.rs new file mode 100644 index 0000000..8028b49 --- /dev/null +++ b/src/compwrapper.rs @@ -0,0 +1,48 @@ + +use std::collections::HashMap; +use specs::{Builder, EntityBuilder}; +use serde_json::Value; +use super::components::{Visible, Blocking, Played}; + + +#[derive(Clone)] +pub enum CompWrapper{ + Visible(Visible), + Blocking(Blocking), + Player(Played) +} + +impl CompWrapper { + + pub fn build<'a>(&self, builder: specs::EntityBuilder<'a>) -> specs::EntityBuilder<'a> { + match self.clone() { + Self::Visible(c) => builder.with(c), + Self::Blocking(c) => builder.with(c), + Self::Player(c) => builder.with(c) + } + } + + pub fn parse_component(data: Value) -> Option { + let a = data.as_array()?; + if a.len() != 2 { + return None + } + let typename = a[0].as_str()?; + let params: HashMap<&str, &Value> = a[1].as_object()?.into_iter().map(|(key, val)| (key.as_str(), val)).collect(); + Self::load_component(typename, params) + } + + pub fn load_component(typename: &str, mut parameters: HashMap<&str, &Value>) -> Option { + match typename { + "Visible" => Some(CompWrapper::Visible(Visible{ + sprite: parameters.remove("sprite")?.as_str()?.to_string(), + height: parameters.remove("height")?.as_f64()? + })), + "Blocking" => Some(CompWrapper::Blocking(Blocking)), + "Player" => Some(CompWrapper::Player(Played::new( + parameters.remove("name")?.as_str()?.to_string() + ))), + _ => None + } + } +} -- cgit