change menu labels to reflect settings

This commit is contained in:
yuni 2024-05-13 21:37:57 +02:00
parent 83fe739e91
commit f476f351c9
2 changed files with 52 additions and 12 deletions

View file

@ -69,9 +69,9 @@ fn handle_game_event(
mut er_game: EventReader<GameEvent>, mut er_game: EventReader<GameEvent>,
mut ew_sfx: EventWriter<audio::PlaySfxEvent>, mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
mut ew_updateoverlays: EventWriter<hud::UpdateOverlayVisibility>, mut ew_updateoverlays: EventWriter<hud::UpdateOverlayVisibility>,
mut ew_updatemenu: EventWriter<menu::UpdateMenuEvent>,
mut ew_togglemusic: EventWriter<audio::ToggleMusicEvent>, mut ew_togglemusic: EventWriter<audio::ToggleMusicEvent>,
mut q_window: Query<&mut Window, With<PrimaryWindow>>, mut q_window: Query<&mut Window, With<PrimaryWindow>>,
mut q_menu_vis: Query<&mut Visibility, With<menu::MenuElement>>,
mut q_light: Query<&mut DirectionalLight>, mut q_light: Query<&mut DirectionalLight>,
mut mapcam: ResMut<camera::MapCam>, mut mapcam: ResMut<camera::MapCam>,
opt: Res<var::CommandLineOptions>, opt: Res<var::CommandLineOptions>,
@ -112,9 +112,7 @@ fn handle_game_event(
} }
GameEvent::SetMenu(turn) => { GameEvent::SetMenu(turn) => {
settings.menu_active = turn.to_bool(settings.menu_active); settings.menu_active = turn.to_bool(settings.menu_active);
for mut vis in &mut q_menu_vis { ew_updatemenu.send(menu::UpdateMenuEvent);
*vis = bool2vis(settings.menu_active);
}
} }
GameEvent::SetThirdPerson(turn) => { GameEvent::SetThirdPerson(turn) => {
settings.third_person = turn.to_bool(settings.third_person); settings.third_person = turn.to_bool(settings.third_person);

View file

@ -22,12 +22,13 @@ impl Plugin for MenuPlugin {
app.add_systems(Startup, setup.after(hud::setup)); app.add_systems(Startup, setup.after(hud::setup));
app.add_systems(PreUpdate, show_deathscreen.run_if(on_event::<DeathScreenEvent>())); app.add_systems(PreUpdate, show_deathscreen.run_if(on_event::<DeathScreenEvent>()));
app.add_systems(Update, handle_deathscreen_input); app.add_systems(Update, handle_deathscreen_input);
app.add_systems(PostUpdate, update_menu); app.add_systems(PostUpdate, update_menu.run_if(on_event::<UpdateMenuEvent>()));
app.add_systems(Update, handle_input.run_if(alive)); app.add_systems(Update, handle_input.run_if(alive));
app.insert_resource(DeathScreenInputDelayTimer( app.insert_resource(DeathScreenInputDelayTimer(
Timer::from_seconds(1.0, TimerMode::Once))); Timer::from_seconds(1.0, TimerMode::Once)));
app.insert_resource(MenuState::default()); app.insert_resource(MenuState::default());
app.add_event::<DeathScreenEvent>(); app.add_event::<DeathScreenEvent>();
app.add_event::<UpdateMenuEvent>();
} }
} }
@ -36,15 +37,16 @@ impl Plugin for MenuPlugin {
#[derive(Component)] pub struct MenuTopLevel; #[derive(Component)] pub struct MenuTopLevel;
#[derive(Component)] pub struct DeathScreenElement; #[derive(Component)] pub struct DeathScreenElement;
#[derive(Component)] pub struct DeathText; #[derive(Component)] pub struct DeathText;
#[derive(Event)] pub struct UpdateMenuEvent;
#[derive(Event, PartialEq)] pub enum DeathScreenEvent { Show, Hide } #[derive(Event, PartialEq)] pub enum DeathScreenEvent { Show, Hide }
pub const MENUDEF: &[(&str, MenuAction)] = &[ pub const MENUDEF: &[(&str, MenuAction)] = &[
("Toggle Map [M]", MenuAction::ToggleMap), ("", MenuAction::ToggleMap),
("Toggle Augmented Reality [TAB]", MenuAction::ToggleAR), ("", MenuAction::ToggleAR),
("Toggle Sound", MenuAction::ToggleSound), ("", MenuAction::ToggleSound),
("Toggle Music", MenuAction::ToggleMusic), ("", MenuAction::ToggleMusic),
("Toggle Fullscreen [F11]", MenuAction::ToggleFullscreen), ("Toggle Fullscreen [F11]", MenuAction::ToggleFullscreen),
("Toggle Shadows", MenuAction::ToggleShadows), ("", MenuAction::ToggleShadows),
("Restart Game", MenuAction::Restart), ("Restart Game", MenuAction::Restart),
("Quit", MenuAction::Quit), ("Quit", MenuAction::Quit),
]; ];
@ -244,11 +246,15 @@ pub struct MenuState {
pub fn update_menu( pub fn update_menu(
mut q_text: Query<&mut Text, With<MenuTopLevel>>, mut q_text: Query<&mut Text, With<MenuTopLevel>>,
mut q_vis: Query<&mut Visibility, With<menu::MenuElement>>,
menustate: Res<MenuState>, menustate: Res<MenuState>,
settings: Res<Settings>, settings: Res<Settings>,
) { ) {
if !settings.menu_active { for mut vis in &mut q_vis {
return; *vis = bool2vis(settings.menu_active);
}
fn bool2string(boolean: bool) -> String {
if boolean { "On" } else { "Off" }.to_string()
} }
if let Ok(mut text) = q_text.get_single_mut() { if let Ok(mut text) = q_text.get_single_mut() {
for i in 0..text.sections.len() { for i in 0..text.sections.len() {
@ -257,6 +263,34 @@ pub fn update_menu(
} else { } else {
text.sections[i].style.color = settings.hud_color; text.sections[i].style.color = settings.hud_color;
} }
match MENUDEF[i].1 {
MenuAction::ToggleSound => {
let onoff = bool2string(!settings.mute_sfx);
text.sections[i].value = format!("Sound: {onoff}\n");
}
MenuAction::ToggleMusic => {
let onoff = bool2string(!settings.mute_music);
text.sections[i].value = format!("Music: {onoff}\n");
}
MenuAction::ToggleAR => {
let onoff = bool2string(settings.hud_active);
text.sections[i].value = format!("Augmented Reality: {onoff} [TAB]\n");
}
MenuAction::ToggleMap => {
let onoff = bool2string(settings.map_active);
text.sections[i].value = format!("Map: {onoff} [M]\n");
}
MenuAction::ToggleShadows => {
let onoff = if settings.shadows_sun {
"Flashlight + Sun"
} else {
"Flashlight Only"
};
text.sections[i].value = format!("Shadows: {onoff}\n");
}
_ => {}
}
} }
} }
} }
@ -269,6 +303,7 @@ pub fn handle_input(
mut ew_game: EventWriter<game::GameEvent>, mut ew_game: EventWriter<game::GameEvent>,
mut ew_playerdies: EventWriter<game::PlayerDiesEvent>, mut ew_playerdies: EventWriter<game::PlayerDiesEvent>,
mut ew_sfx: EventWriter<audio::PlaySfxEvent>, mut ew_sfx: EventWriter<audio::PlaySfxEvent>,
mut ew_updatemenu: EventWriter<UpdateMenuEvent>,
) { ) {
if keyboard_input.just_pressed(settings.key_menu) if keyboard_input.just_pressed(settings.key_menu)
|| keyboard_input.just_pressed(settings.key_vehicle) && settings.menu_active || keyboard_input.just_pressed(settings.key_vehicle) && settings.menu_active
@ -284,6 +319,7 @@ pub fn handle_input(
{ {
menustate.cursor = menustate.cursor.saturating_sub(1); menustate.cursor = menustate.cursor.saturating_sub(1);
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click)); ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click));
ew_updatemenu.send(UpdateMenuEvent);
} }
if keyboard_input.just_pressed(settings.key_back) if keyboard_input.just_pressed(settings.key_back)
|| keyboard_input.just_pressed(settings.key_mousedown) || keyboard_input.just_pressed(settings.key_mousedown)
@ -291,6 +327,7 @@ pub fn handle_input(
{ {
menustate.cursor = (menustate.cursor + 1).min(MENUDEF.len() - 1); menustate.cursor = (menustate.cursor + 1).min(MENUDEF.len() - 1);
ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click)); ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::Click));
ew_updatemenu.send(UpdateMenuEvent);
} }
if keyboard_input.just_pressed(settings.key_interact) if keyboard_input.just_pressed(settings.key_interact)
|| keyboard_input.just_pressed(KeyCode::Enter) || keyboard_input.just_pressed(KeyCode::Enter)
@ -300,21 +337,26 @@ pub fn handle_input(
MenuAction::ToggleMap => { MenuAction::ToggleMap => {
ew_game.send(GameEvent::SetMap(Toggle)); ew_game.send(GameEvent::SetMap(Toggle));
ew_game.send(GameEvent::SetMenu(Turn::Off)); ew_game.send(GameEvent::SetMenu(Turn::Off));
ew_updatemenu.send(UpdateMenuEvent);
}, },
MenuAction::ToggleAR => { MenuAction::ToggleAR => {
ew_game.send(GameEvent::SetAR(Toggle)); ew_game.send(GameEvent::SetAR(Toggle));
ew_updatemenu.send(UpdateMenuEvent);
}, },
MenuAction::ToggleMusic => { MenuAction::ToggleMusic => {
ew_game.send(GameEvent::SetMusic(Toggle)); ew_game.send(GameEvent::SetMusic(Toggle));
ew_updatemenu.send(UpdateMenuEvent);
}, },
MenuAction::ToggleSound => { MenuAction::ToggleSound => {
ew_game.send(GameEvent::SetSound(Toggle)); ew_game.send(GameEvent::SetSound(Toggle));
ew_updatemenu.send(UpdateMenuEvent);
}, },
MenuAction::ToggleFullscreen => { MenuAction::ToggleFullscreen => {
ew_game.send(GameEvent::SetFullscreen(Toggle)); ew_game.send(GameEvent::SetFullscreen(Toggle));
}, },
MenuAction::ToggleShadows => { MenuAction::ToggleShadows => {
ew_game.send(GameEvent::SetShadows(Toggle)); ew_game.send(GameEvent::SetShadows(Toggle));
ew_updatemenu.send(UpdateMenuEvent);
}, },
MenuAction::Restart => { MenuAction::Restart => {
settings.god_mode = false; settings.god_mode = false;