use std::collections::HashSet; use specs::{ Entities, ReadStorage, WriteStorage, System, Join, Read, Write }; use crate::components::{ Controller, Position, ControlCooldown, Interactable, Dead, Removed, Sound, Ear }; use crate::controls::{Control}; use crate::resources::{Ground, NewEntities}; pub struct Interact; impl <'a> System<'a> for Interact { type SystemData = ( Entities<'a>, ReadStorage<'a, Controller>, ReadStorage<'a, Position>, Read<'a, Ground>, WriteStorage<'a, ControlCooldown>, ReadStorage<'a, Interactable>, WriteStorage<'a, Dead>, WriteStorage<'a, Removed>, Write<'a, NewEntities>, WriteStorage<'a, Ear> ); fn run(&mut self, (entities, controllers, positions, ground, mut cooldowns, interactables, mut deads, mut removeds, mut new, mut ears): Self::SystemData) { for (entity, controller, position) in (&entities, &controllers, &positions).join(){ let mut target = None; 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, pos, arg.clone())); break 'targets; } } } } } _ => {} } if let Some((ent, interactable, pos, arg)) = target { match interactable { Interactable::Harvest => { deads.insert(ent, Dead).unwrap(); } Interactable::Change(into) => { new.create(pos, into).unwrap(); removeds.insert(ent, Removed).unwrap(); } Interactable::Say(text) => { if let Some(ear) = ears.get_mut(entity) { 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(); } } } }