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/controls.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'src/controls.rs') 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), -- cgit