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 | |
| parent | 068f98cec100772defce8ba966e5b917558b191c (diff) | |
interactions can have an argument
| -rw-r--r-- | content/encyclopediae/default_encyclopedia.json | 8 | ||||
| -rw-r--r-- | content/maps/room.json | 3 | ||||
| -rw-r--r-- | src/components/interactable.rs | 25 | ||||
| -rw-r--r-- | src/controls.rs | 27 | ||||
| -rw-r--r-- | src/systems/interact.rs | 15 | ||||
| -rw-r--r-- | todo.md | 1 |
6 files changed, 52 insertions, 27 deletions
diff --git a/content/encyclopediae/default_encyclopedia.json b/content/encyclopediae/default_encyclopedia.json index 5fa6ebf..a7196d8 100644 --- a/content/encyclopediae/default_encyclopedia.json +++ b/content/encyclopediae/default_encyclopedia.json @@ -270,6 +270,14 @@ "components": [ ["Interactable", {"action": ["interaction", ["say", "Good morning there, World"]]}] ] + }, + "dude": { + "sprite": "human", + "height": 1.5, + "flags": ["Occupied"], + "components": [ + ["Interactable", {"action": ["interaction", ["reply", "did you say '{}'?"]]}] + ] } } } diff --git a/content/maps/room.json b/content/maps/room.json index e1bf55e..ca50eff 100644 --- a/content/maps/room.json +++ b/content/maps/room.json @@ -17,7 +17,7 @@ "X,^,,,.,,,,,,,,,,,,bbb,,,,,,,,,,#++++#,,,X", "X,,,,,.............bbb..........D++++#,,,X", "X,**,,.,,,,,,,,,,,,bbb,,,,,,,,,,#++++#,,,X", - "X,*,*,.,,,,,V,,V,,,~~~,,,T,,,T,,#++++#,,,X", + "X,*,*,.,u,,,V,,V,,,~~~,,,T,,,T,,#++++#,,,X", "X,,*,,.,,,,,,,,,,,,~~~,,,,,,,,,,######,,,X", "X,oo,,.,s,d,,,,,,,~~~~,,,,,,,,,,f,,,,f,,,X", "X,,*,,.,,,,,,,,,,,~~~''''''''''''''''f'''X", @@ -49,6 +49,7 @@ "/": ["grass", "sword"], "D": ["ground", "closeddoor"], "s": ["ground", "sign"], + "u": ["ground", "dude"], " ": [] } } 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<Self> { 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<String>) -> 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<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), 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(); } @@ -3,7 +3,6 @@ - make readme - more tests -- parameterised interactions - trader - visitors - improved tutorial |
