add sound effects

This commit is contained in:
Jim 2025-09-02 21:13:21 -04:00
parent 86f2345667
commit 587b902e79
Signed by: jim
GPG key ID: 3D7D94BA53088BF4
39 changed files with 234 additions and 40 deletions

View file

@ -6,6 +6,7 @@ edition = "2024"
[dependencies]
bevy = { workspace = true }
bevy_egui = { workspace = true }
rand = { workspace = true }
[lints]
workspace = true

View file

@ -4,11 +4,48 @@
use bevy::prelude::*;
use bevy_egui::EguiPlugin;
use rand::{Rng, thread_rng};
pub struct UiPlugin;
impl Plugin for UiPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(EguiPlugin::default());
app.add_plugins(EguiPlugin::default())
.add_event::<PlayClickSound>()
.add_systems(Update, play_click_sound)
.add_systems(Startup, init);
}
}
fn init(assets: Res<AssetServer>, mut commands: Commands) {
commands.insert_resource(UIResources {
click_audio: assets.load("sounds/click1.ogg"),
});
}
#[derive(Resource)]
struct UIResources {
click_audio: Handle<AudioSource>,
}
#[derive(Event)]
pub struct PlayClickSound;
fn play_click_sound(
mut reader: EventReader<PlayClickSound>,
mut commands: Commands,
res: Res<UIResources>,
) {
if reader.is_empty() {
return;
}
reader.clear();
commands.spawn((
AudioPlayer(res.click_audio.clone()),
PlaybackSettings {
mode: bevy::audio::PlaybackMode::Despawn,
speed: thread_rng().gen_range(0.9..1.1),
..default()
},
));
}