Support randomized footsteps.
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
6f025d49aa
commit
dc56b5fc6d
|
@ -1,14 +1,14 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use bevy::{prelude::*, transform::TransformSystem};
|
use bevy::{prelude::*, transform::TransformSystem};
|
||||||
use bevy_synthizer::{Audio, Sound};
|
use bevy_synthizer::{Audio, Buffer, Sound};
|
||||||
use rand::random;
|
use rand::prelude::*;
|
||||||
|
|
||||||
use crate::core::PointLike;
|
use crate::core::PointLike;
|
||||||
|
|
||||||
#[derive(Component, Clone, Debug)]
|
#[derive(Component, Clone, Debug)]
|
||||||
pub struct Footstep {
|
pub struct Footstep {
|
||||||
pub audio: Audio,
|
pub audio: Option<Audio>,
|
||||||
pub step_length: f32,
|
pub step_length: f32,
|
||||||
pub gain: f64,
|
pub gain: f64,
|
||||||
pub pitch: Option<f64>,
|
pub pitch: Option<f64>,
|
||||||
|
@ -27,37 +27,92 @@ impl Default for Footstep {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Clone, Default, Deref, DerefMut, Debug)]
|
||||||
|
pub struct Footsteps(pub Vec<Audio>);
|
||||||
|
|
||||||
|
impl Footsteps {
|
||||||
|
pub fn from_handles(handles: Vec<Handle<Buffer>>) -> Self {
|
||||||
|
Self(
|
||||||
|
handles
|
||||||
|
.iter()
|
||||||
|
.cloned()
|
||||||
|
.map(|v| v.into())
|
||||||
|
.collect::<Vec<Audio>>(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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 {
|
for (entity, footstep) in &footsteps {
|
||||||
let buffer = footstep.audio.clone();
|
if let Some(audio) = &footstep.audio {
|
||||||
commands.entity(entity).insert(Sound {
|
commands.entity(entity).insert(Sound {
|
||||||
audio: buffer.into(),
|
audio: audio.clone(),
|
||||||
paused: true,
|
paused: true,
|
||||||
..default()
|
..default()
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(
|
fn update(
|
||||||
|
mut commands: Commands,
|
||||||
mut last_step_distance: Local<HashMap<Entity, (f32, GlobalTransform)>>,
|
mut last_step_distance: Local<HashMap<Entity, (f32, GlobalTransform)>>,
|
||||||
mut footsteps: Query<(Entity, &Footstep, &Parent, &mut Sound)>,
|
mut query: Query<(
|
||||||
|
Entity,
|
||||||
|
&Footstep,
|
||||||
|
&Parent,
|
||||||
|
Option<&mut Sound>,
|
||||||
|
Option<&Footsteps>,
|
||||||
|
)>,
|
||||||
transforms_storage: Query<&GlobalTransform>,
|
transforms_storage: Query<&GlobalTransform>,
|
||||||
) {
|
) {
|
||||||
for (entity, footstep, parent, mut sound) in &mut footsteps {
|
for (entity, footstep, parent, sound, footsteps) in &mut query {
|
||||||
if let Ok(transform) = transforms_storage.get(**parent) {
|
if let Ok(transform) = transforms_storage.get(**parent) {
|
||||||
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(transform));
|
let distance = last.0 + (last.1.distance(transform));
|
||||||
if distance >= footstep.step_length {
|
if distance >= footstep.step_length {
|
||||||
last_step_distance.insert(entity, (0., *transform));
|
last_step_distance.insert(entity, (0., *transform));
|
||||||
sound.gain = footstep.gain;
|
if let Some(footsteps) = footsteps {
|
||||||
sound.pitch = footstep.pitch.unwrap_or(1.);
|
let audio = if footsteps.len() == 1 {
|
||||||
if let Some(pitch_variation) = footstep.pitch_variation {
|
Some(footsteps[0].clone())
|
||||||
let mut pitch = sound.pitch - pitch_variation / 2.;
|
} else if !footsteps.is_empty() {
|
||||||
pitch += random::<f64>() * pitch_variation;
|
let mut footsteps = footsteps.clone();
|
||||||
sound.pitch = pitch;
|
footsteps.shuffle(&mut thread_rng());
|
||||||
|
let mut audio = footsteps.first().unwrap().clone();
|
||||||
|
if let Some(sound) = sound {
|
||||||
|
while sound.audio == audio {
|
||||||
|
footsteps.shuffle(&mut thread_rng());
|
||||||
|
audio = footsteps[0].clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(audio)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
if let Some(audio) = audio {
|
||||||
|
let mut pitch = 1.;
|
||||||
|
if let Some(pitch_variation) = footstep.pitch_variation {
|
||||||
|
pitch -= pitch_variation / 2.;
|
||||||
|
pitch += random::<f64>() * pitch_variation;
|
||||||
|
}
|
||||||
|
commands.entity(entity).insert(Sound {
|
||||||
|
audio,
|
||||||
|
gain: footstep.gain,
|
||||||
|
pitch,
|
||||||
|
..default()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if let Some(mut sound) = sound {
|
||||||
|
sound.gain = footstep.gain;
|
||||||
|
sound.pitch = footstep.pitch.unwrap_or(1.);
|
||||||
|
if let Some(pitch_variation) = footstep.pitch_variation {
|
||||||
|
let mut pitch = sound.pitch - pitch_variation / 2.;
|
||||||
|
pitch += random::<f64>() * pitch_variation;
|
||||||
|
sound.pitch = pitch;
|
||||||
|
}
|
||||||
|
sound.paused = false;
|
||||||
|
sound.restart = true;
|
||||||
}
|
}
|
||||||
sound.paused = false;
|
|
||||||
sound.restart = true;
|
|
||||||
} else if last.1 != *transform {
|
} else if last.1 != *transform {
|
||||||
last_step_distance.insert(entity, (distance, *transform));
|
last_step_distance.insert(entity, (distance, *transform));
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ pub mod icon;
|
||||||
pub mod pitch_shift;
|
pub mod pitch_shift;
|
||||||
pub mod volumetric;
|
pub mod volumetric;
|
||||||
|
|
||||||
pub use footstep::Footstep;
|
pub use footstep::{Footstep, Footsteps};
|
||||||
pub use icon::{SoundIcon, SoundIconPlugin};
|
pub use icon::{SoundIcon, SoundIconPlugin};
|
||||||
pub use pitch_shift::PitchShiftPlugin;
|
pub use pitch_shift::PitchShiftPlugin;
|
||||||
pub use volumetric::Volumetric;
|
pub use volumetric::Volumetric;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user