diff options
| author | troido <troido@protonmail.com> | 2020-04-05 20:04:33 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-05 20:04:33 +0200 |
| commit | 48c24ec8b011d081550dc78329cbe61de67b30e9 (patch) | |
| tree | d2d700897dc5ba3d0f52e8a1cd57c0f4880272fd /src/item.rs | |
| parent | 84c70cee089b72720a85d285ee0437b65be298b9 (diff) | |
items are now mostly replaced by itemids, with a mapping to the item in the encyclopedia
Diffstat (limited to 'src/item.rs')
| -rw-r--r-- | src/item.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/item.rs b/src/item.rs new file mode 100644 index 0000000..d5ffaa1 --- /dev/null +++ b/src/item.rs @@ -0,0 +1,74 @@ + + +use std::collections::HashSet; +use serde_json::{Value}; +use specs::{Component, DenseVecStorage}; +use crate::{ + Template, + components::{ + Flag, + equipment::Equippable + } +}; + + + +#[derive(Debug, Default, PartialEq, Eq, Clone, Hash)] +pub struct ItemId(pub String); + +#[derive(Debug, Clone)] +pub struct Item { + pub ent: Template, + pub name: String, + pub action: ItemAction +} + +#[derive(Debug, Clone, PartialEq)] +pub enum ItemAction { + Eat(i64), + Build(Template, HashSet<Flag>, HashSet<Flag>), + Equip(Equippable), + None +} + +use ItemAction::{Eat, Build, Equip, None}; + +impl ItemAction { + + pub fn from_json(val: &Value) -> Option<Self> { + let typ = val.get(0)?; + let arg = val.get(1)?; + Some(match typ.as_str()? { + "eat" => Eat(arg.as_i64()?), + "build" => Build( + Template::from_json(arg.get(0)?).ok()?, + arg.get(1)?.as_array()?.into_iter().map(|v| Flag::from_str(v.as_str()?)).collect::<Option<HashSet<Flag>>>()?, + arg.get(2)?.as_array()?.into_iter().map(|v| Flag::from_str(v.as_str()?)).collect::<Option<HashSet<Flag>>>()? + ), + "none" => None, + "equip" => Equip(Equippable::from_json(arg)?), + _ => {return Option::None} + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::hashmap; + use crate::components::equipment::*; + use serde_json::json; + + #[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 + ); + } +} + |
