Skip to content

Commit fa14dba

Browse files
committed
Use instanced audio control.
1 parent 3d51fd4 commit fa14dba

File tree

4 files changed

+54
-68
lines changed

4 files changed

+54
-68
lines changed

Cargo.lock

Lines changed: 33 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bounce-up"
3-
version = "1.4.3"
3+
version = "1.4.4"
44
edition = "2021"
55

66
[lib]
@@ -23,7 +23,7 @@ bevy = { version = "0.8", default-features = false, features = [
2323
"png",
2424
"x11",
2525
] }
26-
bevy_kira_audio = { git = "https://github.com/NiklasEi/bevy_kira_audio", branch = "main", features = [
26+
bevy_kira_audio = { version = "0.12", features = [
2727
"wav",
2828
"flac",
2929
"ogg"

src/config.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ pub const IMPACT_AUDIOS: [&str; 2] = ["audios/impacts/impact-1.ogg", "audios/imp
4545
pub const BUTTON_HOVER_AUDIO: &str = "audios/button/hover.ogg";
4646
pub const BUTTON_CLICK_AUDIO: &str = "audios/button/click.ogg";
4747

48-
pub const AUDIO_CHANNEL_COUNT: usize = 16;
49-
5048
pub const MENU_MUSIC: &str = "musics/E2M2 Myrgharok - Halls of Wandering Spirits.ogg";
5149
pub const GAME_MUSIC: &str = "musics/E3M8 Myrgharok - Mother of All Doom.ogg";
5250

src/game/mod.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ use crate::{
99
AppState, AudioVolume, MusicTrack, TimeScale,
1010
};
1111
use bevy::{prelude::*, sprite::MaterialMesh2dBundle, time::FixedTimestep};
12-
use bevy_kira_audio::{
13-
Audio, AudioApp, AudioChannel, AudioControl, AudioSource, DynamicAudioChannels,
14-
};
12+
use bevy_kira_audio::{Audio, AudioApp, AudioChannel, AudioControl, AudioSource};
1513
use itertools::Itertools;
1614
use std::f32::consts::FRAC_PI_4;
1715

@@ -45,6 +43,7 @@ impl Plugin for GamePlugin {
4543
miss: Timer::from_seconds(0.5, false),
4644
})
4745
.init_resource::<Slits>()
46+
.add_audio_channel::<BounceAudioChannel>()
4847
.add_audio_channel::<ScoreAudioChannel>()
4948
.add_startup_system(setup_game)
5049
.add_system_set(
@@ -88,6 +87,8 @@ impl Plugin for GamePlugin {
8887
}
8988
}
9089

90+
struct BounceAudioChannel;
91+
9192
struct ScoreAudioChannel;
9293

9394
#[derive(Clone, Copy)]
@@ -852,13 +853,12 @@ fn score_system(
852853

853854
#[allow(clippy::too_many_arguments)]
854855
fn bounce_audio(
856+
audio: Res<AudioChannel<BounceAudioChannel>>,
855857
audios: Res<Audios>,
856-
mut audio: ResMut<DynamicAudioChannels>,
857858
volume: Res<AudioVolume>,
858859
time: Res<Time>,
859860
mut timer: ResMut<Debounce>,
860861
mut events: EventReader<CollisionEvent>,
861-
mut index: Local<usize>,
862862
mut bounce_entities: Local<Option<[Entity; 2]>>,
863863
query: Query<(Entity, &BounceAudio)>,
864864
balls: Query<(), With<Ball>>,
@@ -868,10 +868,6 @@ fn bounce_audio(
868868
timer.audio_bounce_short.tick(time.delta());
869869
timer.audio_hit.tick(time.delta());
870870

871-
let channels = (0..AUDIO_CHANNEL_COUNT)
872-
.map(|index| format!("impact_{}", index))
873-
.collect_vec();
874-
875871
for event in events.iter() {
876872
// one of the entities must be a ball
877873
let results = event.entities.map(|entity| balls.get(entity).is_ok());
@@ -907,9 +903,6 @@ fn bounce_audio(
907903
*bounce_entities = entities;
908904
}
909905

910-
*index = (*index + 1) % AUDIO_CHANNEL_COUNT;
911-
let channel = audio.create_channel(&channels[*index]);
912-
913906
if can_play_audio {
914907
let velocities = motions
915908
.many(event.entities)
@@ -921,15 +914,13 @@ fn bounce_audio(
921914
.clamp(0.0, 1.0);
922915

923916
let panning = event.hit.location().x / ARENA_WIDTH + 0.5;
924-
channel.set_panning(panning.into());
925-
926917
let volume = volume.effects * (0.5 * normalized_speed + 0.5);
927-
channel.set_volume(volume.into());
928-
929918
let playback_rate = 0.4 * fastrand::f32() + 0.8;
930-
channel.set_playback_rate(playback_rate.into());
931-
932-
channel.play(audio_source);
919+
audio
920+
.play(audio_source)
921+
.with_volume(volume.into())
922+
.with_panning(panning.into())
923+
.with_playback_rate(playback_rate.into());
933924

934925
timer.audio_bounce_long.reset();
935926
timer.audio_bounce_short.reset();
@@ -939,30 +930,25 @@ fn bounce_audio(
939930
}
940931

941932
fn score_audio(
942-
audios: Res<Audios>,
943933
audio: Res<AudioChannel<ScoreAudioChannel>>,
934+
audios: Res<Audios>,
944935
volume: Res<AudioVolume>,
945936
mut player_miss_events: EventReader<PlayerMissEvent>,
946937
mut game_over_events: EventReader<GameOverEvent>,
947938
) {
948939
for event in player_miss_events.iter() {
949-
// let channel = &AudioChannel::new("miss".into());
950940
let panning = event.location.x / ARENA_WIDTH + 0.5;
951-
// audio.set_volume_in_channel(volume.effects, channel);
952-
// audio.set_panning_in_channel(panning, channel);
953-
// audio.play_in_channel(audios.miss_audio.clone(), channel);
954-
audio.set_volume(volume.effects.into());
955-
audio.set_panning(panning.into());
956-
audio.play(audios.miss_audio.clone());
941+
audio
942+
.play(audios.miss_audio.clone())
943+
.with_volume(volume.effects.into())
944+
.with_panning(panning.into());
957945
}
958946

959947
for event in game_over_events.iter() {
960-
// let channel = &AudioChannel::new("over".into());
961-
// audio.set_volume_in_channel(volume.effects, channel);
962-
audio.set_volume(volume.effects.into());
963-
match event {
964-
GameOverEvent::Win => audio.play(audios.explosion_audio.clone()),
965-
GameOverEvent::Lose => audio.play(audios.lose_audio.clone()),
948+
let audio_source = match event {
949+
GameOverEvent::Win => audios.explosion_audio.clone(),
950+
GameOverEvent::Lose => audios.lose_audio.clone(),
966951
};
952+
audio.play(audio_source).with_volume(volume.effects.into());
967953
}
968954
}

0 commit comments

Comments
 (0)