implement radio stations

This commit is contained in:
yuni 2024-06-13 03:26:19 +02:00
parent f1512e01c9
commit 8c7a856717
5 changed files with 71 additions and 21 deletions

View file

@ -166,8 +166,8 @@ pub fn setup(
pub fn respawn_sinks( pub fn respawn_sinks(
mut commands: Commands, mut commands: Commands,
asset_server: Res<AssetServer>, asset_server: Res<AssetServer>,
settings: Res<var::Settings>,
q_audiosinks: Query<Entity, (With<AudioSink>, With<Sfx>)>, q_audiosinks: Query<Entity, (With<AudioSink>, With<Sfx>)>,
mut ew_togglemusic: EventWriter<ToggleMusicEvent>,
) { ) {
for sink in &q_audiosinks { for sink in &q_audiosinks {
commands.entity(sink).despawn(); commands.entity(sink).despawn();
@ -182,7 +182,7 @@ pub fn respawn_sinks(
source, source,
settings: PlaybackSettings { settings: PlaybackSettings {
mode: PlaybackMode::Loop, mode: PlaybackMode::Loop,
paused: settings.mute_music || !settings.hud_active, paused: true,
..default() ..default()
}, },
}, },
@ -195,7 +195,7 @@ pub fn respawn_sinks(
source, source,
settings: PlaybackSettings { settings: PlaybackSettings {
mode: PlaybackMode::Loop, mode: PlaybackMode::Loop,
paused: settings.mute_music || settings.hud_active, paused: true,
..default() ..default()
}, },
}, },
@ -218,6 +218,7 @@ pub fn respawn_sinks(
SfxType::OneOff => (), SfxType::OneOff => (),
} }
} }
ew_togglemusic.send(ToggleMusicEvent());
} }
pub fn play_sfx( pub fn play_sfx(
@ -242,16 +243,16 @@ pub fn play_sfx(
pub fn toggle_music(q_audiosinks: Query<(&AudioSink, &Sfx)>, settings: Res<var::Settings>) { pub fn toggle_music(q_audiosinks: Query<(&AudioSink, &Sfx)>, settings: Res<var::Settings>) {
for (bgm_sink, sfx) in &q_audiosinks { for (bgm_sink, sfx) in &q_audiosinks {
let play = match *sfx { let play = match *sfx {
Sfx::BGM => settings.hud_active, Sfx::BGM => settings.radio_mode == 1,
Sfx::BGMActualJupiterRecording => !settings.hud_active, Sfx::BGMActualJupiterRecording => settings.radio_mode == 2,
_ => { _ => {
continue; continue;
} }
}; };
if settings.mute_music || !play { if play {
bgm_sink.pause();
} else {
bgm_sink.play(); bgm_sink.play();
} else {
bgm_sink.pause();
} }
} }
} }

View file

@ -80,7 +80,7 @@ pub enum AchievementEvent {
#[derive(Event)] #[derive(Event)]
pub enum GameEvent { pub enum GameEvent {
SetAR(Turn), SetAR(Turn),
SetMusic(Turn), SetMusic(Cycle),
SetSound(Turn), SetSound(Turn),
SetMap(Turn), SetMap(Turn),
SetFullscreen(Turn), SetFullscreen(Turn),
@ -107,9 +107,43 @@ impl Turn {
} }
} }
pub enum Cycle {
First,
Last,
Next,
Previous,
}
impl Cycle {
pub fn to_index<T>(&self, current_index: usize, vector: &Vec<T>) -> Option<usize> {
if vector.is_empty() {
return None;
}
match self {
Cycle::First => Some(0),
Cycle::Last => Some(vector.len() - 1),
Cycle::Next => {
let index = current_index.saturating_add(1);
if index >= vector.len() {
Some(0)
} else {
Some(index)
}
}
Cycle::Previous => {
if current_index == 0 {
Some(vector.len() - 1)
} else {
Some(current_index - 1)
}
}
}
}
}
pub fn setup(mut settings: ResMut<Settings>, prefs: Res<var::Preferences>) { pub fn setup(mut settings: ResMut<Settings>, prefs: Res<var::Preferences>) {
settings.hud_active = prefs.augmented_reality; settings.hud_active = prefs.augmented_reality;
settings.mute_music = !prefs.music; settings.radio_mode = prefs.radio_station;
settings.mute_sfx = !prefs.sound; settings.mute_sfx = !prefs.sound;
settings.third_person = prefs.third_person; settings.third_person = prefs.third_person;
settings.shadows_sun = prefs.shadows_sun; settings.shadows_sun = prefs.shadows_sun;
@ -139,10 +173,14 @@ pub fn handle_game_event(
prefs.save(); prefs.save();
} }
GameEvent::SetMusic(turn) => { GameEvent::SetMusic(turn) => {
// TODO invert "mute_music" to "music_active" match turn.to_index(settings.radio_mode, &settings.radio_modes) {
settings.mute_music = turn.to_bool(settings.mute_music); Some(radio_mode) => {
settings.radio_mode = radio_mode;
}
None => {}
}
ew_togglemusic.send(audio::ToggleMusicEvent()); ew_togglemusic.send(audio::ToggleMusicEvent());
prefs.music = !settings.mute_music; prefs.radio_station = settings.radio_mode;
prefs.save(); prefs.save();
} }
GameEvent::SetSound(turn) => { GameEvent::SetSound(turn) => {

View file

@ -35,6 +35,7 @@ pub mod prelude {
actor, audio, camera, chat, cmd, common, game, hud, load, menu, nature, var, visual, world, actor, audio, camera, chat, cmd, common, game, hud, load, menu, nature, var, visual, world,
}; };
pub use game::Turn::Toggle; pub use game::Turn::Toggle;
pub use game::Cycle::Next;
pub use game::{GameEvent, Turn}; pub use game::{GameEvent, Turn};
} }

View file

@ -462,8 +462,13 @@ pub fn update_menu(
text.sections[i].value = format!("Sound: {onoff}\n"); text.sections[i].value = format!("Sound: {onoff}\n");
} }
MenuAction::ToggleMusic => { MenuAction::ToggleMusic => {
let onoff = bool2string(!settings.mute_music); let station =
text.sections[i].value = format!("Music: {onoff}\n"); if let Some(station) = settings.radio_modes.get(settings.radio_mode) {
station
} else {
&settings.radio_modes[0]
};
text.sections[i].value = format!("Radio: {station}\n");
} }
MenuAction::ToggleAR => { MenuAction::ToggleAR => {
let onoff = bool2string(settings.hud_active); let onoff = bool2string(settings.hud_active);
@ -566,7 +571,7 @@ pub fn handle_input(
ew_updatemenu.send(UpdateMenuEvent); ew_updatemenu.send(UpdateMenuEvent);
} }
MenuAction::ToggleMusic => { MenuAction::ToggleMusic => {
ew_game.send(GameEvent::SetMusic(Toggle)); ew_game.send(GameEvent::SetMusic(Next));
ew_updatemenu.send(UpdateMenuEvent); ew_updatemenu.send(UpdateMenuEvent);
} }
MenuAction::ToggleSound => { MenuAction::ToggleSound => {

View file

@ -14,7 +14,7 @@
use crate::prelude::*; use crate::prelude::*;
use bevy::prelude::*; use bevy::prelude::*;
use bevy::window::WindowMode; use bevy::window::WindowMode;
use serde::{Serialize, Deserialize}; use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::env; use std::env;
use std::fs; use std::fs;
@ -40,7 +40,8 @@ pub struct Settings {
pub version: String, pub version: String,
pub alive: bool, pub alive: bool,
pub mute_sfx: bool, pub mute_sfx: bool,
pub mute_music: bool, pub radio_mode: usize,
pub radio_modes: Vec<String>,
pub volume_sfx: u8, pub volume_sfx: u8,
pub volume_music: u8, pub volume_music: u8,
pub mouse_sensitivity: f32, pub mouse_sensitivity: f32,
@ -156,7 +157,6 @@ impl Default for Settings {
fn default() -> Self { fn default() -> Self {
let dev_mode = cfg!(feature = "dev_mode") && env::var("CARGO").is_ok(); let dev_mode = cfg!(feature = "dev_mode") && env::var("CARGO").is_ok();
let default_mute_sfx = false; let default_mute_sfx = false;
let default_mute_music = dev_mode;
let version = if let Some(version) = option_env!("CARGO_PKG_VERSION") { let version = if let Some(version) = option_env!("CARGO_PKG_VERSION") {
version.to_string() version.to_string()
} else { } else {
@ -169,7 +169,12 @@ impl Default for Settings {
version, version,
alive: true, alive: true,
mute_sfx: default_mute_sfx, mute_sfx: default_mute_sfx,
mute_music: default_mute_music, radio_mode: 1,
radio_modes: vec![
"Off".to_string(),
"Cinematic".to_string(),
"Vibration Sensor Readings".to_string(),
],
volume_sfx: 100, volume_sfx: 100,
volume_music: 100, volume_music: 100,
mouse_sensitivity: 0.4, mouse_sensitivity: 0.4,
@ -418,7 +423,7 @@ pub struct Preferences {
pub fullscreen_on: bool, pub fullscreen_on: bool,
pub render_mode: String, pub render_mode: String,
pub augmented_reality: bool, pub augmented_reality: bool,
pub music: bool, pub radio_station: usize,
pub sound: bool, pub sound: bool,
pub third_person: bool, pub third_person: bool,
pub shadows_sun: bool, pub shadows_sun: bool,