Simplify sound components and refactor to single-source.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
5cdcb7a4d4
commit
23d616eead
|
@ -1,7 +1,7 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use bevy::{prelude::*, transform::TransformSystem};
|
use bevy::{prelude::*, transform::TransformSystem};
|
||||||
use bevy_synthizer::{Buffer, DistanceMax, DistanceRef, Rolloff, Sound};
|
use bevy_synthizer::{Buffer, Sound};
|
||||||
use rand::random;
|
use rand::random;
|
||||||
|
|
||||||
use crate::{commands::RunIfExistsExt, core::PointLike};
|
use crate::{commands::RunIfExistsExt, core::PointLike};
|
||||||
|
@ -12,9 +12,6 @@ pub struct Footstep {
|
||||||
pub sound: Handle<Buffer>,
|
pub sound: Handle<Buffer>,
|
||||||
pub step_length: f32,
|
pub step_length: f32,
|
||||||
pub gain: f64,
|
pub gain: f64,
|
||||||
pub reference_distance: Option<f64>,
|
|
||||||
pub max_distance: Option<f64>,
|
|
||||||
pub rolloff: Option<f64>,
|
|
||||||
pub pitch: Option<f64>,
|
pub pitch: Option<f64>,
|
||||||
pub pitch_variation: Option<f64>,
|
pub pitch_variation: Option<f64>,
|
||||||
}
|
}
|
||||||
|
@ -25,22 +22,12 @@ impl Default for Footstep {
|
||||||
sound: default(),
|
sound: default(),
|
||||||
step_length: 0.8,
|
step_length: 0.8,
|
||||||
gain: 1.,
|
gain: 1.,
|
||||||
reference_distance: None,
|
|
||||||
max_distance: None,
|
|
||||||
rolloff: None,
|
|
||||||
pitch: None,
|
pitch: None,
|
||||||
pitch_variation: Some(0.15),
|
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>>) {
|
fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added<Footstep>>) {
|
||||||
for (entity, footstep) in footsteps.iter() {
|
for (entity, footstep) in footsteps.iter() {
|
||||||
let buffer = footstep.sound.clone();
|
let buffer = footstep.sound.clone();
|
||||||
|
@ -55,79 +42,17 @@ fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added<Foo
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(
|
fn update(
|
||||||
mut commands: Commands,
|
mut last_step_distance: Local<HashMap<Entity, (f32, GlobalTransform)>>,
|
||||||
mut last_step_distance: Local<HashMap<Entity, (f32, Transform)>>,
|
mut footsteps: Query<(Entity, &Footstep, &Parent, &mut Sound)>,
|
||||||
mut footsteps: Query<
|
transforms_storage: Query<&GlobalTransform>,
|
||||||
(
|
|
||||||
Entity,
|
|
||||||
&Footstep,
|
|
||||||
&Parent,
|
|
||||||
&mut Sound,
|
|
||||||
Option<&DistanceRef>,
|
|
||||||
Option<&DistanceMax>,
|
|
||||||
Option<&Rolloff>,
|
|
||||||
),
|
|
||||||
Changed<GlobalTransform>,
|
|
||||||
>,
|
|
||||||
transforms_storage: Query<&Transform>,
|
|
||||||
) {
|
) {
|
||||||
for (entity, footstep, parent, mut sound, reference_distance, max_distance, rolloff) in
|
for (entity, footstep, parent, mut sound) in footsteps.iter_mut() {
|
||||||
footsteps.iter_mut()
|
if let Ok(transform) = transforms_storage.get(**parent) {
|
||||||
{
|
|
||||||
let coordinates = transforms_storage.get(**parent).unwrap();
|
|
||||||
if let Some(last) = last_step_distance.get(&entity) {
|
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 {
|
if distance >= footstep.step_length {
|
||||||
last_step_distance.insert(entity, (0., *coordinates));
|
last_step_distance.insert(entity, (0., *transform));
|
||||||
sound.gain = footstep.gain;
|
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.);
|
sound.pitch = footstep.pitch.unwrap_or(1.);
|
||||||
if let Some(pitch_variation) = footstep.pitch_variation {
|
if let Some(pitch_variation) = footstep.pitch_variation {
|
||||||
let mut pitch = sound.pitch - pitch_variation / 2.;
|
let mut pitch = sound.pitch - pitch_variation / 2.;
|
||||||
|
@ -136,11 +61,12 @@ fn update(
|
||||||
}
|
}
|
||||||
sound.paused = false;
|
sound.paused = false;
|
||||||
sound.restart = true;
|
sound.restart = true;
|
||||||
} else if last.1 != *coordinates {
|
} else if last.1 != *transform {
|
||||||
last_step_distance.insert(entity, (distance, *coordinates));
|
last_step_distance.insert(entity, (distance, *transform));
|
||||||
}
|
}
|
||||||
} else {
|
} 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 {
|
impl Plugin for FootstepPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.register_type::<Footstep>()
|
app.register_type::<Footstep>()
|
||||||
.add_system(added)
|
.add_system_to_stage(CoreStage::PreUpdate, added)
|
||||||
.add_system_to_stage(
|
.add_system_to_stage(
|
||||||
CoreStage::PostUpdate,
|
CoreStage::PostUpdate,
|
||||||
update.after(TransformSystem::TransformPropagate),
|
update.after(TransformSystem::TransformPropagate),
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::{fmt::Debug, hash::Hash, time::Duration};
|
use std::{fmt::Debug, hash::Hash, time::Duration};
|
||||||
|
|
||||||
use bevy::{prelude::*, transform::TransformSystem};
|
use bevy::prelude::*;
|
||||||
use bevy_synthizer::{Buffer, DistanceMax, DistanceRef, Rolloff, Sound};
|
use bevy_synthizer::{Buffer, Sound};
|
||||||
|
|
||||||
use rand::random;
|
use rand::random;
|
||||||
|
|
||||||
|
@ -17,9 +17,6 @@ pub struct SoundIcon {
|
||||||
pub sound: Handle<Buffer>,
|
pub sound: Handle<Buffer>,
|
||||||
pub gain: f64,
|
pub gain: f64,
|
||||||
pub pitch: f64,
|
pub pitch: f64,
|
||||||
pub reference_distance: Option<f64>,
|
|
||||||
pub max_distance: Option<f64>,
|
|
||||||
pub rolloff: Option<f64>,
|
|
||||||
pub interval: Option<Timer>,
|
pub interval: Option<Timer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,9 +27,6 @@ impl Default for SoundIcon {
|
||||||
sound: default(),
|
sound: default(),
|
||||||
gain: 1.,
|
gain: 1.,
|
||||||
pitch: 1.,
|
pitch: 1.,
|
||||||
reference_distance: None,
|
|
||||||
max_distance: None,
|
|
||||||
rolloff: None,
|
|
||||||
interval: Some(Timer::from_seconds(seconds, true)),
|
interval: Some(Timer::from_seconds(seconds, true)),
|
||||||
};
|
};
|
||||||
if let Some(ref mut interval) = icon.interval {
|
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>>) {
|
fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added<SoundIcon>>) {
|
||||||
for (entity, icon) in icons.iter() {
|
for (entity, icon) in icons.iter() {
|
||||||
let buffer = icon.sound.clone();
|
let buffer = icon.sound.clone();
|
||||||
let gain = icon.gain;
|
let gain = icon.gain;
|
||||||
let pitch = icon.pitch;
|
let pitch = icon.pitch;
|
||||||
let looping = icon.interval.is_none();
|
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| {
|
commands.run_if_exists(entity, move |mut entity| {
|
||||||
entity.insert(Sound {
|
entity.insert(Sound {
|
||||||
buffer,
|
buffer,
|
||||||
|
@ -68,21 +52,11 @@ fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added<SoundI
|
||||||
paused: true,
|
paused: true,
|
||||||
..default()
|
..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>(
|
fn update<S>(
|
||||||
mut commands: Commands,
|
|
||||||
config: Res<SoundIconConfig<S>>,
|
config: Res<SoundIconConfig<S>>,
|
||||||
state: Res<State<S>>,
|
state: Res<State<S>>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
|
@ -93,9 +67,6 @@ fn update<S>(
|
||||||
Option<&Visible>,
|
Option<&Visible>,
|
||||||
Option<&Parent>,
|
Option<&Parent>,
|
||||||
&mut Sound,
|
&mut Sound,
|
||||||
Option<&DistanceRef>,
|
|
||||||
Option<&DistanceMax>,
|
|
||||||
Option<&Rolloff>,
|
|
||||||
)>,
|
)>,
|
||||||
) where
|
) where
|
||||||
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
||||||
|
@ -104,17 +75,7 @@ fn update<S>(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for visible in viewers.iter() {
|
for visible in viewers.iter() {
|
||||||
for (
|
for (icon_entity, mut icon, visibility, parent, mut sound) in icons.iter_mut() {
|
||||||
icon_entity,
|
|
||||||
mut icon,
|
|
||||||
visibility,
|
|
||||||
parent,
|
|
||||||
mut sound,
|
|
||||||
distance_ref,
|
|
||||||
distance_max,
|
|
||||||
rolloff,
|
|
||||||
) in icons.iter_mut()
|
|
||||||
{
|
|
||||||
let entity = if visibility.is_some() {
|
let entity = if visibility.is_some() {
|
||||||
Some(icon_entity)
|
Some(icon_entity)
|
||||||
} else if parent.is_some() {
|
} else if parent.is_some() {
|
||||||
|
@ -141,54 +102,6 @@ fn update<S>(
|
||||||
sound.gain = icon.gain;
|
sound.gain = icon.gain;
|
||||||
sound.pitch = icon.pitch;
|
sound.pitch = icon.pitch;
|
||||||
sound.looping = icon.interval.is_none();
|
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 {
|
} else if !sound.paused {
|
||||||
sound.paused = true;
|
sound.paused = true;
|
||||||
sound.restart = true;
|
sound.restart = true;
|
||||||
|
@ -288,10 +201,7 @@ where
|
||||||
{
|
{
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_system(added)
|
app.add_system(added)
|
||||||
.add_system_to_stage(
|
.add_system_to_stage(CoreStage::PostUpdate, update::<S>)
|
||||||
CoreStage::PostUpdate,
|
|
||||||
update::<S>.after(TransformSystem::TransformPropagate),
|
|
||||||
)
|
|
||||||
.add_system_to_stage(
|
.add_system_to_stage(
|
||||||
CoreStage::PostUpdate,
|
CoreStage::PostUpdate,
|
||||||
exploration_focus_changed.after(update::<S>),
|
exploration_focus_changed.after(update::<S>),
|
||||||
|
|
|
@ -7,8 +7,8 @@ use crate::core::CoreConfig;
|
||||||
pub mod footstep;
|
pub mod footstep;
|
||||||
pub mod icon;
|
pub mod icon;
|
||||||
|
|
||||||
pub use footstep::{Footstep, FootstepBundle};
|
pub use footstep::Footstep;
|
||||||
pub use icon::{SoundIcon, SoundIconBundle};
|
pub use icon::SoundIcon;
|
||||||
|
|
||||||
/*fn scale_sounds(config: Res<CoreConfig>, mut sounds: Query<&mut Sound>) {
|
/*fn scale_sounds(config: Res<CoreConfig>, mut sounds: Query<&mut Sound>) {
|
||||||
let pixels_per_unit = config.pixels_per_unit as f32;
|
let pixels_per_unit = config.pixels_per_unit as f32;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user