Skip to content

Commit 7d3abaf

Browse files
committed
Add UI interaction sfx
1 parent f57f0cf commit 7d3abaf

File tree

10 files changed

+60
-0
lines changed

10 files changed

+60
-0
lines changed

assets/audio/sfx/UI Click.ogg

7.51 KB
Binary file not shown.

assets/audio/sfx/UI Click.wav

88 KB
Binary file not shown.

assets/audio/sfx/UI Hover.ogg

8.95 KB
Binary file not shown.

assets/audio/sfx/UI Hover.wav

87.1 KB
Binary file not shown.

src/game/card.rs

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl EntityCommand for CardBackground {
126126
pressed: atlas_on,
127127
disabled: atlas_off,
128128
},
129+
InteractionSfx,
129130
));
130131
}
131132
}

src/screen/playing.rs

+4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ pub struct PlayingAssets {
8383
pub sfx_level_up: Handle<AudioSource>,
8484
#[asset(path = "audio/sfx/Projectile Hits Player-02.ogg")]
8585
pub sfx_hurt: Handle<AudioSource>,
86+
#[asset(path = "audio/sfx/UI Hover.ogg")]
87+
pub sfx_ui_click: Handle<AudioSource>,
88+
#[asset(path = "audio/sfx/UI Click.ogg")]
89+
pub sfx_ui_hover: Handle<AudioSource>,
8690
}
8791

8892
impl Configure for PlayingAssets {

src/screen/playing/level_up_menu.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use bevy::ecs::system::EntityCommand;
22
use bevy::ecs::system::SystemState;
33
use bevy::prelude::*;
4+
use bevy_kira_audio::prelude::*;
45
use bevy_mod_picking::prelude::*;
56
use leafwing_input_manager::common_conditions::action_just_pressed;
67
use leafwing_input_manager::prelude::*;
@@ -15,6 +16,7 @@ use crate::game::card::card;
1516
use crate::game::card::deck::Deck;
1617
use crate::game::card::deck::IsDeckDisplay;
1718
use crate::game::card::CardConfig;
19+
use crate::screen::playing::PlayingAssets;
1820
use crate::screen::playing::PlayingMenu;
1921
use crate::ui::prelude::*;
2022
use crate::util::prelude::*;
@@ -383,29 +385,38 @@ fn card_select_right(
383385
}
384386

385387
fn card_swap_left(
388+
audio: Res<Audio>,
389+
assets: Res<PlayingAssets>,
386390
deck_display_query: Query<&Selection, With<IsDeckDisplay>>,
387391
mut deck_query: Query<&mut Deck>,
388392
) {
393+
audio.play(assets.sfx_ui_hover.clone());
389394
for selection in &deck_display_query {
390395
let mut deck = c!(deck_query.get_mut(selection.0));
391396
deck.swap(-1);
392397
}
393398
}
394399

395400
fn card_swap_right(
401+
audio: Res<Audio>,
402+
assets: Res<PlayingAssets>,
396403
deck_display_query: Query<&Selection, With<IsDeckDisplay>>,
397404
mut deck_query: Query<&mut Deck>,
398405
) {
406+
audio.play(assets.sfx_ui_hover.clone());
399407
for selection in &deck_display_query {
400408
let mut deck = c!(deck_query.get_mut(selection.0));
401409
deck.swap(1);
402410
}
403411
}
404412

405413
fn card_discard(
414+
audio: Res<Audio>,
415+
assets: Res<PlayingAssets>,
406416
deck_display_query: Query<&Selection, With<IsDeckDisplay>>,
407417
mut deck_query: Query<&mut Deck>,
408418
) {
419+
audio.play(assets.sfx_ui_click.clone());
409420
for selection in &deck_display_query {
410421
let mut deck = c!(deck_query.get_mut(selection.0));
411422
deck.discard();

src/ui.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod prelude {
1818
pub use super::font::BOLD_FONT_HANDLE;
1919
pub use super::font::FONT_HANDLE;
2020
pub use super::font::THICK_FONT_HANDLE;
21+
pub use super::interaction::InteractionSfx;
2122
pub use super::interaction::InteractionTable;
2223
pub use super::interaction::IsDisabled;
2324
pub use super::tooltip::Tooltip;

src/ui/interaction.rs

+42
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use bevy::prelude::*;
22
use bevy::reflect::GetTypeRegistration;
3+
use bevy_kira_audio::prelude::*;
34
use bevy_mod_picking::prelude::*;
45

56
use crate::animation::offset::Offset;
67
use crate::core::UpdateSet;
8+
use crate::screen::playing::PlayingAssets;
79
use crate::ui::prelude::*;
810
use crate::util::prelude::*;
911

@@ -15,6 +17,7 @@ pub(super) fn plugin(app: &mut App) {
1517
InteractionTable<ThemeColorFor<BackgroundColor>>,
1618
InteractionTable<TextureAtlas>,
1719
InteractionTable<Offset>,
20+
InteractionSfx,
1821
)>();
1922
}
2023

@@ -76,3 +79,42 @@ fn apply_interaction_table<C: Component + Clone>(
7679
.clone();
7780
}
7881
}
82+
83+
#[derive(Component, Reflect)]
84+
#[reflect(Component)]
85+
pub struct InteractionSfx;
86+
87+
impl Configure for InteractionSfx {
88+
fn configure(app: &mut App) {
89+
app.register_type::<Self>();
90+
app.add_systems(Update, play_interaction_sfx.in_set(UpdateSet::RecordInput));
91+
}
92+
}
93+
94+
fn play_interaction_sfx(
95+
assets: Res<PlayingAssets>,
96+
audio: Res<Audio>,
97+
interaction_query: Query<
98+
(Option<&IsDisabled>, &Interaction),
99+
(
100+
With<InteractionSfx>,
101+
Or<(Changed<Interaction>, Changed<IsDisabled>)>,
102+
),
103+
>,
104+
) {
105+
for (is_disabled, interaction) in &interaction_query {
106+
if matches!(is_disabled, Some(IsDisabled(true))) {
107+
continue;
108+
}
109+
110+
match interaction {
111+
Interaction::Hovered => {
112+
audio.play(assets.sfx_ui_hover.clone()).with_volume(0.6);
113+
},
114+
Interaction::Pressed => {
115+
audio.play(assets.sfx_ui_click.clone()).with_volume(0.6);
116+
},
117+
_ => (),
118+
}
119+
}
120+
}

src/ui/widget.rs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub fn menu_button(text: impl Into<String>) -> impl EntityCommand<World> {
5454
pressed: Offset(Vec2::new(0.0, 2.0)),
5555
..default()
5656
},
57+
InteractionSfx,
5758
))
5859
.with_children(|parent| {
5960
parent.spawn((

0 commit comments

Comments
 (0)