summaryrefslogtreecommitdiff
path: root/src/components/item.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-03-05 12:50:25 +0100
committertroido <troido@protonmail.com>2020-03-05 12:50:25 +0100
commit170741fe959d30ee43ce689fd5fbae725cc1dae4 (patch)
treede337876fb9afc3ca20e5a99ef61689813f51ebe /src/components/item.rs
parentea99b86b89659624133a63f03600f0b57592a5f4 (diff)
equipent now kinda works
Diffstat (limited to 'src/components/item.rs')
-rw-r--r--src/components/item.rs28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/components/item.rs b/src/components/item.rs
index bcc672a..9e61567 100644
--- a/src/components/item.rs
+++ b/src/components/item.rs
@@ -2,6 +2,8 @@
use specs::{Component, DenseVecStorage};
use crate::{Template};
+use super::equipment::Equippable;
+
#[derive(Component, Debug, Clone)]
pub struct Item {
pub ent: Template,
@@ -17,16 +19,18 @@ use serde_json::{json, Value};
pub enum ItemAction {
Eat(i64),
Build(Template),
+ Equip(Equippable),
None
}
-use ItemAction::{Eat, Build, None};
+use ItemAction::{Eat, Build, Equip, None};
impl ItemAction {
pub fn to_json(&self) -> Value {
match self {
Eat(health) => json!(["eat", health]),
Build(template) => json!(["build", template.to_json()]),
+ Equip(equippable) => json!(["equip", equippable.to_json()]),
None => json!(["none", null])
}
}
@@ -38,7 +42,29 @@ impl ItemAction {
"eat" => Eat(arg.as_i64()?),
"build" => Build(Template::from_json(arg).ok()?),
"none" => None,
+ "equip" => Equip(Equippable::from_json(arg)?),
_ => {return Option::None}
})
}
}
+
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::hashmap;
+ use super::super::equipment::*;
+
+ #[test]
+ fn equip_from_json() {
+ assert_eq!(
+ ItemAction::from_json(&json!(["equip", {"slot": "hand", "stats": {"strength": 10}}])),
+ Some(ItemAction::Equip(Equippable {slot: Slot::Hand, stats: hashmap!(Stat::Strength => 10)}))
+ );
+ assert_eq!(
+ ItemAction::from_json(&json!(["equip", {"slot": "hand", "stats": {"attack": 50}}])),
+ Option::None
+ );
+ }
+}
+