Skip to content

Commit

Permalink
Load upgrade sfx into a Vec
Browse files Browse the repository at this point in the history
  • Loading branch information
necrashter committed Dec 9, 2023
1 parent a58ac5b commit 38ee326
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use bevy::prelude::*;
use bevy_asset_loader::prelude::*;
use bevy_kira_audio::prelude::*;
use bevy_kira_audio::AudioPlugin as KiraAudioPlugin;
use rand::seq::SliceRandom;
use rand::thread_rng;
use rand::Rng;

pub struct AudioPlugin;

Expand All @@ -20,23 +20,18 @@ impl Plugin for AudioPlugin {
#[derive(AssetCollection, Resource, Reflect, Default)]
#[reflect(Resource)]
pub struct AudioAssets {
#[asset(path = "audio/upgrade0.ogg")]
pub upgrade0: Handle<AudioSource>,
#[asset(path = "audio/upgrade1.ogg")]
pub upgrade1: Handle<AudioSource>,
#[asset(paths("audio/upgrade0.ogg", "audio/upgrade1.ogg"), collection(typed))]
pub upgrade_sounds: Vec<Handle<AudioSource>>,
#[asset(path = "music/ingame.ogg")]
pub music: Handle<AudioSource>,
}

impl AudioAssets {
pub fn random_upgrade(&self) -> Handle<AudioSource> {
let rng = &mut thread_rng();
let rand: f32 = rng.gen_range(0.0..1.0);
if rand < 0.5 {
self.upgrade0.clone()
} else {
self.upgrade1.clone()
}
self.upgrade_sounds
.choose(&mut thread_rng())
.unwrap()
.clone()
}
}

Expand Down

0 comments on commit 38ee326

Please sign in to comment.