From a1edab98f9db5d2034838080a448a3fbb44a1bd2 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 7 Jun 2022 09:13:03 -0500 Subject: [PATCH] Set elapsed time of sound icons to their duration on visibility gain so they begin playing immediately. --- src/sound/icon.rs | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/sound/icon.rs b/src/sound/icon.rs index 1a6cdbd..e645c2e 100644 --- a/src/sound/icon.rs +++ b/src/sound/icon.rs @@ -9,7 +9,7 @@ use crate::{ commands::RunIfExistsExt, core::Player, exploration::ExplorationFocused, - visibility::{Visible, VisibleEntities}, + visibility::{VisibilityChanged, Visible, VisibleEntities}, }; #[derive(Component, Clone, Debug)] @@ -242,6 +242,38 @@ fn exploration_focus_removed( } } +fn reset_timer_on_visibility_gain( + mut events: EventReader, + player: Query>, + mut icons: Query<&mut SoundIcon>, + children: Query<&Children>, +) { + for event in events.iter() { + if let VisibilityChanged::Gained { viewer, viewed } = event { + if player.get(*viewer).is_ok() { + let mut targets = vec![]; + if icons.get(*viewed).is_ok() { + targets.push(viewed); + } + if let Ok(children) = children.get(*viewed) { + for child in children.iter() { + if icons.get(*child).is_ok() { + targets.push(&*child); + } + } + } + for icon in targets.iter_mut() { + if let Ok(mut icon) = icons.get_mut(**icon) { + if let Some(timer) = icon.interval.as_mut() { + timer.set_elapsed(timer.duration()); + } + } + } + } + } + } +} + #[derive(Clone, Debug, Default)] pub struct SoundIconConfig { pub states: Vec, @@ -273,6 +305,7 @@ where .add_system_to_stage( CoreStage::PostUpdate, exploration_focus_removed.after(exploration_focus_changed), - ); + ) + .add_system_to_stage(CoreStage::PostUpdate, reset_timer_on_visibility_gain); } }