From 9c26f587249291c9aeff77d2e19f78ff49b13745 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Mon, 24 May 2021 14:02:22 -0500 Subject: [PATCH] Flatten footsteps. --- src/core.rs | 7 ++++- src/sound.rs | 78 ++++++++++++++++++++++++---------------------------- 2 files changed, 42 insertions(+), 43 deletions(-) diff --git a/src/core.rs b/src/core.rs index 053ff90..cbfacbd 100644 --- a/src/core.rs +++ b/src/core.rs @@ -278,6 +278,11 @@ pub trait PointLike { fn y_usize(&self) -> usize { self.y() as usize } + + fn f32(&self) -> (f32, f32) { + (self.x(), self.y()) + } + fn i32(&self) -> (i32, i32) { (self.x_i32(), self.y_i32()) } @@ -411,7 +416,7 @@ impl_pointlike_for_tuple_component!(Coordinates); impl From<&dyn PointLike> for (i32, i32) { fn from(val: &dyn PointLike) -> Self { - (val.x_i32(), val.y_i32()) + val.i32() } } diff --git a/src/sound.rs b/src/sound.rs index ab7064f..508f1d7 100644 --- a/src/sound.rs +++ b/src/sound.rs @@ -87,55 +87,48 @@ pub struct SoundIconBundle { pub global_transform: GlobalTransform, } -fn footstep( +fn add_footstep_sounds( mut commands: Commands, + footsteps: Query<(Entity, &Footstep), Added>, assets: Res>, - mut last_step_distance: Local>, - footsteps: Query<(Entity, &Footstep, &Parent, Option<&Children>), Changed>, - coordinates_storage: Query<&Coordinates>, - mut sounds: Query<&mut Sound>, ) { - for (entity, footstep, parent, children) in footsteps.iter() { + for (entity, footstep) in footsteps.iter() { + let buffer = assets.get_handle(footstep.sound); + commands.entity(entity).insert(Sound { + buffer, + state: SoundState::Stopped, + gain: footstep.gain, + ..Default::default() + }); + } +} + +fn footstep( + mut last_step_distance: Local>, + mut footsteps: Query<(Entity, &Footstep, &Parent, &mut Sound), Changed>, + coordinates_storage: Query<&Coordinates>, +) { + for (entity, footstep, parent, mut sound) in footsteps.iter_mut() { let coordinates = coordinates_storage.get(**parent).unwrap(); - if let Some(children) = children { - 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)); - let sound = children[0]; - if let Ok(mut sound) = sounds.get_mut(sound) { - sound.gain = footstep.gain; - sound.reference_distance = footstep.reference_distance; - sound.max_distance = footstep.max_distance; - sound.rolloff_factor = footstep.rolloff_factor; - if let Some(pitch_variation) = footstep.pitch_variation { - let mut pitch = 1. - pitch_variation / 2.; - pitch += random::() * pitch_variation; - sound.pitch = pitch; - } - sound.play(); - } - } else if last.1 != *coordinates { - last_step_distance.insert(entity, (distance, *coordinates)); - } - } else { + 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; + sound.reference_distance = footstep.reference_distance; + sound.max_distance = footstep.max_distance; + sound.rolloff_factor = footstep.rolloff_factor; + if let Some(pitch_variation) = footstep.pitch_variation { + let mut pitch = 1. - pitch_variation / 2.; + pitch += random::() * pitch_variation; + sound.pitch = pitch; + } + sound.play(); + } else if last.1 != *coordinates { + last_step_distance.insert(entity, (distance, *coordinates)); } } else { - let buffer = assets.get_handle(footstep.sound); - let sound = Sound { - buffer, - state: SoundState::Stopped, - gain: footstep.gain, - ..Default::default() - }; - let child = commands - .spawn() - .insert(sound) - .insert(Transform::default()) - .insert(GlobalTransform::default()) - .id(); - commands.entity(entity).push_children(&[child]); + last_step_distance.insert(entity, (0., *coordinates)); } } } @@ -305,6 +298,7 @@ where .unwrap(); } app.register_type::() + .add_system(add_footstep_sounds.system()) .add_system_to_stage( CoreStage::PostUpdate, footstep.system().after(TransformSystem::TransformPropagate),