Support randomized footsteps.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nolan Darilek 2023-04-03 11:56:27 -05:00
parent 6f025d49aa
commit dc56b5fc6d
2 changed files with 75 additions and 20 deletions

View File

@ -1,14 +1,14 @@
use std::collections::HashMap;
use bevy::{prelude::*, transform::TransformSystem};
use bevy_synthizer::{Audio, Sound};
use rand::random;
use bevy_synthizer::{Audio, Buffer, Sound};
use rand::prelude::*;
use crate::core::PointLike;
#[derive(Component, Clone, Debug)]
pub struct Footstep {
pub audio: Audio,
pub audio: Option<Audio>,
pub step_length: f32,
pub gain: f64,
pub pitch: Option<f64>,
@ -27,28 +27,82 @@ 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>>) {
for (entity, footstep) in &footsteps {
let buffer = footstep.audio.clone();
if let Some(audio) = &footstep.audio {
commands.entity(entity).insert(Sound {
audio: buffer.into(),
audio: audio.clone(),
paused: true,
..default()
});
}
}
}
fn update(
mut commands: Commands,
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>,
) {
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 Some(last) = last_step_distance.get(&entity) {
let distance = last.0 + (last.1.distance(transform));
if distance >= footstep.step_length {
last_step_distance.insert(entity, (0., *transform));
if let Some(footsteps) = footsteps {
let audio = if footsteps.len() == 1 {
Some(footsteps[0].clone())
} else if !footsteps.is_empty() {
let mut footsteps = footsteps.clone();
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 {
@ -58,6 +112,7 @@ fn update(
}
sound.paused = false;
sound.restart = true;
}
} else if last.1 != *transform {
last_step_distance.insert(entity, (distance, *transform));
}

View File

@ -5,7 +5,7 @@ pub mod icon;
pub mod pitch_shift;
pub mod volumetric;
pub use footstep::Footstep;
pub use footstep::{Footstep, Footsteps};
pub use icon::{SoundIcon, SoundIconPlugin};
pub use pitch_shift::PitchShiftPlugin;
pub use volumetric::Volumetric;