From 16077ff60946b2d9c80e3519ed24818e3f11925e Mon Sep 17 00:00:00 2001 From: Martin Indra Date: Fri, 18 Aug 2023 16:03:33 +0200 Subject: [PATCH] WIP --- Cargo.lock | 2 ++ crates/camera/src/camera.rs | 2 +- crates/camera/src/skybox.rs | 2 +- crates/core/src/events.rs | 9 +++++++-- crates/core/src/gamestate.rs | 7 ++++--- crates/core/src/gconfig.rs | 9 ++++++++- crates/loader/Cargo.toml | 8 +++++--- crates/loader/src/lib.rs | 6 +++++- crates/loader/src/map.rs | 2 +- crates/loader/src/readiness.rs | 22 +++++++++++++++++++++ crates/menu/src/multiplayer/joined/state.rs | 1 + crates/menu/src/singleplayer.rs | 1 + crates/terrain/src/plugin.rs | 2 +- 13 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 crates/loader/src/readiness.rs diff --git a/Cargo.lock b/Cargo.lock index 0413b445..a76c8e80 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2400,6 +2400,8 @@ dependencies = [ "de_conf", "de_core", "de_map", + "de_messages", + "de_multiplayer", "de_spawner", "de_terrain", "futures-lite", diff --git a/crates/camera/src/camera.rs b/crates/camera/src/camera.rs index 357baa46..763a92c1 100644 --- a/crates/camera/src/camera.rs +++ b/crates/camera/src/camera.rs @@ -107,7 +107,7 @@ impl Plugin for CameraPlugin { Update, setup_shadows .track_progress() - .run_if(in_state(GameState::Loading)), + .run_if(in_state(GameState::Initializing)), ); } } diff --git a/crates/camera/src/skybox.rs b/crates/camera/src/skybox.rs index 39d7f952..fe2cb545 100644 --- a/crates/camera/src/skybox.rs +++ b/crates/camera/src/skybox.rs @@ -19,7 +19,7 @@ impl Plugin for SkyboxPlugin { configure_cubemap .track_progress() .run_if(in_state(AppState::AppLoading)), - setup_camera.run_if(in_state(GameState::Loading)), + setup_camera.run_if(in_state(GameState::Initializing)), ), ); } diff --git a/crates/core/src/events.rs b/crates/core/src/events.rs index a0b32695..6e0dfa55 100644 --- a/crates/core/src/events.rs +++ b/crates/core/src/events.rs @@ -10,6 +10,9 @@ use bevy::{ use crate::gamestate::GameState; +/// This plugin accumulates events received during [`GameState::Waiting`] and +/// [`GameState::Initializing`] and re-sends them on enter of +/// [`GameState::Playing`]. pub struct ResendEventPlugin { _marker: PhantomData, } @@ -24,10 +27,12 @@ impl Default for ResendEventPlugin { impl Plugin for ResendEventPlugin { fn build(&self, app: &mut App) { - app.add_systems(OnEnter(GameState::Loading), setup::) + app.add_systems(OnEnter(GameState::Waiting), setup::) .add_systems( Update, - enqueue_events::.run_if(in_state(GameState::Loading)), + enqueue_events::.run_if( + in_state(GameState::Waiting).or_else(in_state(GameState::Initializing)), + ), ) .add_systems( OnEnter(GameState::Playing), diff --git a/crates/core/src/gamestate.rs b/crates/core/src/gamestate.rs index 270ade13..c78a0fae 100644 --- a/crates/core/src/gamestate.rs +++ b/crates/core/src/gamestate.rs @@ -9,7 +9,7 @@ impl Plugin for GameStateSetupPlugin { fn build(&self, app: &mut App) { app.add_plugins(( GameStatePlugin, - ProgressPlugin::new(GameState::Loading).continue_to(GameState::Playing), + ProgressPlugin::new(GameState::Initializing).continue_to(GameState::Playing), )); } } @@ -20,13 +20,14 @@ nested_state!( enter = setup, exit = cleanup, variants = { - Loading, + Waiting, + Initializing, Playing, } ); fn setup(mut next_state: ResMut>) { - next_state.set(GameState::Loading); + next_state.set(GameState::Waiting); } fn cleanup(mut commands: Commands) { diff --git a/crates/core/src/gconfig.rs b/crates/core/src/gconfig.rs index 02b3bcf3..17ba5813 100644 --- a/crates/core/src/gconfig.rs +++ b/crates/core/src/gconfig.rs @@ -10,13 +10,15 @@ use crate::player::{Player, PlayerRange}; #[derive(Resource)] pub struct GameConfig { map_path: PathBuf, + multiplayer: bool, locals: LocalPlayers, } impl GameConfig { - pub fn new>(map_path: P, locals: LocalPlayers) -> Self { + pub fn new>(map_path: P, multiplayer: bool, locals: LocalPlayers) -> Self { Self { map_path: map_path.into(), + multiplayer, locals, } } @@ -25,6 +27,10 @@ impl GameConfig { self.map_path.as_path() } + pub fn multiplayer(&self) -> bool { + self.multiplayer + } + pub fn locals(&self) -> &LocalPlayers { &self.locals } @@ -95,6 +101,7 @@ mod tests { fn test_game_config() { let config = GameConfig::new( "/some/path", + false, LocalPlayers::from_max_player(Player::Player1, Player::Player4), ); assert_eq!(config.map_path().to_string_lossy(), "/some/path"); diff --git a/crates/loader/Cargo.toml b/crates/loader/Cargo.toml index 6c632a88..cbce6bc5 100644 --- a/crates/loader/Cargo.toml +++ b/crates/loader/Cargo.toml @@ -13,12 +13,14 @@ categories.workspace = true [dependencies] # DE +de_camera.workspace = true +de_conf.workspace = true de_core.workspace = true de_map.workspace = true -de_terrain.workspace = true +de_messages.workspace = true +de_multiplayer.workspace = true de_spawner.workspace = true -de_camera.workspace = true -de_conf.workspace = true +de_terrain.workspace = true # Other bevy.workspace = true diff --git a/crates/loader/src/lib.rs b/crates/loader/src/lib.rs index 2caf71f0..9be5e331 100644 --- a/crates/loader/src/lib.rs +++ b/crates/loader/src/lib.rs @@ -1,12 +1,16 @@ use bevy::{app::PluginGroupBuilder, prelude::PluginGroup}; use map::MapLoaderPlugin; +use readiness::ReadinessPlugin; mod map; +mod readiness; pub struct LoaderPluginGroup; impl PluginGroup for LoaderPluginGroup { fn build(self) -> PluginGroupBuilder { - PluginGroupBuilder::start::().add(MapLoaderPlugin) + PluginGroupBuilder::start::() + .add(MapLoaderPlugin) + .add(ReadinessPlugin) } } diff --git a/crates/loader/src/map.rs b/crates/loader/src/map.rs index 5d8566a3..8ff3cfc1 100644 --- a/crates/loader/src/map.rs +++ b/crates/loader/src/map.rs @@ -33,7 +33,7 @@ impl Plugin for MapLoaderPlugin { Update, spawn_map .track_progress() - .run_if(in_state(GameState::Loading)), + .run_if(in_state(GameState::Initializing)), ); } } diff --git a/crates/loader/src/readiness.rs b/crates/loader/src/readiness.rs new file mode 100644 index 00000000..ab8b47e5 --- /dev/null +++ b/crates/loader/src/readiness.rs @@ -0,0 +1,22 @@ +use bevy::prelude::*; +use de_core::gamestate::GameState; +use de_messages::Readiness; +use de_multiplayer::SetReadinessEvent; + +pub(crate) struct ReadinessPlugin; + +impl Plugin for ReadinessPlugin { + fn build(&self, app: &mut App) { + app.add_systems( + OnEnter(GameState::Waiting), + set_readiness(Readiness::Prepared), + ); + // TODO + } +} + +fn set_readiness(readiness: Readiness) -> impl Fn(EventWriter) { + move |mut events: EventWriter| { + events.send(SetReadinessEvent::from(readiness)); + } +} diff --git a/crates/menu/src/multiplayer/joined/state.rs b/crates/menu/src/multiplayer/joined/state.rs index c4540107..d5a7a80b 100644 --- a/crates/menu/src/multiplayer/joined/state.rs +++ b/crates/menu/src/multiplayer/joined/state.rs @@ -143,6 +143,7 @@ fn start( commands.insert_resource(GameConfig::new( map_path, + true, LocalPlayers::from_single(player.0), )); app_state.set(AppState::InGame); diff --git a/crates/menu/src/singleplayer.rs b/crates/menu/src/singleplayer.rs index 63a8d9ac..2d8e7a42 100644 --- a/crates/menu/src/singleplayer.rs +++ b/crates/menu/src/singleplayer.rs @@ -107,6 +107,7 @@ fn button_system( Some(path) => { commands.insert_resource(GameConfig::new( path, + false, LocalPlayers::from_max_player(Player::Player1, Player::Player4), )); next_state.set(AppState::InGame); diff --git a/crates/terrain/src/plugin.rs b/crates/terrain/src/plugin.rs index 1b26214a..0d9a2c71 100644 --- a/crates/terrain/src/plugin.rs +++ b/crates/terrain/src/plugin.rs @@ -25,7 +25,7 @@ impl Plugin for TerrainPlugin { ( setup_textures .track_progress() - .run_if(in_state(GameState::Loading)), + .run_if(in_state(GameState::Initializing)), init.run_if(in_state(AppState::InGame)), ), );