49 lines
1.3 KiB
Rust
49 lines
1.3 KiB
Rust
|
use std::{fmt::Debug, hash::Hash};
|
||
|
|
||
|
use bevy::prelude::*;
|
||
|
use bevy_openal::{Context, Sound};
|
||
|
|
||
|
use crate::core::CoreConfig;
|
||
|
|
||
|
pub mod footstep;
|
||
|
pub mod icon;
|
||
|
|
||
|
pub use footstep::{Footstep, FootstepBundle};
|
||
|
pub use icon::{SoundIcon, SoundIconBundle};
|
||
|
|
||
|
fn scale_sounds(config: Res<CoreConfig>, mut sounds: Query<&mut Sound>) {
|
||
|
let pixels_per_unit = config.pixels_per_unit as f32;
|
||
|
for mut sound in sounds.iter_mut() {
|
||
|
sound.reference_distance *= pixels_per_unit;
|
||
|
if sound.max_distance != f32::MAX {
|
||
|
sound.max_distance *= pixels_per_unit;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub struct SoundPlugin<'a, S>(std::marker::PhantomData<&'a S>);
|
||
|
|
||
|
impl<'a, S> Default for SoundPlugin<'a, S> {
|
||
|
fn default() -> Self {
|
||
|
Self(std::marker::PhantomData)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a, S> Plugin for SoundPlugin<'a, S>
|
||
|
where
|
||
|
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
||
|
'a: 'static,
|
||
|
{
|
||
|
fn build(&self, app: &mut App) {
|
||
|
let core_config = *app.world.get_resource::<CoreConfig>().unwrap();
|
||
|
if let Some(context) = app.world.get_resource::<Context>() {
|
||
|
context
|
||
|
.set_meters_per_unit(1. / core_config.pixels_per_unit as f32)
|
||
|
.unwrap();
|
||
|
}
|
||
|
app.add_plugin(footstep::FootstepPlugin)
|
||
|
.add_plugin(icon::SoundIconPlugin::<S>::default())
|
||
|
.add_system(scale_sounds);
|
||
|
}
|
||
|
}
|