Skip to content

Commit

Permalink
Pressure plate sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
xill47 committed Apr 9, 2023
1 parent b7b5ad8 commit 4f058a9
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 51 deletions.
92 changes: 62 additions & 30 deletions src/audio.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,87 @@
use bevy::prelude::*;
use bevy_ecs_ldtk::GridCoords;

use crate::{levels::tiles::Laser, loading::AudioAssets, player::Player, GameState};
use crate::{
levels::panel::PressurePlate, loading::AudioAssets, player::Player, ui::MuteControl, GameState,
};

pub struct InternalAudioPlugin;

impl Plugin for InternalAudioPlugin {
fn build(&self, app: &mut App) {
app.add_system(start_bgm.in_schedule(OnEnter(GameState::Playing)))
.add_systems((step_audio, laser_audio).in_set(OnUpdate(GameState::Playing)));
app.init_resource::<AudioConfig>()
.add_system(start_bgm.in_schedule(OnEnter(GameState::Playing)))
.add_systems(
(step_audio, mute_control, bgm_mute_control).in_set(OnUpdate(GameState::Playing)),
);
}
}

fn start_bgm(audio_assets: Res<AudioAssets>, audio: Res<Audio>) {
audio.play_with_settings(
audio_assets.bgm.clone_weak(),
PlaybackSettings::LOOP.with_volume(0.3),
);
#[derive(Resource, Default)]
pub struct AudioConfig {
mute: bool,
bgm_handle: Option<Handle<AudioSink>>,
}

fn step_audio(
player_q: Query<(), (Changed<GridCoords>, With<Player>)>,
fn start_bgm(
audio_assets: Res<AudioAssets>,
audio: Res<Audio>,
mut audio_config: ResMut<AudioConfig>,
audio_sinks: Res<Assets<AudioSink>>,
) {
if player_q.iter().next().is_some() {
audio.play(audio_assets.step.clone_weak());
if audio_config.bgm_handle.is_none() {
let weak_handle = audio.play_with_settings(
audio_assets.bgm.clone_weak(),
PlaybackSettings::LOOP.with_volume(0.3),
);
audio_config.bgm_handle = audio_sinks.get_handle(weak_handle).into();
}
}

fn bgm_mute_control(audio_config: Res<AudioConfig>, audio_sinks: Res<Assets<AudioSink>>) {
if audio_config.is_changed() {
info!("Bgm mute changed: {:?}", audio_config.mute);
if let Some(handle) = &audio_config.bgm_handle {
info!("Strong handle: {:?}", handle);
if let Some(sink) = audio_sinks.get(handle) {
if audio_config.mute {
info!("Pausing bgm");
sink.pause();
} else {
sink.play();
}
}
}
}
}

fn laser_audio(
laser_q: Query<(&Transform, &Laser), Changed<Laser>>,
player_q: Query<&Transform, With<Player>>,
fn step_audio(
player_q: Query<&GridCoords, (Changed<GridCoords>, With<Player>)>,
pressure_plate_q: Query<&GridCoords, With<PressurePlate>>,
audio_assets: Res<AudioAssets>,
audio: Res<Audio>,
audio_config: Res<AudioConfig>,
) {
let Ok(player_transform) = player_q.get_single() else { return; };
for (laser_transform, laser) in laser_q.iter() {
if !laser.is_open {
let distance = laser_transform
.translation
.distance(player_transform.translation);
info!("Playing laser sound at distance {}", distance);
if distance < 100.0 {
audio.play_spatial(
audio_assets.laser.clone_weak(),
*player_transform,
1.0,
laser_transform.translation,
);
}
if audio_config.mute {
return;
}
for coords in player_q.iter() {
if pressure_plate_q.iter().any(|l_coords| l_coords == coords) {
audio.play(audio_assets.switch.clone_weak());
}
audio.play(audio_assets.step.clone_weak());
}
}

fn mute_control(
mut audio_config: ResMut<AudioConfig>,
mute_control_q: Query<&Interaction, (With<MuteControl>, Changed<Interaction>)>,
) {
for interaction in mute_control_q.iter() {
info!("Mute control interaction: {:?}", interaction);
if *interaction == Interaction::Clicked {
audio_config.mute = !audio_config.mute;
info!("Mute control: {:?}", audio_config.mute);
}
}
}
6 changes: 3 additions & 3 deletions src/ui/buttons_styles.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use bevy::prelude::*;

pub const BUTTON_DEFAULT_BG_COLOR: Color = Color::rgb(0.15, 0.15, 0.15);
pub const BUTTON_HOVER_BG_COLOR: Color = Color::rgb(0.25, 0.25, 0.25);
pub const BUTTON_CLICK_BG_COLOR: Color = Color::rgb(0.35, 0.35, 0.35);
pub const BUTTON_DEFAULT_BG_COLOR: Color = Color::rgb(131. / 255., 117. / 255., 131. / 255.);
pub const BUTTON_HOVER_BG_COLOR: Color = Color::rgb(155. / 255., 141. / 255., 152. / 255.);
pub const BUTTON_CLICK_BG_COLOR: Color = Color::rgb(232. / 255., 219. / 255., 216. / 255.);

pub fn style_button_interactions(
mut button_query: Query<(&mut BackgroundColor, &Interaction), Changed<Interaction>>,
Expand Down
6 changes: 4 additions & 2 deletions src/ui/color_control_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use bevy::prelude::{ChildBuilder, *};

use crate::player::color_control::ColorControl;

use super::buttons_styles::BUTTON_DEFAULT_BG_COLOR;

#[derive(Component)]
pub struct ColorControlView;

Expand All @@ -15,15 +17,15 @@ pub fn add_color_control(parent: &mut ChildBuilder, text_style: &TextStyle) {
align_items: AlignItems::Center,
..default()
},
background_color: Color::rgb(0.15, 0.15, 0.15).into(),
background_color: BUTTON_DEFAULT_BG_COLOR.into(),
..default()
})
.insert(ColorControlView)
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"",
TextStyle {
color: Color::rgb(0.1, 0.9, 0.1),
color: Color::rgb_u8(21, 18, 23),
..text_style.clone()
},
));
Expand Down
46 changes: 33 additions & 13 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct RootUI;
pub fn spawn_game_ui(mut commands: Commands, font_assets: Res<FontAssets>) {
let text_style = TextStyle {
font: font_assets.fira_sans.clone_weak(),
color: Color::rgb(0.3, 0.3, 0.3),
color: Color::rgb_u8(21, 18, 23),
font_size: 24.,
};
commands
Expand Down Expand Up @@ -84,7 +84,7 @@ pub fn spawn_game_ui(mut commands: Commands, font_assets: Res<FontAssets>) {
flex_direction: FlexDirection::Row,
..default()
},
background_color: BackgroundColor(Color::rgba(0.1, 0.1, 0.1, 0.5)),
background_color: BackgroundColor(Color::rgba(0.0, 0.0, 0.0, 0.5)),
..default()
})
.with_children(|parent| {
Expand All @@ -104,6 +104,7 @@ pub fn spawn_game_ui(mut commands: Commands, font_assets: Res<FontAssets>) {
{
add_debug_button(parent, &text_style);
}
add_mute_button(parent, &text_style);
});
add_notifications_ui(parent, &text_style);
let wasd_size = 42.;
Expand All @@ -129,17 +130,11 @@ pub fn spawn_game_ui(mut commands: Commands, font_assets: Res<FontAssets>) {
flex_direction: FlexDirection::Column,
..default()
},
background_color: Color::rgb(0.25, 0.15, 0.15).into(),
background_color: BackgroundColor(Color::rgba(0.0, 0.0, 0.0, 0.5)),
..default()
})
.with_children(|parent| {
add_wasd(
parent,
&wasd_node_style,
&text_style,
"W",
MovementDirection::Up,
);
add_wasd(parent, &wasd_node_style, &text_style, MovementDirection::Up);
parent
.spawn(NodeBundle {
style: Style {
Expand All @@ -160,21 +155,18 @@ pub fn spawn_game_ui(mut commands: Commands, font_assets: Res<FontAssets>) {
parent,
&wasd_node_style,
&text_style,
"A",
MovementDirection::Left,
);
add_wasd(
parent,
&wasd_node_style,
&text_style,
"S",
MovementDirection::Down,
);
add_wasd(
parent,
&wasd_node_style,
&text_style,
"D",
MovementDirection::Right,
);
});
Expand All @@ -183,3 +175,31 @@ pub fn spawn_game_ui(mut commands: Commands, font_assets: Res<FontAssets>) {
})
.insert(RootUI);
}

#[derive(Component)]
pub struct MuteControl;

fn add_mute_button(parent: &mut ChildBuilder, text_style: &TextStyle) {
parent
.spawn(ButtonBundle {
style: Style {
size: Size::new(Val::Px(120.), Val::Px(50.)),
margin: UiRect::all(Val::Auto),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
background_color: Color::rgb(0.15, 0.15, 0.15).into(),
..default()
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Mute",
TextStyle {
color: Color::rgb_u8(21, 18, 23),
..text_style.clone()
},
));
})
.insert(MuteControl);
}
5 changes: 2 additions & 3 deletions src/ui/wasd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ pub fn add_wasd(
parent: &mut ChildBuilder,
node_style: &Style,
text_style: &TextStyle,
str: &str,
movement: MovementDirection,
) {
let wasd = Wasd::from(movement);
Expand All @@ -58,9 +57,9 @@ pub fn add_wasd(
background_color: wasd_bg_color.into(),
..default()
})
.insert(wasd)
.insert(wasd.clone())
.with_children(|parent| {
parent.spawn(TextBundle::from_section(str, text_style.clone()));
parent.spawn(TextBundle::from_section(wasd.text(), text_style.clone()));
});
}

Expand Down

0 comments on commit 4f058a9

Please sign in to comment.