Compare commits

..

No commits in common. "1022037efe716c0b6e80d842b956250a079b7e75" and "6d8f2356572fdeb83979990708796488295bcd7a" have entirely different histories.

4 changed files with 140 additions and 172 deletions

View File

@ -1,17 +1,45 @@
use std::{fmt::Debug, hash::Hash, time::Duration};
use std::{collections::HashMap, fmt::Debug, hash::Hash, time::Duration};
use bevy::{asset::HandleId, prelude::*, transform::TransformSystem};
use bevy_openal::{Buffer, Sound, SoundState};
use bevy_openal::{Buffer, Context, Sound, SoundState};
use rand::random;
use crate::{
commands::RunIfExistsExt,
core::Player,
core::{CoreConfig, Player, PointLike},
exploration::ExplorationFocused,
visibility::{Visible, VisibleEntities},
};
#[derive(Component, Clone, Debug, Reflect)]
#[reflect(Component)]
pub struct Footstep {
pub sound: HandleId,
pub step_length: f32,
pub gain: f32,
pub reference_distance: f32,
pub max_distance: f32,
pub rolloff_factor: f32,
pub pitch: f32,
pub pitch_variation: Option<f32>,
}
impl Default for Footstep {
fn default() -> Self {
Self {
sound: "".into(),
step_length: 0.8,
gain: 1.,
reference_distance: 1.,
max_distance: f32::MAX,
rolloff_factor: 1.,
pitch: 1.,
pitch_variation: Some(0.15),
}
}
}
#[derive(Component, Clone, Debug)]
pub struct SoundIcon {
pub sound: HandleId,
@ -43,6 +71,13 @@ impl Default for SoundIcon {
}
}
#[derive(Bundle, Default)]
pub struct FootstepBundle {
pub footstep: Footstep,
pub transform: Transform,
pub global_transform: GlobalTransform,
}
#[derive(Bundle, Clone, Debug, Default)]
pub struct SoundIconBundle {
pub sound_icon: SoundIcon,
@ -50,6 +85,54 @@ pub struct SoundIconBundle {
pub global_transform: GlobalTransform,
}
fn add_footstep_sounds(
mut commands: Commands,
footsteps: Query<(Entity, &Footstep), Added<Footstep>>,
assets: Res<Assets<Buffer>>,
) {
for (entity, footstep) in footsteps.iter() {
let buffer = assets.get_handle(footstep.sound);
commands.run_if_exists(entity, move |mut entity| {
entity.insert(Sound {
buffer,
state: SoundState::Stopped,
..default()
});
});
}
}
fn footstep(
mut last_step_distance: Local<HashMap<Entity, (f32, Transform)>>,
mut footsteps: Query<(Entity, &Footstep, &Parent, &mut Sound), Changed<GlobalTransform>>,
transforms_storage: Query<&Transform>,
) {
for (entity, footstep, parent, mut sound) in footsteps.iter_mut() {
let coordinates = transforms_storage.get(**parent).unwrap();
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;
sound.pitch = footstep.pitch;
if let Some(pitch_variation) = footstep.pitch_variation {
let mut pitch = footstep.pitch - 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 {
last_step_distance.insert(entity, (0., *coordinates));
}
}
}
fn add_sound_icon_sounds(
mut commands: Commands,
icons: Query<(Entity, &SoundIcon), Added<SoundIcon>>,
@ -79,8 +162,8 @@ fn add_sound_icon_sounds(
}
}
fn update<S>(
config: Res<SoundIconConfig<S>>,
fn sound_icon<S>(
config: Res<SoundConfig<S>>,
state: Res<State<S>>,
time: Res<Time>,
viewers: Query<&VisibleEntities, With<Player>>,
@ -95,7 +178,9 @@ fn update<S>(
) where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
{
if !(*config).states.is_empty() && !config.states.contains(state.current()) {
if !(*config).sound_icon_states.is_empty()
&& !config.sound_icon_states.contains(state.current())
{
return;
}
for visible in viewers.iter() {
@ -133,7 +218,7 @@ fn update<S>(
}
}
fn exploration_focus_changed(
fn sound_icon_exploration_focus_changed(
mut focused: Query<(Entity, Option<&Children>), Changed<ExplorationFocused>>,
mut icons: Query<&mut SoundIcon>,
) {
@ -152,7 +237,7 @@ fn exploration_focus_changed(
}
}
fn exploration_focus_removed(
fn sound_icon_exploration_focus_removed(
removed: RemovedComponents<ExplorationFocused>,
mut query: Query<&mut SoundIcon>,
children: Query<&Children>,
@ -172,37 +257,71 @@ fn exploration_focus_removed(
}
}
#[derive(Clone, Debug, Default)]
pub struct SoundIconConfig<S> {
pub states: Vec<S>,
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 SoundIconPlugin<'a, S>(std::marker::PhantomData<&'a S>);
#[derive(Clone, Debug)]
pub struct SoundConfig<S> {
pub sound_icon_states: Vec<S>,
}
impl<'a, S> Default for SoundIconPlugin<'a, S> {
impl<S> Default for SoundConfig<S> {
fn default() -> Self {
Self {
sound_icon_states: vec![],
}
}
}
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 SoundIconPlugin<'a, S>
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) {
app.add_system(add_sound_icon_sounds)
if !app.world.contains_resource::<SoundConfig<S>>() {
app.insert_resource(SoundConfig::<S>::default());
}
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.register_type::<Footstep>()
.add_system(add_footstep_sounds)
.add_system_to_stage(
CoreStage::PostUpdate,
update::<S>.after(TransformSystem::TransformPropagate),
footstep.after(TransformSystem::TransformPropagate),
)
.add_system(add_sound_icon_sounds)
.add_system_to_stage(
CoreStage::PostUpdate,
sound_icon::<S>.after(TransformSystem::TransformPropagate),
)
.add_system_to_stage(
CoreStage::PostUpdate,
exploration_focus_changed.after(update::<S>),
sound_icon_exploration_focus_changed.after(sound_icon::<S>),
)
.add_system_to_stage(
CoreStage::PostUpdate,
exploration_focus_removed.after(exploration_focus_changed),
);
sound_icon_exploration_focus_removed.after(sound_icon_exploration_focus_changed),
)
.add_system(scale_sounds);
}
}

View File

@ -1,103 +0,0 @@
use std::collections::HashMap;
use bevy::{asset::HandleId, prelude::*, transform::TransformSystem};
use bevy_openal::{Buffer, Sound, SoundState};
use rand::random;
use crate::{commands::RunIfExistsExt, core::PointLike};
#[derive(Component, Clone, Debug, Reflect)]
#[reflect(Component)]
pub struct Footstep {
pub sound: HandleId,
pub step_length: f32,
pub gain: f32,
pub reference_distance: f32,
pub max_distance: f32,
pub rolloff_factor: f32,
pub pitch: f32,
pub pitch_variation: Option<f32>,
}
impl Default for Footstep {
fn default() -> Self {
Self {
sound: "".into(),
step_length: 0.8,
gain: 1.,
reference_distance: 1.,
max_distance: f32::MAX,
rolloff_factor: 1.,
pitch: 1.,
pitch_variation: Some(0.15),
}
}
}
#[derive(Bundle, Default)]
pub struct FootstepBundle {
pub footstep: Footstep,
pub transform: Transform,
pub global_transform: GlobalTransform,
}
fn added(
mut commands: Commands,
footsteps: Query<(Entity, &Footstep), Added<Footstep>>,
assets: Res<Assets<Buffer>>,
) {
for (entity, footstep) in footsteps.iter() {
let buffer = assets.get_handle(footstep.sound);
commands.run_if_exists(entity, move |mut entity| {
entity.insert(Sound {
buffer,
state: SoundState::Stopped,
..default()
});
});
}
}
fn update(
mut last_step_distance: Local<HashMap<Entity, (f32, Transform)>>,
mut footsteps: Query<(Entity, &Footstep, &Parent, &mut Sound), Changed<GlobalTransform>>,
transforms_storage: Query<&Transform>,
) {
for (entity, footstep, parent, mut sound) in footsteps.iter_mut() {
let coordinates = transforms_storage.get(**parent).unwrap();
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;
sound.pitch = footstep.pitch;
if let Some(pitch_variation) = footstep.pitch_variation {
let mut pitch = footstep.pitch - 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 {
last_step_distance.insert(entity, (0., *coordinates));
}
}
}
pub struct FootstepPlugin;
impl Plugin for FootstepPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Footstep>()
.add_system(added)
.add_system_to_stage(
CoreStage::PostUpdate,
update.after(TransformSystem::TransformPropagate),
);
}
}

View File

@ -1,48 +0,0 @@
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);
}
}

View File

@ -76,7 +76,7 @@ impl Viewshed {
for e in entities {
new_visible_entities.insert(*e);
}
if entities.contains(viewer_entity) {
if entities.contains(&viewer_entity) {
return 0;
} else {
return *opacity;
@ -107,7 +107,7 @@ impl Viewshed {
for e in &entities {
new_visible_entities.insert(*e);
}
if entities.contains(viewer_entity) {
if entities.contains(&viewer_entity) {
0
} else {
cache.insert(coord, (opacity, entities));