From f0d690a9f862c9b685faf25a8384e8865614b8c9 Mon Sep 17 00:00:00 2001 From: Michael Bradley Date: Sat, 24 May 2025 18:44:04 -0400 Subject: [PATCH] Add basic loading state --- src/game/setup.rs | 8 ++++++++ src/lib.rs | 23 ++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/game/setup.rs b/src/game/setup.rs index 55ed28b..01b987c 100644 --- a/src/game/setup.rs +++ b/src/game/setup.rs @@ -11,6 +11,8 @@ use bevy::{ use rand::{Rng as _, SeedableRng}; use wyrand::WyRand; +use crate::AppState; + use super::{ objects::{Ball, Player, Radius, Wall}, rng::thread_rng, @@ -21,6 +23,12 @@ const BALL_COUNT: u8 = 32; const BALL_SIZES: Range = 10.0..25.0; const DIMENSION_SIZES: Range = 500.0..2000.0; +pub fn check_for_seed(seed: Option>, mut next_state: ResMut>) { + if seed.is_some() { + next_state.set(AppState::InGame); + } +} + /// The size of the playable area (x, y) #[derive(Resource)] pub struct PlayableArea(f32, f32); diff --git a/src/lib.rs b/src/lib.rs index 31ba246..113b8b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ mod game; use game::{ runtime::{move_camera, move_player, quit, zoom_camera}, seed::Seed, - setup::{setup_balls, setup_from_seed, setup_player, setup_ui, setup_walls}, + setup::{check_for_seed, setup_balls, setup_from_seed, setup_player, setup_ui, setup_walls}, }; /// The initial configuration passed to the game's setup functions. @@ -39,6 +39,13 @@ struct Source { connect: Option, } +#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)] +enum AppState { + #[default] + Loading, + InGame, +} + impl Plugin for AppSettings { fn build(&self, app: &mut App) { app.insert_resource(Gravity(Vector::ZERO)) @@ -56,23 +63,25 @@ impl Plugin for AppSettings { #[cfg(feature = "dev")] dev::dev_tools, )) + .init_state::() + .add_systems(Startup, setup_ui) .add_systems( - Startup, + OnEnter(AppState::InGame), ( setup_from_seed, - setup_ui, (setup_player, setup_balls, setup_walls).after(setup_from_seed), ), ) .add_systems( Update, ( - move_player, + check_for_seed.run_if(in_state(AppState::Loading)), + (move_player, move_camera.after(move_player), zoom_camera) + .run_if(in_state(AppState::InGame)), quit.run_if(input_pressed(KeyCode::KeyQ)), - zoom_camera, ), - ) - .add_systems(PostUpdate, move_camera); + ); + if let Some(ref seed) = self.source.seed { app.insert_resource(seed.clone()); } else if let Some(ref peer) = self.source.connect {