Skip to content

Commit

Permalink
Use instanced audio control.
Browse files Browse the repository at this point in the history
  • Loading branch information
cryscan committed Aug 20, 2022
1 parent 3d51fd4 commit fa14dba
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 68 deletions.
64 changes: 33 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bounce-up"
version = "1.4.3"
version = "1.4.4"
edition = "2021"

[lib]
Expand All @@ -23,7 +23,7 @@ bevy = { version = "0.8", default-features = false, features = [
"png",
"x11",
] }
bevy_kira_audio = { git = "https://github.com/NiklasEi/bevy_kira_audio", branch = "main", features = [
bevy_kira_audio = { version = "0.12", features = [
"wav",
"flac",
"ogg"
Expand Down
2 changes: 0 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ pub const IMPACT_AUDIOS: [&str; 2] = ["audios/impacts/impact-1.ogg", "audios/imp
pub const BUTTON_HOVER_AUDIO: &str = "audios/button/hover.ogg";
pub const BUTTON_CLICK_AUDIO: &str = "audios/button/click.ogg";

pub const AUDIO_CHANNEL_COUNT: usize = 16;

pub const MENU_MUSIC: &str = "musics/E2M2 Myrgharok - Halls of Wandering Spirits.ogg";
pub const GAME_MUSIC: &str = "musics/E3M8 Myrgharok - Mother of All Doom.ogg";

Expand Down
52 changes: 19 additions & 33 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use crate::{
AppState, AudioVolume, MusicTrack, TimeScale,
};
use bevy::{prelude::*, sprite::MaterialMesh2dBundle, time::FixedTimestep};
use bevy_kira_audio::{
Audio, AudioApp, AudioChannel, AudioControl, AudioSource, DynamicAudioChannels,
};
use bevy_kira_audio::{Audio, AudioApp, AudioChannel, AudioControl, AudioSource};
use itertools::Itertools;
use std::f32::consts::FRAC_PI_4;

Expand Down Expand Up @@ -45,6 +43,7 @@ impl Plugin for GamePlugin {
miss: Timer::from_seconds(0.5, false),
})
.init_resource::<Slits>()
.add_audio_channel::<BounceAudioChannel>()
.add_audio_channel::<ScoreAudioChannel>()
.add_startup_system(setup_game)
.add_system_set(
Expand Down Expand Up @@ -88,6 +87,8 @@ impl Plugin for GamePlugin {
}
}

struct BounceAudioChannel;

struct ScoreAudioChannel;

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -852,13 +853,12 @@ fn score_system(

#[allow(clippy::too_many_arguments)]
fn bounce_audio(
audio: Res<AudioChannel<BounceAudioChannel>>,
audios: Res<Audios>,
mut audio: ResMut<DynamicAudioChannels>,
volume: Res<AudioVolume>,
time: Res<Time>,
mut timer: ResMut<Debounce>,
mut events: EventReader<CollisionEvent>,
mut index: Local<usize>,
mut bounce_entities: Local<Option<[Entity; 2]>>,
query: Query<(Entity, &BounceAudio)>,
balls: Query<(), With<Ball>>,
Expand All @@ -868,10 +868,6 @@ fn bounce_audio(
timer.audio_bounce_short.tick(time.delta());
timer.audio_hit.tick(time.delta());

let channels = (0..AUDIO_CHANNEL_COUNT)
.map(|index| format!("impact_{}", index))
.collect_vec();

for event in events.iter() {
// one of the entities must be a ball
let results = event.entities.map(|entity| balls.get(entity).is_ok());
Expand Down Expand Up @@ -907,9 +903,6 @@ fn bounce_audio(
*bounce_entities = entities;
}

*index = (*index + 1) % AUDIO_CHANNEL_COUNT;
let channel = audio.create_channel(&channels[*index]);

if can_play_audio {
let velocities = motions
.many(event.entities)
Expand All @@ -921,15 +914,13 @@ fn bounce_audio(
.clamp(0.0, 1.0);

let panning = event.hit.location().x / ARENA_WIDTH + 0.5;
channel.set_panning(panning.into());

let volume = volume.effects * (0.5 * normalized_speed + 0.5);
channel.set_volume(volume.into());

let playback_rate = 0.4 * fastrand::f32() + 0.8;
channel.set_playback_rate(playback_rate.into());

channel.play(audio_source);
audio
.play(audio_source)
.with_volume(volume.into())
.with_panning(panning.into())
.with_playback_rate(playback_rate.into());

timer.audio_bounce_long.reset();
timer.audio_bounce_short.reset();
Expand All @@ -939,30 +930,25 @@ fn bounce_audio(
}

fn score_audio(
audios: Res<Audios>,
audio: Res<AudioChannel<ScoreAudioChannel>>,
audios: Res<Audios>,
volume: Res<AudioVolume>,
mut player_miss_events: EventReader<PlayerMissEvent>,
mut game_over_events: EventReader<GameOverEvent>,
) {
for event in player_miss_events.iter() {
// let channel = &AudioChannel::new("miss".into());
let panning = event.location.x / ARENA_WIDTH + 0.5;
// audio.set_volume_in_channel(volume.effects, channel);
// audio.set_panning_in_channel(panning, channel);
// audio.play_in_channel(audios.miss_audio.clone(), channel);
audio.set_volume(volume.effects.into());
audio.set_panning(panning.into());
audio.play(audios.miss_audio.clone());
audio
.play(audios.miss_audio.clone())
.with_volume(volume.effects.into())
.with_panning(panning.into());
}

for event in game_over_events.iter() {
// let channel = &AudioChannel::new("over".into());
// audio.set_volume_in_channel(volume.effects, channel);
audio.set_volume(volume.effects.into());
match event {
GameOverEvent::Win => audio.play(audios.explosion_audio.clone()),
GameOverEvent::Lose => audio.play(audios.lose_audio.clone()),
let audio_source = match event {
GameOverEvent::Win => audios.explosion_audio.clone(),
GameOverEvent::Lose => audios.lose_audio.clone(),
};
audio.play(audio_source).with_volume(volume.effects.into());
}
}

0 comments on commit fa14dba

Please sign in to comment.