blackout/src/sound/footstep.rs

135 lines
4.4 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
2022-08-03 14:43:07 +00:00
use bevy::{prelude::*, transform::TransformSystem};
2023-04-03 16:56:27 +00:00
use bevy_synthizer::{Audio, Buffer, Sound};
use rand::prelude::*;
2023-04-01 12:20:59 +00:00
use crate::core::PointLike;
#[derive(Component, Clone, Debug)]
pub struct Footstep {
2023-04-03 16:56:27 +00:00
pub audio: Option<Audio>,
pub step_length: f32,
2022-05-23 18:35:25 +00:00
pub gain: f64,
pub pitch: Option<f64>,
pub pitch_variation: Option<f64>,
}
impl Default for Footstep {
fn default() -> Self {
Self {
audio: default(),
step_length: 0.8,
gain: 1.,
2022-05-23 18:35:25 +00:00
pitch: None,
pitch_variation: Some(0.15),
}
}
}
2023-04-03 16:56:27 +00:00
#[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>>(),
)
}
}
2022-08-03 14:43:07 +00:00
fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added<Footstep>>) {
2023-03-28 17:13:23 +00:00
for (entity, footstep) in &footsteps {
2023-04-03 16:56:27 +00:00
if let Some(audio) = &footstep.audio {
let mut pitch = 1.;
if let Some(pitch_variation) = footstep.pitch_variation {
pitch -= pitch_variation / 2.;
pitch += random::<f64>() * pitch_variation;
}
2023-04-03 16:56:27 +00:00
commands.entity(entity).insert(Sound {
audio: audio.clone(),
gain: footstep.gain,
pitch,
2023-04-03 16:56:27 +00:00
paused: true,
..default()
});
}
}
}
fn update(
2023-04-03 16:56:27 +00:00
mut commands: Commands,
mut last_step_distance: Local<HashMap<Entity, (f32, GlobalTransform)>>,
2023-04-03 16:56:27 +00:00
mut query: Query<(
Entity,
&Footstep,
&Parent,
Option<&mut Sound>,
Option<&Footsteps>,
)>,
transforms_storage: Query<&GlobalTransform>,
) {
2023-04-03 16:56:27 +00:00
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));
let audio = if let Some(footsteps) = footsteps {
if footsteps.len() == 1 {
2023-04-03 16:56:27 +00:00
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
}
} else {
footstep.audio.clone()
};
if let Some(audio) = audio {
let mut pitch = 1.;
2023-04-03 16:56:27 +00:00
if let Some(pitch_variation) = footstep.pitch_variation {
pitch -= pitch_variation / 2.;
2023-04-03 16:56:27 +00:00
pitch += random::<f64>() * pitch_variation;
}
commands.entity(entity).insert(Sound {
audio,
gain: footstep.gain,
pitch,
..default()
});
2022-05-23 18:35:25 +00:00
}
} else if last.1 != *transform {
last_step_distance.insert(entity, (distance, *transform));
}
} else {
last_step_distance.insert(entity, (0., *transform));
}
}
}
}
pub struct FootstepPlugin;
impl Plugin for FootstepPlugin {
fn build(&self, app: &mut App) {
2023-09-08 21:12:35 +00:00
app.add_systems(PreUpdate, added).add_systems(
2025-01-08 01:51:50 +00:00
FixedPostUpdate,
2023-09-08 21:12:35 +00:00
update.after(TransformSystem::TransformPropagate),
);
}
}