@@ -9,9 +9,7 @@ use crate::{
99 AppState , AudioVolume , MusicTrack , TimeScale ,
1010} ;
1111use 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 } ;
1513use itertools:: Itertools ;
1614use 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+
9192struct ScoreAudioChannel ;
9293
9394#[ derive( Clone , Copy ) ]
@@ -852,13 +853,12 @@ fn score_system(
852853
853854#[ allow( clippy:: too_many_arguments) ]
854855fn 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
941932fn 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