From 23d616eead83b6ea094f7f4290837833ebfb8c51 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Sat, 6 Aug 2022 11:05:04 -0500 Subject: [PATCH] Simplify sound components and refactor to single-source. --- src/sound/footstep.rs | 120 ++++++++---------------------------------- src/sound/icon.rs | 98 ++-------------------------------- src/sound/mod.rs | 4 +- 3 files changed, 29 insertions(+), 193 deletions(-) diff --git a/src/sound/footstep.rs b/src/sound/footstep.rs index 61cb8b5..7400dce 100644 --- a/src/sound/footstep.rs +++ b/src/sound/footstep.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use bevy::{prelude::*, transform::TransformSystem}; -use bevy_synthizer::{Buffer, DistanceMax, DistanceRef, Rolloff, Sound}; +use bevy_synthizer::{Buffer, Sound}; use rand::random; use crate::{commands::RunIfExistsExt, core::PointLike}; @@ -12,9 +12,6 @@ pub struct Footstep { pub sound: Handle, pub step_length: f32, pub gain: f64, - pub reference_distance: Option, - pub max_distance: Option, - pub rolloff: Option, pub pitch: Option, pub pitch_variation: Option, } @@ -25,22 +22,12 @@ impl Default for Footstep { sound: default(), step_length: 0.8, gain: 1., - reference_distance: None, - max_distance: None, - rolloff: None, pitch: None, pitch_variation: Some(0.15), } } } -#[derive(Bundle, Default)] -pub struct FootstepBundle { - pub footstep: Footstep, - pub transform: Transform, - pub global_transform: GlobalTransform, -} - fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added>) { for (entity, footstep) in footsteps.iter() { let buffer = footstep.sound.clone(); @@ -55,92 +42,31 @@ fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added>, - mut footsteps: Query< - ( - Entity, - &Footstep, - &Parent, - &mut Sound, - Option<&DistanceRef>, - Option<&DistanceMax>, - Option<&Rolloff>, - ), - Changed, - >, - transforms_storage: Query<&Transform>, + mut last_step_distance: Local>, + mut footsteps: Query<(Entity, &Footstep, &Parent, &mut Sound)>, + transforms_storage: Query<&GlobalTransform>, ) { - for (entity, footstep, parent, mut sound, reference_distance, max_distance, rolloff) in - footsteps.iter_mut() - { - let coordinates = transforms_storage.get(**parent).unwrap(); - if let Some(last) = last_step_distance.get(&entity) { - let distance = last.0 + (last.1.distance(coordinates)); - if distance >= footstep.step_length { - last_step_distance.insert(entity, (0., *coordinates)); - sound.gain = footstep.gain; - if let Some(v) = footstep.reference_distance { - let insert = if let Some(v2) = reference_distance { - v != **v2 - } else { - true - }; - if insert { - commands.run_if_exists(entity, move |mut entity| { - entity.insert(DistanceRef(v)); - }) + for (entity, footstep, parent, mut sound) in footsteps.iter_mut() { + if let Ok(transform) = transforms_storage.get(**parent) { + if let Some(last) = last_step_distance.get(&entity) { + let distance = last.0 + (last.1.distance(transform)); + if distance >= footstep.step_length { + last_step_distance.insert(entity, (0., *transform)); + sound.gain = footstep.gain; + sound.pitch = footstep.pitch.unwrap_or(1.); + if let Some(pitch_variation) = footstep.pitch_variation { + let mut pitch = sound.pitch - pitch_variation / 2.; + pitch += random::() * pitch_variation; + sound.pitch = pitch; } - } else if reference_distance.is_some() { - commands.run_if_exists(entity, |mut entity| { - entity.remove::(); - }); + sound.paused = false; + sound.restart = true; + } else if last.1 != *transform { + last_step_distance.insert(entity, (distance, *transform)); } - if let Some(v) = footstep.max_distance { - let insert = if let Some(v2) = max_distance { - v != **v2 - } else { - true - }; - if insert { - commands.run_if_exists(entity, move |mut entity| { - entity.insert(DistanceMax(v)); - }) - } - } else if max_distance.is_some() { - commands.run_if_exists(entity, |mut entity| { - entity.remove::(); - }); - } - if let Some(v) = footstep.rolloff { - let insert = if let Some(v2) = rolloff { - v != **v2 - } else { - true - }; - if insert { - commands.run_if_exists(entity, move |mut entity| { - entity.insert(Rolloff(v)); - }) - } - } else if rolloff.is_some() { - commands.run_if_exists(entity, |mut entity| { - entity.remove::(); - }); - } - sound.pitch = footstep.pitch.unwrap_or(1.); - if let Some(pitch_variation) = footstep.pitch_variation { - let mut pitch = sound.pitch - pitch_variation / 2.; - pitch += random::() * pitch_variation; - sound.pitch = pitch; - } - sound.paused = false; - sound.restart = true; - } else if last.1 != *coordinates { - last_step_distance.insert(entity, (distance, *coordinates)); + } else { + last_step_distance.insert(entity, (0., *transform)); } - } else { - last_step_distance.insert(entity, (0., *coordinates)); } } } @@ -150,7 +76,7 @@ pub struct FootstepPlugin; impl Plugin for FootstepPlugin { fn build(&self, app: &mut App) { app.register_type::() - .add_system(added) + .add_system_to_stage(CoreStage::PreUpdate, added) .add_system_to_stage( CoreStage::PostUpdate, update.after(TransformSystem::TransformPropagate), diff --git a/src/sound/icon.rs b/src/sound/icon.rs index f0df503..e68a12d 100644 --- a/src/sound/icon.rs +++ b/src/sound/icon.rs @@ -1,7 +1,7 @@ use std::{fmt::Debug, hash::Hash, time::Duration}; -use bevy::{prelude::*, transform::TransformSystem}; -use bevy_synthizer::{Buffer, DistanceMax, DistanceRef, Rolloff, Sound}; +use bevy::prelude::*; +use bevy_synthizer::{Buffer, Sound}; use rand::random; @@ -17,9 +17,6 @@ pub struct SoundIcon { pub sound: Handle, pub gain: f64, pub pitch: f64, - pub reference_distance: Option, - pub max_distance: Option, - pub rolloff: Option, pub interval: Option, } @@ -30,9 +27,6 @@ impl Default for SoundIcon { sound: default(), gain: 1., pitch: 1., - reference_distance: None, - max_distance: None, - rolloff: None, interval: Some(Timer::from_seconds(seconds, true)), }; if let Some(ref mut interval) = icon.interval { @@ -43,22 +37,12 @@ impl Default for SoundIcon { } } -#[derive(Bundle, Clone, Debug, Default)] -pub struct SoundIconBundle { - pub sound_icon: SoundIcon, - pub transform: Transform, - pub global_transform: GlobalTransform, -} - fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added>) { for (entity, icon) in icons.iter() { let buffer = icon.sound.clone(); let gain = icon.gain; let pitch = icon.pitch; let looping = icon.interval.is_none(); - let reference_distance = icon.reference_distance; - let max_distance = icon.max_distance; - let rolloff = icon.rolloff; commands.run_if_exists(entity, move |mut entity| { entity.insert(Sound { buffer, @@ -68,21 +52,11 @@ fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added( - mut commands: Commands, config: Res>, state: Res>, time: Res