Skip to content

Commit

Permalink
Remove config plugin
Browse files Browse the repository at this point in the history
We don't need to pretend that we can change the config while the game is
running (yet).
  • Loading branch information
Piturnah committed Jan 8, 2025
1 parent b189e03 commit 30b665f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
10 changes: 4 additions & 6 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ impl Plugin for AudioPlugin {
.add_audio_channel::<BgmChannel>()
.add_audio_channel::<SfxChannel>()
.add_startup_system(load_audio)
.add_startup_system(set_volume)
.add_enter_system(GameState::MainMenu, start_title_bgm)
.add_enter_system(GameState::InGame, start_fight_bgm)
.add_exit_system(GameState::MainMenu, stop_bgm)
.add_exit_system(GameState::InGame, stop_bgm)
.add_system(set_volume)
.add_system_set(
ConditionSet::new()
.run_in_state(GameState::InGame)
Expand All @@ -43,16 +43,14 @@ pub enum PlaySfx {
BombFuse,
}

/// Update the channel volumes if the config has changed.
/// Update the channel volumes based on values in the [`Config`] resource.
fn set_volume(
bgm_channel: Res<AudioChannel<BgmChannel>>,
sfx_channel: Res<AudioChannel<SfxChannel>>,
config: Res<Config>,
) {
if config.is_changed() {
bgm_channel.set_volume(config.bgm_volume);
sfx_channel.set_volume(config.sfx_volume);
}
bgm_channel.set_volume(config.bgm_volume);
sfx_channel.set_volume(config.sfx_volume);
}

fn start_title_bgm(audio: Res<AudioChannel<BgmChannel>>, bgm: Res<Bgm>) {
Expand Down
24 changes: 11 additions & 13 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
//! example:
//!
//! Linux: `~/.config/bomby/config.toml`
//!
//! Currently, the config is loaded at startup and cannot be changed from inside the game. So, this
//! module does not export a bevy plugin (yet).
use bevy::prelude::*;

Expand All @@ -10,6 +13,10 @@ use serde_derive::{Deserialize, Serialize};

use std::fs;

const DEFAULT_ASPECT_RATIO: f32 = 16.0 / 9.0;
const DEFAULT_WINDOW_HEIGHT: f32 = 900.0;
const DEFAULT_WINDOW_WIDTH: f32 = DEFAULT_WINDOW_HEIGHT * DEFAULT_ASPECT_RATIO;

/// Config resource containing runtime settings for the game.
#[derive(Resource, Debug, Serialize, Deserialize)]
#[serde(default)]
Expand All @@ -25,24 +32,16 @@ impl Default for Config {
fn default() -> Self {
Self {
window_resizable: true,
window_width: 1600.0,
window_height: 900.0,
window_width: DEFAULT_WINDOW_WIDTH,
window_height: DEFAULT_WINDOW_HEIGHT,
bgm_volume: 1.0,
sfx_volume: 1.0,
}
}
}

pub struct ConfigPlugin;

impl Plugin for ConfigPlugin {
fn build(&self, app: &mut App) {
app.add_startup_system(load_config);
}
}

/// Load the [`Config`] or generate a new one and insert it as a resource.
fn load_config(mut commands: Commands) {
pub fn load_config() -> Config {
let dirs = ProjectDirs::from("com", "Spicy Lobster", "Bomby");
let mut config = dirs
.map(|dirs| {
Expand Down Expand Up @@ -72,6 +71,5 @@ fn load_config(mut commands: Commands) {
config.bgm_volume = config.bgm_volume.clamp(0.0, 1.0);
config.sfx_volume = config.sfx_volume.clamp(0.0, 1.0);

info!("{config:?}");
commands.insert_resource(config);
config
}
26 changes: 7 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ mod player;
mod ui;
mod z_sort;

const RESOLUTION: f32 = 16.0 / 9.0;
const WINDOW_HEIGHT: f32 = 900.0;
const WINDOW_WIDTH: f32 = WINDOW_HEIGHT * RESOLUTION;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GameState {
MainMenu,
Expand All @@ -30,25 +26,28 @@ pub enum GameState {
pub struct GameRng(SmallRng);

fn main() {
let config = config::load_config();
info!("Initialised config: {config:?}");

App::new()
.add_loopless_state(GameState::MainMenu)
.add_plugins(
DefaultPlugins
.set(ImagePlugin::default_nearest())
.set(WindowPlugin {
window: WindowDescriptor {
width: WINDOW_WIDTH,
height: WINDOW_HEIGHT,
width: config.window_width,
height: config.window_height,
title: "Bomby!".to_string(),
resizable: true,
resizable: config.window_resizable,
..default()
},
..default()
}),
)
.insert_resource(config)
.add_plugin(bevy_kira_audio::AudioPlugin)
.add_plugin(audio::AudioPlugin)
.add_plugin(config::ConfigPlugin)
.add_plugin(debug::DebugPlugin)
.add_plugin(player::PlayerPlugin)
.add_plugin(ldtk::BombyLdtkPlugin)
Expand All @@ -57,16 +56,5 @@ fn main() {
.add_plugin(ui::UiPlugin)
.add_plugin(z_sort::ZSortPlugin)
.insert_resource(GameRng(SmallRng::from_entropy()))
.add_system(set_window_resizable)
.run();
}

/// System that detects if the [`Config`](config::Config) was changed and accordingly updates the
/// window descriptor.
fn set_window_resizable(config: Res<config::Config>, mut windows: ResMut<Windows>) {
if config.is_changed() {
let window = windows.primary_mut();
window.set_resolution(config.window_width, config.window_height);
window.set_resizable(config.window_resizable);
}
}

0 comments on commit 30b665f

Please sign in to comment.