Flatten footsteps.

This commit is contained in:
Nolan Darilek 2021-05-24 14:02:22 -05:00
parent 41bfe08a03
commit 9c26f58724
2 changed files with 42 additions and 43 deletions

View File

@ -278,6 +278,11 @@ pub trait PointLike {
fn y_usize(&self) -> usize { fn y_usize(&self) -> usize {
self.y() as usize self.y() as usize
} }
fn f32(&self) -> (f32, f32) {
(self.x(), self.y())
}
fn i32(&self) -> (i32, i32) { fn i32(&self) -> (i32, i32) {
(self.x_i32(), self.y_i32()) (self.x_i32(), self.y_i32())
} }
@ -411,7 +416,7 @@ impl_pointlike_for_tuple_component!(Coordinates);
impl From<&dyn PointLike> for (i32, i32) { impl From<&dyn PointLike> for (i32, i32) {
fn from(val: &dyn PointLike) -> Self { fn from(val: &dyn PointLike) -> Self {
(val.x_i32(), val.y_i32()) val.i32()
} }
} }

View File

@ -87,23 +87,33 @@ pub struct SoundIconBundle {
pub global_transform: GlobalTransform, pub global_transform: GlobalTransform,
} }
fn footstep( fn add_footstep_sounds(
mut commands: Commands, mut commands: Commands,
footsteps: Query<(Entity, &Footstep), Added<Footstep>>,
assets: Res<Assets<Buffer>>, assets: Res<Assets<Buffer>>,
mut last_step_distance: Local<HashMap<Entity, (f32, Coordinates)>>,
footsteps: Query<(Entity, &Footstep, &Parent, Option<&Children>), Changed<GlobalTransform>>,
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<HashMap<Entity, (f32, Coordinates)>>,
mut footsteps: Query<(Entity, &Footstep, &Parent, &mut Sound), Changed<GlobalTransform>>,
coordinates_storage: Query<&Coordinates>,
) {
for (entity, footstep, parent, mut sound) in footsteps.iter_mut() {
let coordinates = coordinates_storage.get(**parent).unwrap(); let coordinates = coordinates_storage.get(**parent).unwrap();
if let Some(children) = children {
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(coordinates));
if distance >= footstep.step_length { if distance >= footstep.step_length {
last_step_distance.insert(entity, (0., *coordinates)); 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.gain = footstep.gain;
sound.reference_distance = footstep.reference_distance; sound.reference_distance = footstep.reference_distance;
sound.max_distance = footstep.max_distance; sound.max_distance = footstep.max_distance;
@ -114,29 +124,12 @@ fn footstep(
sound.pitch = pitch; sound.pitch = pitch;
} }
sound.play(); sound.play();
}
} else if last.1 != *coordinates { } else if last.1 != *coordinates {
last_step_distance.insert(entity, (distance, *coordinates)); last_step_distance.insert(entity, (distance, *coordinates));
} }
} else { } else {
last_step_distance.insert(entity, (0., *coordinates)); last_step_distance.insert(entity, (0., *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]);
}
} }
} }
@ -305,6 +298,7 @@ where
.unwrap(); .unwrap();
} }
app.register_type::<Footstep>() app.register_type::<Footstep>()
.add_system(add_footstep_sounds.system())
.add_system_to_stage( .add_system_to_stage(
CoreStage::PostUpdate, CoreStage::PostUpdate,
footstep.system().after(TransformSystem::TransformPropagate), footstep.system().after(TransformSystem::TransformPropagate),