Skip to content

Commit

Permalink
fix spatial audio examples
Browse files Browse the repository at this point in the history
  • Loading branch information
awtterpip committed Oct 12, 2024
1 parent 992d17b commit 8914299
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
15 changes: 11 additions & 4 deletions examples/audio/spatial_audio_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bevy::{
audio::{AudioPlugin, SpatialScale},
color::palettes::css::*,
prelude::*,
time::Stopwatch,
};

/// Spatial audio uses the distance to attenuate the sound volume. In 2D with the default camera,
Expand Down Expand Up @@ -75,7 +76,7 @@ fn setup(

#[derive(Component, Default)]
struct Emitter {
stopped: bool,
stopwatch: Stopwatch,
}

fn update_emitters(
Expand All @@ -85,11 +86,17 @@ fn update_emitters(
) {
for (mut emitter_transform, mut emitter) in emitters.iter_mut() {
if keyboard.just_pressed(KeyCode::Space) {
emitter.stopped = !emitter.stopped;
if emitter.stopwatch.paused() {
emitter.stopwatch.unpause();
} else {
emitter.stopwatch.pause();
}
}

if !emitter.stopped {
emitter_transform.translation.x = ops::sin(time.elapsed_seconds()) * 500.0;
emitter.stopwatch.tick(time.delta());

if !emitter.stopwatch.paused() {
emitter_transform.translation.x = ops::sin(emitter.stopwatch.elapsed_secs()) * 500.0;
}
}
}
Expand Down
17 changes: 12 additions & 5 deletions examples/audio/spatial_audio_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use bevy::{
color::palettes::basic::{BLUE, LIME, RED},
prelude::*,
time::Stopwatch,
};

fn main() {
Expand Down Expand Up @@ -77,7 +78,7 @@ fn setup(

#[derive(Component, Default)]
struct Emitter {
stopped: bool,
stopwatch: Stopwatch,
}

fn update_positions(
Expand All @@ -87,12 +88,18 @@ fn update_positions(
) {
for (mut emitter_transform, mut emitter) in emitters.iter_mut() {
if keyboard.just_pressed(KeyCode::Space) {
emitter.stopped = !emitter.stopped;
if emitter.stopwatch.paused() {
emitter.stopwatch.unpause();
} else {
emitter.stopwatch.pause();
}
}

if !emitter.stopped {
emitter_transform.translation.x = ops::sin(time.elapsed_seconds()) * 3.0;
emitter_transform.translation.z = ops::cos(time.elapsed_seconds()) * 3.0;
emitter.stopwatch.tick(time.delta());

if !emitter.stopwatch.paused() {
emitter_transform.translation.x = ops::sin(emitter.stopwatch.elapsed_secs()) * 3.0;
emitter_transform.translation.z = ops::cos(emitter.stopwatch.elapsed_secs()) * 3.0;
}
}
}
Expand Down

0 comments on commit 8914299

Please sign in to comment.