send GameEvents instead of running redundand code

This commit is contained in:
yuni 2024-05-13 20:25:25 +02:00
parent eb681999f8
commit ac9451ea4a
3 changed files with 12 additions and 20 deletions

View file

@ -160,19 +160,17 @@ pub fn respawn_sinks(
pub fn handle_input( pub fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>, keyboard_input: Res<ButtonInput<KeyCode>>,
mut ew_toggle: EventWriter<ToggleMusicEvent>,
mut ew_sfx: EventWriter<PlaySfxEvent>, mut ew_sfx: EventWriter<PlaySfxEvent>,
mut settings: ResMut<var::Settings>, mut ew_game: EventWriter<GameEvent>,
settings: Res<var::Settings>,
) { ) {
if keyboard_input.just_pressed(settings.key_toggle_music) { if keyboard_input.just_pressed(settings.key_toggle_music) {
settings.mute_music ^= true;
ew_sfx.send(PlaySfxEvent(Sfx::Click)); ew_sfx.send(PlaySfxEvent(Sfx::Click));
ew_toggle.send(ToggleMusicEvent()); ew_game.send(GameEvent::SetMusic(Toggle));
} }
if keyboard_input.just_pressed(settings.key_toggle_sfx) { if keyboard_input.just_pressed(settings.key_toggle_sfx) {
settings.mute_sfx ^= true;
ew_sfx.send(PlaySfxEvent(Sfx::Click)); ew_sfx.send(PlaySfxEvent(Sfx::Click));
ew_toggle.send(ToggleMusicEvent()); ew_game.send(GameEvent::SetMusic(Toggle));
} }
} }

View file

@ -34,6 +34,7 @@ pub mod prelude {
pub use crate::var::Settings; pub use crate::var::Settings;
pub use crate::load::load_asset; pub use crate::load::load_asset;
pub use game::{GameEvent, Turn}; pub use game::{GameEvent, Turn};
pub use game::Turn::Toggle;
} }
use bevy::window::{Window, WindowMode, PrimaryWindow, CursorGrabMode}; use bevy::window::{Window, WindowMode, PrimaryWindow, CursorGrabMode};
@ -162,18 +163,11 @@ fn setup(
fn handle_input( fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>, keyboard_input: Res<ButtonInput<KeyCode>>,
settings: Res<var::Settings>, settings: Res<var::Settings>,
opt: Res<var::CommandLineOptions>,
mut windows: Query<&mut Window, With<PrimaryWindow>>,
mut ew_sfx: EventWriter<audio::PlaySfxEvent>, mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
mut ew_game: EventWriter<game::GameEvent>,
) { ) {
if keyboard_input.just_pressed(settings.key_fullscreen) { if keyboard_input.just_pressed(settings.key_fullscreen) {
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click)); ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click));
for mut window in &mut windows { ew_game.send(game::GameEvent::SetFullscreen(game::Turn::Toggle));
window.mode = if window.mode == WindowMode::Windowed {
opt.window_mode_fullscreen
} else {
WindowMode::Windowed
}
}
} }
} }

View file

@ -286,23 +286,23 @@ pub fn handle_input(
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click)); ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click));
match MENUDEF[menustate.cursor].1 { match MENUDEF[menustate.cursor].1 {
MenuAction::ToggleMap => { MenuAction::ToggleMap => {
ew_game.send(GameEvent::SetMap(Turn::Toggle)); ew_game.send(GameEvent::SetMap(Toggle));
settings.menu_active = false; settings.menu_active = false;
for mut vis in &mut q_vis { for mut vis in &mut q_vis {
*vis = Visibility::Hidden; *vis = Visibility::Hidden;
} }
}, },
MenuAction::ToggleAR => { MenuAction::ToggleAR => {
ew_game.send(GameEvent::SetAR(Turn::Toggle)); ew_game.send(GameEvent::SetAR(Toggle));
}, },
MenuAction::ToggleMusic => { MenuAction::ToggleMusic => {
ew_game.send(GameEvent::SetMusic(Turn::Toggle)); ew_game.send(GameEvent::SetMusic(Toggle));
}, },
MenuAction::ToggleSound => { MenuAction::ToggleSound => {
ew_game.send(GameEvent::SetSound(Turn::Toggle)); ew_game.send(GameEvent::SetSound(Toggle));
}, },
MenuAction::ToggleFullscreen => { MenuAction::ToggleFullscreen => {
ew_game.send(GameEvent::SetFullscreen(Turn::Toggle)); ew_game.send(GameEvent::SetFullscreen(Toggle));
}, },
MenuAction::Quit => { MenuAction::Quit => {
app_exit_events.send(bevy::app::AppExit); app_exit_events.send(bevy::app::AppExit);