From 84c70cee089b72720a85d285ee0437b65be298b9 Mon Sep 17 00:00:00 2001 From: troido Date: Sun, 5 Apr 2020 13:19:27 +0200 Subject: interactions can have an argument --- src/components/interactable.rs | 25 +++++++++++++++++++------ src/controls.rs | 27 ++++++++++++--------------- src/systems/interact.rs | 15 +++++++++++---- 3 files changed, 42 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/components/interactable.rs b/src/components/interactable.rs index 3c8f918..a59cc90 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -13,18 +13,31 @@ use crate::{ pub enum Interactable { Harvest, Change(Template), - Say(String) + Say(String), + Reply(String) } +use Interactable::*; + impl Interactable { pub fn from_json(val: &Value) -> Option { let typ = if val.is_string() {val} else {val.get(0)?}; let arg = if val.is_string() {&Value::Null} else {val.get(1)?}; - match typ.as_str()? { - "harvest" => Some(Interactable::Harvest), - "change" => Some(Interactable::Change(Template::from_json(arg).ok()?)), - "say" => Some(Interactable::Say(arg.as_str()?.to_string())), - _ => None + Some(match typ.as_str()? { + "harvest" => Harvest, + "change" => Change(Template::from_json(arg).ok()?), + "say" => Say(arg.as_str()?.to_string()), + "reply" => Reply(arg.as_str()?.to_string()), + _ => None? + }) + } + + pub fn accepts_arg(&self, arg: &Option) -> bool { + match self { + Harvest => arg.is_none(), + Change(_) => arg.is_none(), + Say(_) => arg.is_none(), + Reply(_) => arg.is_some(), } } } diff --git a/src/controls.rs b/src/controls.rs index 63f418a..7ed8374 100644 --- a/src/controls.rs +++ b/src/controls.rs @@ -48,7 +48,7 @@ pub enum Control { Use(usize), Attack(Vec), AttackTarget(Entity), - Interact(Vec) + Interact(Vec, Option), } @@ -77,26 +77,23 @@ impl Control { } Control::Use(rank as usize) }), - "attack" => Some(Control::Attack({ - let mut directions = Vec::new(); - for dir in val.get(1)?.as_array()? { - directions.push(Direction::from_json(dir)?); - } - directions - })), - "interact" => Some(Control::Interact({ - let mut directions = Vec::new(); - for dir in val.get(1)?.as_array()? { - directions.push(Direction::from_json(dir)?); - } - directions - })), + "attack" => Some(Control::Attack( + parse_directions(val.get(1)?)? + )), + "interact" => Some(Control::Interact( + parse_directions(val.get(1)?)?, + val.get(2).map(|v| Some(v.as_str()?.to_string())).flatten() + )), _ => None } } else {None} } } +fn parse_directions(val: &Value) -> Option> { + val.as_array()?.into_iter().map(Direction::from_json).collect() +} + #[derive(Debug, Clone)] pub enum Action { Join(PlayerId), diff --git a/src/systems/interact.rs b/src/systems/interact.rs index b932fd5..4bf2e4c 100644 --- a/src/systems/interact.rs +++ b/src/systems/interact.rs @@ -45,20 +45,22 @@ impl <'a> System<'a> for Interact { for (entity, controller, position) in (&entities, &controllers, &positions).join(){ let mut target = None; match &controller.control { - Control::Interact(directions) => { + Control::Interact(directions, arg) => { 'targets: for direction in directions { let pos = position.pos + direction.to_position(); for ent in ground.cells.get(&pos).unwrap_or(&HashSet::new()) { if let Some(interactable) = interactables.get(*ent) { - target = Some((*ent, interactable, pos)); - break 'targets; + if interactable.accepts_arg(arg){ + target = Some((*ent, interactable, pos, arg.clone())); + break 'targets; + } } } } } _ => {} } - if let Some((ent, interactable, pos)) = target { + if let Some((ent, interactable, pos, arg)) = target { match interactable { Interactable::Harvest => { deads.insert(ent, Dead).unwrap(); @@ -72,6 +74,11 @@ impl <'a> System<'a> for Interact { ear.sounds.push(Sound{source: None, text: text.clone()}); } } + Interactable::Reply(text) => { + if let Some(ear) = ears.get_mut(entity) { + ear.sounds.push(Sound{source: None, text: text.replace("{}", &arg.unwrap())}); + } + } } cooldowns.insert(entity, ControlCooldown{amount: 2}).unwrap(); } -- cgit