Simplify sound components and refactor to single-source.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nolan Darilek 2022-08-06 11:05:04 -05:00
parent 5cdcb7a4d4
commit 23d616eead
3 changed files with 29 additions and 193 deletions

View File

@ -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<Buffer>,
pub step_length: f32,
pub gain: f64,
pub reference_distance: Option<f64>,
pub max_distance: Option<f64>,
pub rolloff: Option<f64>,
pub pitch: Option<f64>,
pub pitch_variation: Option<f64>,
}
@ -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<Footstep>>) {
for (entity, footstep) in footsteps.iter() {
let buffer = footstep.sound.clone();
@ -55,79 +42,17 @@ fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added<Foo
}
fn update(
mut commands: Commands,
mut last_step_distance: Local<HashMap<Entity, (f32, Transform)>>,
mut footsteps: Query<
(
Entity,
&Footstep,
&Parent,
&mut Sound,
Option<&DistanceRef>,
Option<&DistanceMax>,
Option<&Rolloff>,
),
Changed<GlobalTransform>,
>,
transforms_storage: Query<&Transform>,
mut last_step_distance: Local<HashMap<Entity, (f32, GlobalTransform)>>,
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();
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(coordinates));
let distance = last.0 + (last.1.distance(transform));
if distance >= footstep.step_length {
last_step_distance.insert(entity, (0., *coordinates));
last_step_distance.insert(entity, (0., *transform));
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));
})
}
} else if reference_distance.is_some() {
commands.run_if_exists(entity, |mut entity| {
entity.remove::<DistanceRef>();
});
}
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::<DistanceMax>();
});
}
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::<Rolloff>();
});
}
sound.pitch = footstep.pitch.unwrap_or(1.);
if let Some(pitch_variation) = footstep.pitch_variation {
let mut pitch = sound.pitch - pitch_variation / 2.;
@ -136,11 +61,12 @@ fn update(
}
sound.paused = false;
sound.restart = true;
} else if last.1 != *coordinates {
last_step_distance.insert(entity, (distance, *coordinates));
} else if last.1 != *transform {
last_step_distance.insert(entity, (distance, *transform));
}
} else {
last_step_distance.insert(entity, (0., *coordinates));
last_step_distance.insert(entity, (0., *transform));
}
}
}
}
@ -150,7 +76,7 @@ pub struct FootstepPlugin;
impl Plugin for FootstepPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Footstep>()
.add_system(added)
.add_system_to_stage(CoreStage::PreUpdate, added)
.add_system_to_stage(
CoreStage::PostUpdate,
update.after(TransformSystem::TransformPropagate),

View File

@ -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<Buffer>,
pub gain: f64,
pub pitch: f64,
pub reference_distance: Option<f64>,
pub max_distance: Option<f64>,
pub rolloff: Option<f64>,
pub interval: Option<Timer>,
}
@ -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<SoundIcon>>) {
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<SoundI
paused: true,
..default()
});
if let Some(v) = reference_distance {
entity.insert(DistanceRef(v));
}
if let Some(v) = max_distance {
entity.insert(DistanceMax(v));
}
if let Some(v) = rolloff {
entity.insert(Rolloff(v));
}
});
}
}
fn update<S>(
mut commands: Commands,
config: Res<SoundIconConfig<S>>,
state: Res<State<S>>,
time: Res<Time>,
@ -93,9 +67,6 @@ fn update<S>(
Option<&Visible>,
Option<&Parent>,
&mut Sound,
Option<&DistanceRef>,
Option<&DistanceMax>,
Option<&Rolloff>,
)>,
) where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
@ -104,17 +75,7 @@ fn update<S>(
return;
}
for visible in viewers.iter() {
for (
icon_entity,
mut icon,
visibility,
parent,
mut sound,
distance_ref,
distance_max,
rolloff,
) in icons.iter_mut()
{
for (icon_entity, mut icon, visibility, parent, mut sound) in icons.iter_mut() {
let entity = if visibility.is_some() {
Some(icon_entity)
} else if parent.is_some() {
@ -141,54 +102,6 @@ fn update<S>(
sound.gain = icon.gain;
sound.pitch = icon.pitch;
sound.looping = icon.interval.is_none();
if let Some(v) = icon.reference_distance {
let insert = if let Some(v2) = distance_ref {
v != **v2
} else {
true
};
if insert {
commands.run_if_exists(icon_entity, move |mut entity| {
entity.insert(DistanceRef(v));
})
}
} else if distance_ref.is_some() {
commands.run_if_exists(icon_entity, |mut entity| {
entity.remove::<DistanceRef>();
});
}
if let Some(v) = icon.max_distance {
let insert = if let Some(v2) = distance_max {
v != **v2
} else {
true
};
if insert {
commands.run_if_exists(icon_entity, move |mut entity| {
entity.insert(DistanceMax(v));
})
}
} else if distance_max.is_some() {
commands.run_if_exists(icon_entity, |mut entity| {
entity.remove::<DistanceMax>();
});
}
if let Some(v) = icon.rolloff {
let insert = if let Some(v2) = rolloff {
v != **v2
} else {
true
};
if insert {
commands.run_if_exists(icon_entity, move |mut entity| {
entity.insert(Rolloff(v));
})
}
} else if rolloff.is_some() {
commands.run_if_exists(icon_entity, |mut entity| {
entity.remove::<Rolloff>();
});
}
} else if !sound.paused {
sound.paused = true;
sound.restart = true;
@ -288,10 +201,7 @@ where
{
fn build(&self, app: &mut App) {
app.add_system(added)
.add_system_to_stage(
CoreStage::PostUpdate,
update::<S>.after(TransformSystem::TransformPropagate),
)
.add_system_to_stage(CoreStage::PostUpdate, update::<S>)
.add_system_to_stage(
CoreStage::PostUpdate,
exploration_focus_changed.after(update::<S>),

View File

@ -7,8 +7,8 @@ use crate::core::CoreConfig;
pub mod footstep;
pub mod icon;
pub use footstep::{Footstep, FootstepBundle};
pub use icon::{SoundIcon, SoundIconBundle};
pub use footstep::Footstep;
pub use icon::SoundIcon;
/*fn scale_sounds(config: Res<CoreConfig>, mut sounds: Query<&mut Sound>) {
let pixels_per_unit = config.pixels_per_unit as f32;