diff --git a/src/actor.rs b/src/actor.rs index 9574dcd..8d0d2c7 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -35,29 +35,35 @@ impl Plugin for ActorPlugin { app.add_systems( FixedUpdate, ( - update_physics_lifeforms, - update_power, - handle_gravity, - handle_wants_maxrotation, - handle_wants_maxvelocity.run_if(any_with_component::), - handle_wants_lookat.run_if(alive), + update_physics_lifeforms.run_if(game_running), + update_power.run_if(game_running), + handle_gravity.run_if(game_running), + handle_wants_maxrotation.run_if(game_running), + handle_wants_maxvelocity + .run_if(game_running) + .run_if(any_with_component::), + handle_wants_lookat.run_if(game_running).run_if(alive), ), ); app.add_systems( PostUpdate, handle_gforce + .run_if(game_running) .after(PhysicsSet::Sync) .after(sync::position_to_transform), ); app.add_systems( Update, ( - handle_input.run_if(in_control), - handle_collisions, - handle_damage, + handle_input.run_if(in_control).run_if(game_running), + handle_collisions.run_if(game_running), + handle_damage.run_if(game_running), ), ); - app.add_systems(PostUpdate, (handle_vehicle_enter_exit,)); + app.add_systems( + PostUpdate, + (handle_vehicle_enter_exit.run_if(game_running),), + ); app.add_event::(); } } diff --git a/src/camera.rs b/src/camera.rs index 8c4f17e..b32591a 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -32,15 +32,23 @@ pub struct CameraPlugin; impl Plugin for CameraPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, setup_camera); - app.add_systems(Update, handle_input.run_if(in_control)); - app.add_systems(Update, update_map_only_object_visibility.run_if(alive)); + app.add_systems(Update, handle_input.run_if(in_control).run_if(game_running)); + app.add_systems( + Update, + update_map_only_object_visibility + .run_if(alive) + .run_if(game_running), + ); app.add_systems( PostUpdate, - manage_player_actor.in_set(sync::SyncSet::PositionToTransform), + manage_player_actor + .run_if(game_running) + .in_set(sync::SyncSet::PositionToTransform), ); app.add_systems( PostUpdate, sync_camera_to_player + .run_if(game_running) .after(PhysicsSet::Sync) .after(apply_input_to_player) .before(TransformSystem::TransformPropagate), @@ -48,12 +56,16 @@ impl Plugin for CameraPlugin { app.add_systems( PostUpdate, update_mapcam_center + .run_if(game_running) .before(sync::position_to_transform) .in_set(sync::SyncSet::PositionToTransform), ); - app.add_systems(Update, update_map_camera.run_if(in_control)); - app.add_systems(Update, update_fov.run_if(alive)); - app.add_systems(PreUpdate, apply_input_to_player); + app.add_systems( + Update, + update_map_camera.run_if(in_control).run_if(game_running), + ); + app.add_systems(Update, update_fov.run_if(alive).run_if(game_running)); + app.add_systems(PreUpdate, apply_input_to_player.run_if(game_running)); app.insert_resource(MapCam::default()); // To center the renderer origin on the player camera, @@ -66,6 +78,7 @@ impl Plugin for CameraPlugin { app.add_systems( PostUpdate, position_to_transform + .run_if(game_running) .after(sync::position_to_transform) .in_set(sync::SyncSet::PositionToTransform), ); diff --git a/src/chat.rs b/src/chat.rs index 0017807..297ebc7 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -75,12 +75,20 @@ impl Plugin for ChatPlugin { app.add_systems( Update, ( - handle_reply_keys.before(handle_chat_timer), - handle_chat_timer.before(handle_chat_events), - handle_new_conversations.before(handle_chat_events), - handle_chat_events.before(handle_chat_scripts), - handle_chat_scripts, - update_chat_variables, + handle_reply_keys + .run_if(game_running) + .before(handle_chat_timer), + handle_chat_timer + .run_if(game_running) + .before(handle_chat_events), + handle_new_conversations + .run_if(game_running) + .before(handle_chat_events), + handle_chat_events + .run_if(game_running) + .before(handle_chat_scripts), + handle_chat_scripts.run_if(game_running), + update_chat_variables.run_if(game_running), ), ); app.add_event::(); diff --git a/src/cmd.rs b/src/cmd.rs index 729508e..4b7dbf1 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -29,15 +29,21 @@ impl Plugin for CmdPlugin { app.add_systems( Update, handle_spawn_events + .run_if(game_running) .before(spawn_entities) .before(spawn_scenes), ); - app.add_systems(Update, spawn_entities); - app.add_systems(Update, spawn_scenes.after(spawn_entities)); - app.add_systems(Update, process_mesh); + app.add_systems(Update, spawn_entities.run_if(game_running)); + app.add_systems( + Update, + spawn_scenes.run_if(game_running).after(spawn_entities), + ); + app.add_systems(Update, process_mesh.run_if(game_running)); app.add_systems( PreUpdate, - hide_colliders.run_if(any_with_component::), + hide_colliders + .run_if(game_running) + .run_if(any_with_component::), ); app.add_event::(); app.add_event::(); diff --git a/src/common.rs b/src/common.rs index 2cc54ab..dcf3d02 100644 --- a/src/common.rs +++ b/src/common.rs @@ -72,6 +72,10 @@ pub fn in_control(settings: Res) -> bool { return settings.in_control(); } +pub fn game_running(settings: Res) -> bool { + return settings.is_game_running(); +} + pub fn in_shadow( light_source_pos: DVec3, light_source_r: f64, diff --git a/src/game.rs b/src/game.rs index 55253f8..7841c15 100644 --- a/src/game.rs +++ b/src/game.rs @@ -26,20 +26,28 @@ pub struct GamePlugin; impl Plugin for GamePlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, setup); - app.add_systems(Update, handle_cheats.run_if(in_control)); - app.add_systems(Update, debug); + app.add_systems( + Update, + handle_cheats.run_if(game_running).run_if(in_control), + ); + app.add_systems(Update, debug.run_if(game_running)); app.add_systems(PostUpdate, handle_game_event); - app.add_systems(PreUpdate, handle_player_death); + app.add_systems(Update, handle_window_focus); + app.add_systems(PreUpdate, handle_player_death.run_if(game_running)); app.add_systems( PostUpdate, - update_id2pos.in_set(bevy_xpbd_3d::plugins::sync::SyncSet::PositionToTransform), + update_id2pos + .run_if(game_running) + .in_set(bevy_xpbd_3d::plugins::sync::SyncSet::PositionToTransform), ); app.add_systems(PostUpdate, update_id2v); app.add_systems( Update, - handle_achievement_event.run_if(on_event::()), + handle_achievement_event + .run_if(game_running) + .run_if(on_event::()), ); - app.add_systems(Update, check_achievements); + app.add_systems(Update, check_achievements.run_if(game_running)); app.insert_resource(Id2Pos(HashMap::new())); app.insert_resource(Id2V(HashMap::new())); app.insert_resource(JupiterPos(DVec3::ZERO)); @@ -609,3 +617,19 @@ fn check_achievements( ew_achievement.send(AchievementEvent::InJupitersShadow); } } + +fn handle_window_focus( + mut settings: ResMut, + mut er_focus: EventReader, + mut physicstime: ResMut>, +) { + for event in er_focus.read() { + settings.window_focused = event.focused; + } + + if settings.is_game_running() { + physicstime.unpause(); + } else { + physicstime.pause(); + } +} diff --git a/src/hud.rs b/src/hud.rs index c4351d8..bd7efc3 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -57,26 +57,31 @@ impl Plugin for HudPlugin { app.add_systems( Update, ( - update_hud, - update_dashboard, - update_speedometer, - update_gauges, - handle_input.run_if(in_control), - handle_target_event, + update_hud.run_if(game_running), + update_dashboard.run_if(game_running), + update_speedometer.run_if(game_running), + update_gauges.run_if(game_running), + handle_input.run_if(game_running).run_if(in_control), + handle_target_event.run_if(game_running), ), ); app.add_systems( PostUpdate, ( - update_overlay_visibility, - update_avatar.run_if(on_event::()), + update_overlay_visibility.run_if(game_running), + update_avatar + .run_if(game_running) + .run_if(on_event::()), update_ar_overlays + .run_if(game_running) .after(camera::position_to_transform) .in_set(sync::SyncSet::PositionToTransform), update_poi_overlays + .run_if(game_running) .after(camera::position_to_transform) .in_set(sync::SyncSet::PositionToTransform), update_target_selectagon + .run_if(game_running) .after(PhysicsSet::Sync) .after(camera::apply_input_to_player) .before(TransformSystem::TransformPropagate), diff --git a/src/var.rs b/src/var.rs index ff38e37..300e23a 100644 --- a/src/var.rs +++ b/src/var.rs @@ -39,6 +39,7 @@ pub struct Settings { pub god_mode: bool, pub version: String, pub alive: bool, + pub window_focused: bool, pub mute_sfx: bool, pub noise_cancellation_mode: usize, pub noise_cancellation_modes: Vec<(String, f32)>, @@ -171,6 +172,7 @@ impl Default for Settings { god_mode: false, version, alive: true, + window_focused: true, mute_sfx: false, noise_cancellation_mode: 0, noise_cancellation_modes: vec![ @@ -308,6 +310,10 @@ impl Settings { *self = Self::default(); } + pub fn is_game_running(&self) -> bool { + !self.menu_active && self.window_focused + } + pub fn reset_player_settings(&mut self) { println!("Resetting player settings!"); let default = Self::default(); diff --git a/src/visual.rs b/src/visual.rs index 48fbfce..4ecaee4 100644 --- a/src/visual.rs +++ b/src/visual.rs @@ -22,14 +22,17 @@ impl Plugin for VisualPlugin { app.add_systems(Startup, setup.after(menu::setup).after(hud::setup)); app.add_systems( Startup, - spawn_effects.after(setup).after(camera::setup_camera), + spawn_effects + .run_if(game_running) + .after(setup) + .after(camera::setup_camera), ); - app.add_systems(Update, spawn_effects); - app.add_systems(Update, update_fadein); - app.add_systems(Update, update_fadeout); - app.add_systems(Update, update_fade_material); - app.add_systems(Update, update_grow); - app.add_systems(Update, play_animations); + app.add_systems(Update, spawn_effects.run_if(game_running)); + app.add_systems(Update, update_fadein.run_if(game_running)); + app.add_systems(Update, update_fadeout.run_if(game_running)); + app.add_systems(Update, update_fade_material.run_if(game_running)); + app.add_systems(Update, update_grow.run_if(game_running)); + app.add_systems(Update, play_animations.run_if(game_running)); // Blackout disabled for now //app.add_systems(Update, update_blackout); app.add_event::(); diff --git a/src/world.rs b/src/world.rs index 4bca800..2517005 100644 --- a/src/world.rs +++ b/src/world.rs @@ -37,10 +37,17 @@ pub struct WorldPlugin; impl Plugin for WorldPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, setup); - app.add_systems(Update, handle_respawn.run_if(on_event::())); app.add_systems( Update, - handle_despawn_at.run_if(any_with_component::), + handle_respawn + .run_if(game_running) + .run_if(on_event::()), + ); + app.add_systems( + Update, + handle_despawn_at + .run_if(game_running) + .run_if(any_with_component::), ); app.add_plugins(PhysicsPlugins::default()); //app.add_plugins(PhysicsDebugPlugin::default()); @@ -52,8 +59,8 @@ impl Plugin for WorldPlugin { ASTEROID_UPDATE_INTERVAL, TimerMode::Repeating, ))); - app.add_systems(Update, spawn_despawn_asteroids); - app.add_systems(PostUpdate, handle_despawn_asteroids); + app.add_systems(Update, spawn_despawn_asteroids.run_if(game_running)); + app.add_systems(PostUpdate, handle_despawn_asteroids.run_if(game_running)); app.add_event::(); } }