Fix volumetric sound and adapt sound icon code to new bevy_synthizer.

This commit is contained in:
Nolan Darilek 2024-12-02 13:30:37 -06:00
parent 8c930c321d
commit d268ed2b0c
3 changed files with 65 additions and 49 deletions

View File

@ -49,7 +49,7 @@ features = [
[dependencies]
bevy_rapier2d = "0.27"
bevy_synthizer = "0.7"
bevy_synthizer = "0.8"
bevy_tts = { version = "0.9", default-features = false, features = ["tolk"] }
coord_2d = "0.3"
here_be_dragons = { version = "0.3", features = ["serde"] }

View File

@ -75,42 +75,43 @@ fn update<S>(
} else {
None
};
if let Some(entity) = entity {
if visible.contains(&entity) {
if let Some(interval) = icon.interval.as_mut() {
if interval.finished() {
interval.reset();
continue;
} else if interval.fraction() == 0. {
sound.generator = None;
}
interval.tick(time.delta());
}
if sound.audio != icon.audio {
sound.audio = icon.audio.clone();
}
if sound.gain != icon.gain {
sound.gain = icon.gain;
}
if sound.pitch != icon.pitch {
sound.pitch = icon.pitch;
}
let looping = icon.interval.is_none();
if sound.looping != looping {
sound.looping = looping;
}
if sound.paused {
sound.paused = false;
let Some(entity) = entity else {
continue;
};
if visible.contains(&entity) {
if let Some(interval) = icon.interval.as_mut() {
if interval.finished() {
interval.reset();
continue;
} else if interval.fraction() == 0. {
sound.playback_position = 0.;
sound.generator = None;
}
} else if !sound.paused {
sound.paused = true;
if let Some(interval) = icon.interval.as_mut() {
interval.reset();
}
interval.tick(time.delta());
}
if sound.audio != icon.audio {
sound.audio = icon.audio.clone();
}
if sound.gain != icon.gain {
sound.gain = icon.gain;
}
if sound.pitch != icon.pitch {
sound.pitch = icon.pitch;
}
let looping = icon.interval.is_none();
if sound.looping != looping {
sound.looping = looping;
}
if sound.paused {
sound.paused = false;
sound.playback_position = 0.;
sound.generator = None;
}
} else if !sound.paused {
sound.paused = true;
if let Some(interval) = icon.interval.as_mut() {
interval.reset();
}
} else {
panic!("Should not happen");
}
}
}

View File

@ -9,31 +9,46 @@ use crate::core::GlobalTransformExt;
pub struct Volumetric;
fn update(
mut commands: Commands,
listener: Query<(&Collider, &GlobalTransform), With<Listener>>,
mut sounds: Query<(&Sound, &Parent, &mut Transform), With<Volumetric>>,
mut sounds: Query<(Entity, &Sound, &Parent, Option<&mut Transform>), With<Volumetric>>,
colliders: Query<(&Collider, &GlobalTransform)>,
) {
if let Ok((listener_collider, listener_global_transform)) = listener.get_single() {
for (sound, parent, mut sound_transform) in &mut sounds {
for (sound_entity, sound, parent, sound_transform) in &mut sounds {
if sound.paused {
continue;
}
if let Ok((sound_collider, sound_global_transform)) = colliders.get(**parent) {
let closest = listener_global_transform.closest_points(
listener_collider,
sound_global_transform,
sound_collider,
);
if let ClosestPoints::WithinMargin(_p1, p2) = closest {
let p2 = Vec3::new(p2.x, p2.y, 0.);
let Ok((sound_collider, sound_global_transform)) = colliders.get(**parent) else {
continue;
};
let closest = listener_global_transform.closest_points(
listener_collider,
sound_global_transform,
sound_collider,
);
if let ClosestPoints::WithinMargin(_p1, p2) = closest {
if let Some(mut sound_transform) = sound_transform {
sound_transform.translation.x = p2.x - sound_global_transform.translation().x;
sound_transform.translation.y = p2.y - sound_global_transform.translation().y;
} else if closest == ClosestPoints::Intersecting {
sound_transform.translation.x = listener_global_transform.translation().x
- sound_global_transform.translation().x;
sound_transform.translation.y = listener_global_transform.translation().y
- sound_global_transform.translation().y;
} else {
let sound_translation = Vec3::new(
p2.x - sound_global_transform.translation().x,
p2.y - sound_global_transform.translation().y,
0.,
);
commands
.entity(sound_entity)
.insert(TransformBundle::from_transform(
Transform::from_translation(sound_translation),
));
}
} else if closest == ClosestPoints::Intersecting && sound_transform.is_some() {
println!("Clearing volumetric");
commands
.entity(sound_entity)
.remove::<Transform>()
.remove::<GlobalTransform>();
}
}
}