diff options
| author | troido <troido@protonmail.com> | 2020-04-05 23:22:36 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-05 23:22:36 +0200 |
| commit | ff457701ff56072914acb8a7160cd02c2a07095a (patch) | |
| tree | a9f7c9130ce274887924ee140824dc15af061b73 /src/components | |
| parent | 48c24ec8b011d081550dc78329cbe61de67b30e9 (diff) | |
trading now works
Diffstat (limited to 'src/components')
| -rw-r--r-- | src/components/interactable.rs | 29 | ||||
| -rw-r--r-- | src/components/inventory.rs | 9 |
2 files changed, 36 insertions, 2 deletions
diff --git a/src/components/interactable.rs b/src/components/interactable.rs index a59cc90..33e3a12 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -1,11 +1,14 @@ +use std::collections::HashMap; use serde_json::{Value}; use specs::{ Component, HashMapStorage }; use crate::{ - Template + Template, + exchange::Exchange, + ItemId }; #[derive(Component, Debug, Clone, PartialEq)] @@ -14,7 +17,8 @@ pub enum Interactable { Harvest, Change(Template), Say(String), - Reply(String) + Reply(String), + Exchange(String, HashMap<String, Exchange>) } use Interactable::*; @@ -28,6 +32,20 @@ impl Interactable { "change" => Change(Template::from_json(arg).ok()?), "say" => Say(arg.as_str()?.to_string()), "reply" => Reply(arg.as_str()?.to_string()), + "exchange" => Exchange( + arg.get(0)?.as_str()?.to_string(), + arg.get(1)? + .as_object()? + .iter() + .map(|(id, ex)| { + let exchange = Exchange { + cost: ex.get(0)?.as_array()?.iter().map(|i| Some(ItemId(i.as_str()?.to_string()))).collect::<Option<Vec<ItemId>>>()?, + offer: ex.get(1)?.as_array()?.iter().map(|i| Some(ItemId(i.as_str()?.to_string()))).collect::<Option<Vec<ItemId>>>()? + }; + Some((id.clone(), exchange)) + }) + .collect::<Option<HashMap<String, Exchange>>>()? + ), _ => None? }) } @@ -38,6 +56,13 @@ impl Interactable { Change(_) => arg.is_none(), Say(_) => arg.is_none(), Reply(_) => arg.is_some(), + Exchange(prefix, _exchanges) => { + if let Some(txt) = arg { + txt.starts_with(prefix) + } else { + true + } + } } } } diff --git a/src/components/inventory.rs b/src/components/inventory.rs index c3282e9..fa65b03 100644 --- a/src/components/inventory.rs +++ b/src/components/inventory.rs @@ -5,6 +5,7 @@ use crate::{ ItemId, item::{Item, ItemAction}, components::equipment::{Stat, Equippable}, + Encyclopedia }; #[derive(Debug, Clone)] @@ -25,6 +26,14 @@ impl Component for Inventory { impl Inventory { + pub fn add_item(&mut self, itemid: ItemId, enc: &Encyclopedia) { + self.items.insert(0, InventoryEntry{ + itemid: itemid.clone(), + item: enc.get_item(&itemid).unwrap(), + is_equipped: false + }); + } + fn equipped(&self) -> Vec<Equippable> { let mut equippables = Vec::new(); for entry in self.items.iter() { |
