Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Velocity optional #53

Merged
merged 10 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions src/components/audio_listener.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
use bevy::math::Vec3;
use bevy::prelude::{Component, GlobalTransform, Query, Res, With};

use crate::attributes_3d::attributes3d;
use crate::components::velocity::Velocity;
use crate::fmod_studio::FmodStudio;

/// See the [`Velocity`] component for information on enabling the Doppler effect.
#[derive(Component, Default)]
pub struct AudioListener;

impl AudioListener {
pub(crate) fn update_3d_attributes(
query: Query<(&Velocity, &GlobalTransform), With<AudioListener>>,
query: Query<(&GlobalTransform, Option<&Velocity>), With<AudioListener>>,
studio: Res<FmodStudio>,
) {
match query.get_single() {
Ok((velocity, transform)) => {
studio
.0
.set_listener_attributes(
0,
attributes3d(
transform.translation(),
velocity.current_velocity,
transform.forward(),
transform.up(),
),
None,
)
.unwrap();
if let Ok((transform, vel_component)) = query.get_single() {
let mut velocity = Vec3::ZERO;

if let Some(vel_component) = vel_component {
velocity = vel_component.current_velocity;
}
_ => {}

studio
.0
.set_listener_attributes(
0,
attributes3d(
transform.translation(),
velocity,
transform.forward(),
transform.up(),
),
None,
)
.unwrap();
}
}
}
14 changes: 11 additions & 3 deletions src/components/audio_source.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use bevy::math::Vec3;
use bevy::prelude::{AudioSinkPlayback, Component, GlobalTransform, Query};
use libfmod::StopMode::Immediate;
use libfmod::{EventDescription, EventInstance, StopMode};

use crate::attributes_3d::attributes3d;
use crate::components::velocity::Velocity;

/// See the [`Velocity`] component for information on enabling the Doppler effect.
#[derive(Component)]
pub struct AudioSource {
pub event_instance: EventInstance,
Expand All @@ -18,16 +20,22 @@ impl AudioSource {
}

pub(crate) fn update_3d_attributes(
mut query: Query<(&AudioSource, &Velocity, &GlobalTransform)>,
mut query: Query<(&AudioSource, &GlobalTransform, Option<&Velocity>)>,
) {
query
.iter_mut()
.for_each(|(audio_source, velocity, transform)| {
.for_each(|(audio_source, transform, vel_component)| {
let mut velocity = Vec3::ZERO;

if let Some(vel_component) = vel_component {
velocity = vel_component.current_velocity;
}

audio_source
.event_instance
.set_3d_attributes(attributes3d(
transform.translation(),
velocity.current_velocity,
velocity,
transform.forward(),
transform.up(),
))
Expand Down
12 changes: 1 addition & 11 deletions src/components/bundles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,9 @@ impl SpatialAudioBundle {
}
}

#[derive(Bundle)]
#[derive(Bundle, Default)]
pub struct SpatialListenerBundle {
audio_listener: AudioListener,
velocity: Velocity,
transform: Transform,
}

impl Default for SpatialListenerBundle {
fn default() -> Self {
SpatialListenerBundle {
audio_listener: AudioListener::default(),
velocity: Velocity::default(),
transform: Transform::default(),
}
}
}
5 changes: 5 additions & 0 deletions src/components/velocity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ use bevy::app::{App, Plugin, Update};
use bevy::math::Vec3;
use bevy::prelude::{Component, FixedTime, GlobalTransform, Query, Res};

/// Automatic velocity updates for [`AudioListener`] and [`AudioSource`]
///
/// Make sure to add this component to your listener and source entities in order
/// to enable the Doppler effect. The recommended way to do this is to use the [`SpatialAudioBundle`]
/// and [`SpatialListenerBundle`].
#[derive(Component, Default)]
pub struct Velocity {
last_position: Vec3,
Expand Down