Compare commits

..

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

7 changed files with 85 additions and 214 deletions

View File

@ -25,8 +25,8 @@ features = [
[dependencies]
backtrace = "0.3"
bevy_input_actionmap = { git = "https://github.com/lightsoutgames/bevy_input_actionmap" }
bevy_openal = { git = "https://github.com/lightsoutgames/bevy_openal" }
bevy_rapier2d = "0.13"
bevy_synthizer = { git = "https://labs.lightsout.games/projects/bevy_synthizer" }
bevy_tts = { git = "https://github.com/lightsoutgames/bevy_tts", default-features = false, features = ["tolk"] }
coord_2d = "0.3"
futures-lite = "1"
@ -36,6 +36,6 @@ maze_generator = "2"
once_cell = "1"
pathfinding = "3"
rand = "0.8"
sentry = "0.26"
sentry = "0.25"
serde = "1"
shadowcast = "0.8"

View File

@ -586,7 +586,7 @@ impl<RapierUserData: 'static + WorldQuery + Send + Sync> PluginGroup
fn build(&mut self, group: &mut bevy::app::PluginGroupBuilder) {
group
.add(crate::bevy_tts::TtsPlugin)
.add(crate::bevy_synthizer::SynthizerPlugin)
.add(crate::bevy_openal::OpenAlPlugin)
.add(CorePlugin::<RapierUserData>::default());
}
}

View File

@ -3,7 +3,7 @@
#![allow(clippy::type_complexity)]
pub use bevy_input_actionmap;
pub use bevy_synthizer;
pub use bevy_openal;
pub use bevy_rapier2d;
pub use bevy_tts;
pub mod commands;

View File

@ -3,7 +3,7 @@ use std::collections::HashMap;
use bevy::prelude::*;
use crate::{
bevy_synthizer::{Listener, Sound},
bevy_openal::{Listener, Sound},
commands::RunIfExistsExt,
sound::SoundIcon,
};
@ -14,7 +14,7 @@ struct Behind;
#[derive(Clone, Copy, Debug)]
pub struct PitchShiftConfig {
pub downshift_behind: bool,
pub downshift: f64,
pub downshift: f32,
}
impl Default for PitchShiftConfig {
@ -64,10 +64,10 @@ fn tag_behind(
}
#[derive(Clone, Debug, Default, Deref, DerefMut)]
struct LastIconPitch(HashMap<Entity, f64>);
struct LastIconPitch(HashMap<Entity, f32>);
#[derive(Clone, Debug, Default, Deref, DerefMut)]
struct LastSoundPitch(HashMap<Entity, f64>);
struct LastSoundPitch(HashMap<Entity, f32>);
fn behind_added(
config: Res<PitchShiftConfig>,

View File

@ -1,7 +1,7 @@
use std::collections::HashMap;
use bevy::{asset::HandleId, prelude::*, transform::TransformSystem};
use bevy_synthizer::{DistanceMax, DistanceRef, Rolloff, Sound};
use bevy_openal::{Buffer, Sound, SoundState};
use rand::random;
use crate::{commands::RunIfExistsExt, core::PointLike};
@ -11,12 +11,12 @@ use crate::{commands::RunIfExistsExt, core::PointLike};
pub struct Footstep {
pub sound: HandleId,
pub step_length: f32,
pub gain: f64,
pub reference_distance: Option<f64>,
pub max_distance: Option<f64>,
pub rolloff: Option<f64>,
pub pitch: Option<f64>,
pub pitch_variation: Option<f64>,
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 {
@ -25,10 +25,10 @@ impl Default for Footstep {
sound: "".into(),
step_length: 0.8,
gain: 1.,
reference_distance: None,
max_distance: None,
rolloff: None,
pitch: None,
reference_distance: 1.,
max_distance: f32::MAX,
rolloff_factor: 1.,
pitch: 1.,
pitch_variation: Some(0.15),
}
}
@ -43,15 +43,15 @@ pub struct FootstepBundle {
fn added(
mut commands: Commands,
asset_server: Res<AssetServer>,
footsteps: Query<(Entity, &Footstep), Added<Footstep>>,
assets: Res<Assets<Buffer>>,
) {
for (entity, footstep) in footsteps.iter() {
let buffer = asset_server.get_handle(footstep.sound);
let buffer = assets.get_handle(footstep.sound);
commands.run_if_exists(entity, move |mut entity| {
entity.insert(Sound {
buffer,
paused: true,
state: SoundState::Stopped,
..default()
});
});
@ -59,87 +59,27 @@ fn added(
}
fn update(
mut commands: Commands,
mut last_step_distance: Local<HashMap<Entity, (f32, Transform)>>,
mut footsteps: Query<
(
Entity,
&Footstep,
&Parent,
&mut Sound,
Option<&DistanceRef>,
Option<&DistanceMax>,
Option<&Rolloff>,
),
Changed<GlobalTransform>,
>,
mut footsteps: Query<(Entity, &Footstep, &Parent, &mut Sound), Changed<GlobalTransform>>,
transforms_storage: Query<&Transform>,
) {
for (entity, footstep, parent, mut sound, reference_distance, max_distance, rolloff) in
footsteps.iter_mut()
{
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;
if let Some(v) = footstep.reference_distance {
let insert = if let Some(v2) = reference_distance {
v != **v2
} else {
true
};
if insert {
commands.run_if_exists(entity, move |mut entity| {
entity.insert(DistanceRef(v));
})
}
} else if reference_distance.is_some() {
commands.run_if_exists(entity, |mut entity| {
entity.remove::<DistanceRef>();
});
}
if let Some(v) = footstep.max_distance {
let insert = if let Some(v2) = max_distance {
v != **v2
} else {
true
};
if insert {
commands.run_if_exists(entity, move |mut entity| {
entity.insert(DistanceMax(v));
})
}
} else if max_distance.is_some() {
commands.run_if_exists(entity, |mut entity| {
entity.remove::<DistanceMax>();
});
}
if let Some(v) = footstep.rolloff {
let insert = if let Some(v2) = rolloff {
v != **v2
} else {
true
};
if insert {
commands.run_if_exists(entity, move |mut entity| {
entity.insert(Rolloff(v));
})
}
} else if rolloff.is_some() {
commands.run_if_exists(entity, |mut entity| {
entity.remove::<Rolloff>();
});
}
sound.pitch = footstep.pitch.unwrap_or(1.);
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 = sound.pitch - pitch_variation / 2.;
pitch += random::<f64>() * pitch_variation;
let mut pitch = footstep.pitch - pitch_variation / 2.;
pitch += random::<f32>() * pitch_variation;
sound.pitch = pitch;
}
sound.paused = false;
sound.restart = true;
sound.play();
} else if last.1 != *coordinates {
last_step_distance.insert(entity, (distance, *coordinates));
}

View File

@ -1,7 +1,7 @@
use std::{fmt::Debug, hash::Hash, time::Duration};
use bevy::{asset::HandleId, prelude::*, transform::TransformSystem};
use bevy_synthizer::{DistanceMax, DistanceRef, Rolloff, Sound};
use bevy_openal::{Buffer, Sound, SoundState};
use rand::random;
@ -15,11 +15,11 @@ use crate::{
#[derive(Component, Clone, Debug)]
pub struct SoundIcon {
pub sound: HandleId,
pub gain: f64,
pub pitch: f64,
pub reference_distance: Option<f64>,
pub max_distance: Option<f64>,
pub rolloff: Option<f64>,
pub gain: f32,
pub pitch: f32,
pub reference_distance: f32,
pub max_distance: f32,
pub rolloff_factor: f32,
pub interval: Option<Timer>,
}
@ -28,11 +28,11 @@ impl Default for SoundIcon {
let seconds = random::<f32>() + 4.5;
let mut icon = Self {
sound: "".into(),
gain: 1.,
gain: 0.3,
pitch: 1.,
reference_distance: None,
max_distance: None,
rolloff: None,
reference_distance: 1.,
max_distance: f32::MAX,
rolloff_factor: 1.,
interval: Some(Timer::from_seconds(seconds, true)),
};
if let Some(ref mut interval) = icon.interval {
@ -50,43 +50,36 @@ pub struct SoundIconBundle {
pub global_transform: GlobalTransform,
}
fn added(
fn add_sound_icon_sounds(
mut commands: Commands,
asset_server: Res<AssetServer>,
icons: Query<(Entity, &SoundIcon), Added<SoundIcon>>,
assets: Res<Assets<Buffer>>,
) {
for (entity, icon) in icons.iter() {
let buffer = asset_server.get_handle(icon.sound);
let buffer = assets.get_handle(icon.sound);
let gain = icon.gain;
let pitch = icon.pitch;
let looping = icon.interval.is_none();
let reference_distance = icon.reference_distance;
let max_distance = icon.max_distance;
let rolloff = icon.rolloff;
let rolloff_factor = icon.rolloff_factor;
commands.run_if_exists(entity, move |mut entity| {
entity.insert(Sound {
buffer,
gain,
pitch,
looping,
paused: true,
state: SoundState::Stopped,
reference_distance,
max_distance,
rolloff_factor,
..default()
});
if let Some(v) = reference_distance {
entity.insert(DistanceRef(v));
}
if let Some(v) = max_distance {
entity.insert(DistanceMax(v));
}
if let Some(v) = rolloff {
entity.insert(Rolloff(v));
}
});
}
}
fn update<S>(
mut commands: Commands,
config: Res<SoundIconConfig<S>>,
state: Res<State<S>>,
time: Res<Time>,
@ -97,11 +90,8 @@ fn update<S>(
Option<&Visible>,
Option<&Parent>,
&mut Sound,
Option<&DistanceRef>,
Option<&DistanceMax>,
Option<&Rolloff>,
)>,
asset_server: Res<AssetServer>,
buffers: Res<Assets<Buffer>>,
) where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
{
@ -109,95 +99,35 @@ fn update<S>(
return;
}
for visible in viewers.iter() {
for (
icon_entity,
mut icon,
visibility,
parent,
mut sound,
distance_ref,
distance_max,
rolloff,
) in icons.iter_mut()
{
for (icon_entity, mut icon, visibility, parent, mut sound) in icons.iter_mut() {
let entity = if visibility.is_some() {
Some(icon_entity)
} else if parent.is_some() {
Some(**parent.unwrap())
icon_entity
} else {
None
**parent.unwrap()
};
if let Some(entity) = entity {
if visible.contains(&entity) {
if sound.looping {
sound.paused = false;
let looping = sound.looping;
if looping {
sound.state = SoundState::Playing;
} else if let Some(interval) = icon.interval.as_mut() {
interval.tick(time.delta());
if interval.finished() {
sound.paused = false;
sound.restart = true;
sound.state = SoundState::Playing;
interval.reset();
}
}
let buffer = asset_server.get_handle(icon.sound);
let buffer = buffers.get_handle(icon.sound);
sound.looping = icon.interval.is_none();
if sound.buffer != buffer {
sound.buffer = buffer;
}
sound.gain = icon.gain;
sound.pitch = icon.pitch;
sound.looping = icon.interval.is_none();
if let Some(v) = icon.reference_distance {
let insert = if let Some(v2) = distance_ref {
v != **v2
sound.reference_distance = icon.reference_distance;
sound.max_distance = icon.max_distance;
sound.rolloff_factor = icon.rolloff_factor;
} else {
true
};
if insert {
commands.run_if_exists(icon_entity, move |mut entity| {
entity.insert(DistanceRef(v));
})
}
} else if distance_ref.is_some() {
commands.run_if_exists(icon_entity, |mut entity| {
entity.remove::<DistanceRef>();
});
}
if let Some(v) = icon.max_distance {
let insert = if let Some(v2) = distance_max {
v != **v2
} else {
true
};
if insert {
commands.run_if_exists(icon_entity, move |mut entity| {
entity.insert(DistanceMax(v));
})
}
} else if distance_max.is_some() {
commands.run_if_exists(icon_entity, |mut entity| {
entity.remove::<DistanceMax>();
});
}
if let Some(v) = icon.rolloff {
let insert = if let Some(v2) = rolloff {
v != **v2
} else {
true
};
if insert {
commands.run_if_exists(icon_entity, move |mut entity| {
entity.insert(Rolloff(v));
})
}
} else if rolloff.is_some() {
commands.run_if_exists(icon_entity, |mut entity| {
entity.remove::<Rolloff>();
});
}
} else if !sound.paused {
sound.paused = true;
sound.restart = true;
}
sound.state = SoundState::Stopped;
}
}
}
@ -207,7 +137,7 @@ fn exploration_focus_changed(
mut focused: Query<(Entity, Option<&Children>), Changed<ExplorationFocused>>,
mut icons: Query<&mut SoundIcon>,
) {
const ICON_GAIN: f64 = 3.;
const ICON_GAIN: f32 = 3.;
for (entity, children) in focused.iter_mut() {
if let Ok(mut icon) = icons.get_mut(entity) {
icon.gain *= ICON_GAIN;
@ -227,7 +157,7 @@ fn exploration_focus_removed(
mut query: Query<&mut SoundIcon>,
children: Query<&Children>,
) {
const ICON_GAIN: f64 = 3.;
const ICON_GAIN: f32 = 3.;
for entity in removed.iter() {
if let Ok(mut icon) = query.get_mut(entity) {
icon.gain /= ICON_GAIN;
@ -261,7 +191,7 @@ where
'a: 'static,
{
fn build(&self, app: &mut App) {
app.add_system(added)
app.add_system(add_sound_icon_sounds)
.add_system_to_stage(
CoreStage::PostUpdate,
update::<S>.after(TransformSystem::TransformPropagate),

View File

@ -1,6 +1,7 @@
use std::{fmt::Debug, hash::Hash};
use bevy::prelude::*;
use bevy_openal::{Context, Sound};
use crate::core::CoreConfig;
@ -10,7 +11,7 @@ pub mod icon;
pub use footstep::{Footstep, FootstepBundle};
pub use icon::{SoundIcon, SoundIconBundle};
/*fn scale_sounds(config: Res<CoreConfig>, mut sounds: Query<&mut Sound>) {
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;
@ -18,7 +19,7 @@ pub use icon::{SoundIcon, SoundIconBundle};
sound.max_distance *= pixels_per_unit;
}
}
}*/
}
pub struct SoundPlugin<'a, S>(std::marker::PhantomData<&'a S>);
@ -34,14 +35,14 @@ where
'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>() {
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);
.add_plugin(icon::SoundIconPlugin::<S>::default())
.add_system(scale_sounds);
}
}