summaryrefslogtreecommitdiff
path: root/src/compwrapper.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-04 22:23:17 +0100
committertroido <troido@protonmail.com>2020-02-04 22:23:17 +0100
commit323cd679cd29a8475c3b7486ce54ecd37620dbea (patch)
tree1fc55c768a7b74644157c23580f54292e960c66a /src/compwrapper.rs
parenteb5997cbf94b1aa230cf4acb3008a7fe80ec36e0 (diff)
tried to implement deserialisation of entities
Diffstat (limited to 'src/compwrapper.rs')
-rw-r--r--src/compwrapper.rs48
1 files changed, 48 insertions, 0 deletions
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<CompWrapper> {
+ 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<CompWrapper> {
+ 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
+ }
+ }
+}