Flatten footsteps.
This commit is contained in:
parent
41bfe08a03
commit
9c26f58724
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
78
src/sound.rs
78
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<Footstep>>,
|
||||
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();
|
||||
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::<f32>() * 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::<f32>() * 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::<Footstep>()
|
||||
.add_system(add_footstep_sounds.system())
|
||||
.add_system_to_stage(
|
||||
CoreStage::PostUpdate,
|
||||
footstep.system().after(TransformSystem::TransformPropagate),
|
||||
|
|
Loading…
Reference in New Issue
Block a user