diff options
| author | troido <troido@protonmail.com> | 2020-09-21 02:33:19 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-09-21 02:33:19 +0200 |
| commit | 5ce9b012a7987f4085057f4f0b0af35e76810a7a (patch) | |
| tree | a99418b5aec06d6be28e22150bce85d3c40b8ee7 /src/systems | |
| parent | b6a4c7b2d383755402e5e2c6f60d9a75a899b809 (diff) | |
| parent | 455867294cc849bff2c0829a7464e71e79a0dcae (diff) | |
Merge branch 'interact' into master
Diffstat (limited to 'src/systems')
| -rw-r--r-- | src/systems/interact.rs | 67 | ||||
| -rw-r--r-- | src/systems/mod.rs | 2 |
2 files changed, 32 insertions, 37 deletions
diff --git a/src/systems/interact.rs b/src/systems/interact.rs index b377a7a..1eb4589 100644 --- a/src/systems/interact.rs +++ b/src/systems/interact.rs @@ -28,7 +28,7 @@ use crate::{ Minable }, controls::{Control}, - resources::{Ground, NewEntities, Emigration}, + resources::{Ground, Emigration, NewEntities}, hashmap, playerstate::RoomPos, PlayerId, @@ -45,31 +45,26 @@ impl <'a> System<'a> for Interact { WriteStorage<'a, ControlCooldown>, ReadStorage<'a, Interactable>, WriteStorage<'a, TriggerBox>, - Write<'a, NewEntities>, WriteStorage<'a, Ear>, WriteStorage<'a, Inventory>, ReadStorage<'a, Visible>, ReadStorage<'a, Player>, Write<'a, Emigration>, WriteStorage<'a, Whitelist>, - WriteStorage<'a, Minable> + WriteStorage<'a, Minable>, + Read<'a, NewEntities> ); - fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut triggerbox, new, mut ears, mut inventories, visibles, players, mut emigration, mut whitelists, mut minables): Self::SystemData) { + fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut triggerbox, mut ears, mut inventories, visibles, players, mut emigration, mut whitelists, mut minables, new): Self::SystemData) { for (actor, controller, position) in (&entities, &controllers, &positions).join(){ let mut target = None; let ear = ears.get_mut(actor); match &controller.control { 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) { - if interactable.accepts_arg(arg){ - target = Some((*ent, interactable, arg.clone())); - break 'targets; - } - } + for (ent, interactable) in ground.components_near(position.pos, directions, &interactables) { + if interactable.accepts_arg(arg){ + target = Some((ent, interactable, arg.clone())); + break; } } } @@ -79,35 +74,14 @@ impl <'a> System<'a> for Interact { let mut cooldown = 2; let name = visibles.get(ent).map(|v| v.name.as_str()); match interactable { - Interactable::Trigger(trigger) => { - TriggerBox::add_message(&mut triggerbox, ent, *trigger); - } Interactable::Say(text) => { say(ear, text.clone(), name); } Interactable::Reply(text) => { say(ear, text.replace("{}", &arg.unwrap()), name); } - Interactable::Exchange(prefix, exchanges) => { - if let Some(txt) = arg { - if let (Some(inventory), Some(action)) = (inventories.get_mut(actor), strip_prefix(&txt, prefix)) { - if let Some(exchange) = exchanges.get(action) { - if exchange.can_trade(inventory){ - exchange.trade(inventory, &new.encyclopedia); - say(ear, format!("Success! '{}' ({})", txt, exchange.show()), name); - } else { - say(ear, format!("You do not have the required items or inventory space for '{}' ({})", txt, exchange.show()), name); - } - } else { - say(ear, format!("Invalid option: {}", action), name); - } - } - } else if let Some(ear) = ear { - ear.sounds.push(Notification::Options{ - description: "".to_string(), - options: exchanges.iter().map(|(id, exchange)| (format!("{}{}", prefix, id), exchange.show())).collect() - }) - } + Interactable::Trigger(trigger) => { + TriggerBox::add_message(&mut triggerbox, ent, *trigger); } Interactable::Visit(dest) => { if let Some(argument) = arg { @@ -163,6 +137,27 @@ impl <'a> System<'a> for Interact { } } } + Interactable::Exchange(prefix, exchanges) => { + if let Some(txt) = arg { + if let (Some(inventory), Some(action)) = (inventories.get_mut(actor), strip_prefix(&txt, prefix)) { + if let Some(exchange) = exchanges.get(action) { + if exchange.can_trade(inventory){ + exchange.trade(inventory, &new.encyclopedia); + say(ear, format!("Success! '{}' ({})", txt, exchange.show()), name); + } else { + say(ear, format!("You do not have the required items or inventory space for '{}' ({})", txt, exchange.show()), name); + } + } else { + say(ear, format!("Invalid option: {}", action), name); + } + } + } else if let Some(ear) = ear { + ear.sounds.push(Notification::Options{ + description: "".to_string(), + options: exchanges.iter().map(|(id, exchange)| (format!("{}{}", prefix, id), exchange.show())).collect() + }) + } + } } cooldowns.insert(actor, ControlCooldown{amount: cooldown}).unwrap(); } diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 7323dee..7f93bb9 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -50,5 +50,5 @@ pub use self::{ building::Building, deduplicate::Deduplicate, spawntrigger::SpawnTrigger, - replace::Replace + replace::Replace, }; |
