Skip to content

Commit

Permalink
Add shadow settings
Browse files Browse the repository at this point in the history
  • Loading branch information
rparrett committed Dec 28, 2023
1 parent 5cbf958 commit 6bd0bc2
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 14 deletions.
21 changes: 14 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ impl Default for JumpCooldown {
#[derive(Event)]
struct FinishedEvent;

#[derive(Component)]
pub struct MainCamera;

const LAVA: f32 = -200.;

fn main() {
Expand Down Expand Up @@ -331,14 +334,17 @@ fn spawn_camera(mut commands: Commands, zoom: Res<Zoom>) {
UiCameraConfig { show_ui: false },
));

commands.spawn(Camera3dBundle {
camera_3d: Camera3d {
clear_color: ClearColorConfig::None,
..default()
commands.spawn((
Camera3dBundle {
camera_3d: Camera3d {
clear_color: ClearColorConfig::None,
..default()
},
transform: Transform::from_xyz(0., 0., zoom.target),
..Default::default()
},
transform: Transform::from_xyz(0., 0., zoom.target),
..Default::default()
});
MainCamera,
));
}

fn decorate_track(
Expand Down Expand Up @@ -500,6 +506,7 @@ fn spawn_player(mut commands: Commands, game_assets: Res<GameAssets>) {

commands
.spawn((
Name::new("Player"),
SceneBundle {
scene: game_assets.combine.clone(),
..default()
Expand Down
104 changes: 99 additions & 5 deletions src/main_menu.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use bevy::{
audio::{AudioSink, Volume},
pbr::{DirectionalLightShadowMap, ShadowFilteringMethod},
prelude::*,
};
use bevy_ui_navigation::prelude::*;

use crate::{
loading::{AudioAssets, GameAssets},
settings::{MusicSetting, SfxSetting},
settings::{MusicSetting, SfxSetting, ShadowSetting},
ui::{buttons, BUTTON_TEXT, CONTAINER_BACKGROUND, NORMAL_BUTTON},
GameState, MusicController,
GameState, MainCamera, MusicController,
};

pub struct MainMenuPlugin;
Expand All @@ -22,6 +23,7 @@ impl Plugin for MainMenuPlugin {
(
sfx_volume,
music_volume,
shadow_changed,
button_actions,
buttons.after(NavRequestSystem),
)
Expand All @@ -46,6 +48,11 @@ struct SfxSettingButton;
#[derive(Component)]
struct SfxSettingButtonText;
#[derive(Component)]
struct ShadowSettingButton;

#[derive(Component)]
struct ShadowSettingButtonText;
#[derive(Component)]
struct TipText;
#[derive(Resource, Default, Deref, DerefMut)]
struct TipIndex(usize);
Expand Down Expand Up @@ -76,6 +83,7 @@ fn setup_menu(
assets: Res<GameAssets>,
sfx: Res<SfxSetting>,
music: Res<MusicSetting>,
shadow: Res<ShadowSetting>,
mut tip_index: ResMut<TipIndex>,
) {
info!("setup_menu");
Expand Down Expand Up @@ -151,7 +159,7 @@ fn setup_menu(

let audio_settings_title = commands
.spawn(
TextBundle::from_section("Audio", subtitle_text_style).with_style(Style {
TextBundle::from_section("Audio", subtitle_text_style.clone()).with_style(Style {
margin: UiRect::all(Val::Px(10.0)),
..default()
}),
Expand Down Expand Up @@ -180,7 +188,7 @@ fn setup_menu(
let music_button = commands
.spawn((
ButtonBundle {
style: button_style,
style: button_style.clone(),
background_color: NORMAL_BUTTON.into(),
..default()
},
Expand All @@ -190,18 +198,48 @@ fn setup_menu(
))
.with_children(|parent| {
parent.spawn((
TextBundle::from_section(format!("Music {}%", **music), button_text_style),
TextBundle::from_section(format!("Music {}%", **music), button_text_style.clone()),
MusicSettingButtonText,
));
})
.id();

let shadow_settings_title = commands
.spawn(
TextBundle::from_section("Shadows", subtitle_text_style).with_style(Style {
margin: UiRect::all(Val::Px(10.0)),
..default()
}),
)
.id();

let shadow_button = commands
.spawn((
ButtonBundle {
style: button_style,
background_color: NORMAL_BUTTON.into(),
..default()
},
Focusable::default(),
MenuButton::Shadow,
ShadowSettingButton,
))
.with_children(|parent| {
parent.spawn((
TextBundle::from_section(format!("{}", *shadow), button_text_style),
ShadowSettingButtonText,
));
})
.id();

commands.entity(container).push_children(&[
title,
play_button,
audio_settings_title,
sfx_button,
music_button,
shadow_settings_title,
shadow_button,
]);

commands
Expand Down Expand Up @@ -248,6 +286,7 @@ enum MenuButton {
Play,
Sfx,
Music,
Shadow,
}

// Seems like bevy-ui-navigation forces us to write this abomination of a megasystem
Expand All @@ -259,8 +298,10 @@ fn button_actions(
mut text_queries: ParamSet<(
Query<&mut Text, With<SfxSettingButtonText>>,
Query<&mut Text, With<MusicSettingButtonText>>,
Query<&mut Text, With<ShadowSettingButtonText>>,
)>,
mut sfx_setting: ResMut<SfxSetting>,
mut shadow_setting: ResMut<ShadowSetting>,
) {
// Note: we have a closure here because the `buttons` query is mutable.
// for immutable queries, you can use `.activated_in_query` which returns an iterator.
Expand Down Expand Up @@ -293,6 +334,13 @@ fn button_actions(
text.sections[0].value = format!("Music {}%", **music_setting);
}
}
MenuButton::Shadow => {
*shadow_setting = shadow_setting.next();

for mut text in text_queries.p2().iter_mut() {
text.sections[0].value = format!("{}", *shadow_setting);
}
}
}
}
}
Expand Down Expand Up @@ -339,6 +387,52 @@ fn start_music(
));
}

fn shadow_changed(
mut commands: Commands,
shadow_setting: Res<ShadowSetting>,
camera_query: Query<Entity, With<MainCamera>>,
mut light_query: Query<&mut DirectionalLight>,
) {
// Do run when ShadowSetting is first added by SavePlugin
if !shadow_setting.is_changed() {
return;
}

let mut light = light_query.single_mut();
let camera_entity = camera_query.single();

match *shadow_setting {
ShadowSetting::None => {
light.shadows_enabled = false;
commands
.entity(camera_entity)
.insert(ShadowFilteringMethod::Hardware2x2);
commands.insert_resource(DirectionalLightShadowMap { size: 256 });
}
ShadowSetting::Low => {
light.shadows_enabled = true;
commands
.entity(camera_entity)
.insert(ShadowFilteringMethod::Hardware2x2);
commands.insert_resource(DirectionalLightShadowMap { size: 256 });
}
ShadowSetting::Medium => {
light.shadows_enabled = true;
commands
.entity(camera_entity)
.insert(ShadowFilteringMethod::Castano13);
commands.insert_resource(DirectionalLightShadowMap { size: 512 });
}
ShadowSetting::High => {
light.shadows_enabled = true;
commands
.entity(camera_entity)
.insert(ShadowFilteringMethod::Castano13);
commands.insert_resource(DirectionalLightShadowMap { size: 1024 });
}
}
}

fn cleanup_menu(mut commands: Commands, query: Query<Entity, With<MainMenuMarker>>) {
for entity in query.iter() {
commands.entity(entity).despawn_recursive();
Expand Down
11 changes: 9 additions & 2 deletions src/save.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::settings::{LeaderboardSetting, MusicSetting, SfxSetting};
use crate::settings::{LeaderboardSetting, MusicSetting, SfxSetting, ShadowSetting};

use bevy::prelude::*;
use ron::ser::PrettyConfig;
Expand All @@ -22,12 +22,14 @@ struct SaveFile {
sfx: SfxSetting,
music: MusicSetting,
leaderboard: LeaderboardSetting,
shadow: ShadowSetting,
}

pub fn load_system(mut commands: Commands) {
commands.insert_resource(SfxSetting::default());
commands.insert_resource(MusicSetting::default());
commands.insert_resource(LeaderboardSetting::default());
commands.insert_resource(ShadowSetting::default());

#[cfg(not(target_arch = "wasm32"))]
{
Expand All @@ -49,6 +51,7 @@ pub fn load_system(mut commands: Commands) {
commands.insert_resource(save_file.sfx);
commands.insert_resource(save_file.music);
commands.insert_resource(save_file.leaderboard);
commands.insert_resource(save_file.shadow);
}
#[cfg(target_arch = "wasm32")]
{
Expand Down Expand Up @@ -80,19 +83,22 @@ pub fn load_system(mut commands: Commands) {
commands.insert_resource(save_file.sfx);
commands.insert_resource(save_file.music);
commands.insert_resource(save_file.leaderboard);
commands.insert_resource(save_file.shadow);
}
}

pub fn save_system(
sfx: Res<SfxSetting>,
music: Res<MusicSetting>,
leaderboard: Res<LeaderboardSetting>,
shadow: Res<ShadowSetting>,
) {
let sfx_changed = sfx.is_changed() && !sfx.is_added();
let music_changed = music.is_changed() && !music.is_added();
let leaderboard_changed = leaderboard.is_changed() && !leaderboard.is_added();
let shadow_changed = shadow.is_changed() && !shadow.is_added();

if !sfx_changed && !music_changed && !leaderboard_changed {
if !sfx_changed && !music_changed && !leaderboard_changed && !shadow_changed {
return;
}

Expand All @@ -102,6 +108,7 @@ pub fn save_system(
sfx: sfx.clone(),
music: music.clone(),
leaderboard: leaderboard.clone(),
shadow: shadow.clone(),
};

let pretty = PrettyConfig::new();
Expand Down
34 changes: 34 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::prelude::*;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Resource, Deref, DerefMut, Debug, Serialize, Deserialize, Clone)]
pub struct MusicSetting(u8);
Expand All @@ -17,5 +18,38 @@ impl Default for SfxSetting {
}
}

#[derive(Resource, Debug, Serialize, Deserialize, Clone, Default)]
pub enum ShadowSetting {
None,
Low,
#[default]
Medium,
High,
}
impl ShadowSetting {
pub fn next(&self) -> Self {
match self {
Self::None => Self::Low,
Self::Low => Self::Medium,
Self::Medium => Self::High,
Self::High => Self::None,
}
}
}
impl Display for ShadowSetting {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Self::None => "None",
Self::Low => "Low",
Self::Medium => "Medium",
Self::High => "High",
}
)
}
}

#[derive(Resource, Default, Deref, DerefMut, Debug, Serialize, Deserialize, Clone)]
pub struct LeaderboardSetting(pub Option<bevy_jornet::Player>);

0 comments on commit 6bd0bc2

Please sign in to comment.