diff options
Diffstat (limited to 'src/systems')
| -rw-r--r-- | src/systems/exchange.rs | 82 | ||||
| -rw-r--r-- | src/systems/interact.rs | 39 | ||||
| -rw-r--r-- | src/systems/mod.rs | 4 | ||||
| -rw-r--r-- | src/systems/talk.rs | 14 |
4 files changed, 95 insertions, 44 deletions
diff --git a/src/systems/exchange.rs b/src/systems/exchange.rs new file mode 100644 index 0000000..9931bdd --- /dev/null +++ b/src/systems/exchange.rs @@ -0,0 +1,82 @@ + +use specs::{ + Entities, + ReadStorage, + WriteStorage, + System, + Join, + Read, + Write +}; + +use crate::{ + components::{ + Controller, + Position, + Exchanger, + Notification, + Ear, + Inventory, + Visible + }, + controls::{Control}, + resources::{Ground, NewEntities}, + util::strip_prefix +}; + +pub struct Exchange; +impl <'a> System<'a> for Exchange { + type SystemData = ( + Entities<'a>, + ReadStorage<'a, Controller>, + ReadStorage<'a, Position>, + Read<'a, Ground>, + ReadStorage<'a, Exchanger>, + Write<'a, NewEntities>, + WriteStorage<'a, Ear>, + WriteStorage<'a, Inventory>, + ReadStorage<'a, Visible> + ); + + fn run(&mut self, (entities, controllers, positions, ground, exchangers, new, mut ears, mut inventories, visibles): Self::SystemData) { + for (actor, controller, position) in (&entities, &controllers, &positions).join(){ + let ear = ears.get_mut(actor); + match &controller.control { + Control::Interact(directions, arg) => { + for (ent, exchanger) in ground.components_near(position.pos, directions, &exchangers) { + let prefix = exchanger.prefix.as_str(); + let name = visibles.get(ent).map(|v| v.name.as_str()); + if let Some(txt) = arg { + if let (Some(inventory), Some(action)) = (inventories.get_mut(actor), strip_prefix(&txt, prefix)) { + if let Some(exchange) = exchanger.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); + } + break; + } + } else if let Some(ear) = ear { + ear.sounds.push(Notification::Options{ + description: "".to_string(), + options: exchanger.exchanges.iter().map(|(id, exchange)| (format!("{}{}", prefix, id), exchange.show())).collect() + }); + break; + } + } + } + _ => {} + } + } + } +} + +fn say(maybe_ear: Option<&mut Ear>, text: String, source: Option<&str>){ + if let Some(ear) = maybe_ear { + ear.sounds.push(Notification::Sound{text, source: source.map(|s| s.to_string())}); + } +} diff --git a/src/systems/interact.rs b/src/systems/interact.rs index 2090f6f..bce2afc 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}, hashmap, playerstate::RoomPos, PlayerId, @@ -45,7 +45,6 @@ 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>, @@ -55,21 +54,16 @@ impl <'a> System<'a> for Interact { WriteStorage<'a, Minable> ); - 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, inventories, visibles, players, mut emigration, mut whitelists, mut minables): 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; } } } @@ -82,27 +76,6 @@ impl <'a> System<'a> for Interact { Interactable::Trigger(trigger) => { TriggerBox::add_message(&mut triggerbox, ent, *trigger); } - 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::Visit(dest) => { if let Some(argument) = arg { if let (Some(player), Some(whitelist)) = (players.get(actor), whitelists.get_mut(ent)){ diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 56606e7..76e39ce 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -25,6 +25,7 @@ mod deduplicate; mod spawntrigger; mod replace; mod talk; +mod exchange; pub use self::{ controlinput::ControlInput, @@ -52,5 +53,6 @@ pub use self::{ deduplicate::Deduplicate, spawntrigger::SpawnTrigger, replace::Replace, - talk::Talk + talk::Talk, + exchange::Exchange }; diff --git a/src/systems/talk.rs b/src/systems/talk.rs index 4bb898a..50e491f 100644 --- a/src/systems/talk.rs +++ b/src/systems/talk.rs @@ -1,5 +1,4 @@ -use std::collections::HashSet; use specs::{ ReadStorage, @@ -37,15 +36,10 @@ impl <'a> System<'a> for Talk { for (controller, position, ear) in (&controllers, &positions, &mut ears).join(){ match &controller.control { Control::Interact(directions, None) => { - '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(Talkable{text}) = talkables.get(*ent) { - let name = visibles.get(*ent).map(|v| v.name.clone()); - ear.sounds.push(Notification::Sound{text: text.clone(), source: name}); - break 'targets; - } - } + for (ent, Talkable{text}) in ground.components_near(position.pos, directions, &talkables) { + let name = visibles.get(ent).map(|v| v.name.clone()); + ear.sounds.push(Notification::Sound{text: text.clone(), source: name}); + break; } } _ => {} |
