diff options
| author | troido <troido@protonmail.com> | 2020-04-05 13:19:27 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-05 13:19:27 +0200 |
| commit | 84c70cee089b72720a85d285ee0437b65be298b9 (patch) | |
| tree | cf93ce44eadcb430e1b425a882659303a2b0382d /src/controls.rs | |
| parent | 068f98cec100772defce8ba966e5b917558b191c (diff) | |
interactions can have an argument
Diffstat (limited to 'src/controls.rs')
| -rw-r--r-- | src/controls.rs | 27 |
1 files changed, 12 insertions, 15 deletions
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<Direction>), AttackTarget(Entity), - Interact(Vec<Direction>) + Interact(Vec<Direction>, Option<String>), } @@ -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<Vec<Direction>> { + val.as_array()?.into_iter().map(Direction::from_json).collect() +} + #[derive(Debug, Clone)] pub enum Action { Join(PlayerId), |
