@@ 7,13 7,14 @@
*/
use bevy::{
+ app::AppExit,
prelude::*,
window::{CompositeAlphaMode, CursorGrabMode, PresentMode, WindowResizeConstraints},
};
use bevy_console::{ConsoleConfiguration, ConsolePlugin, ToggleConsoleKey};
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use iyes_loopless::prelude::*;
-use naia_bevy_client::{ClientConfig as NaiaClientConfig, Plugin as NaiaClientPlugin};
+use naia_bevy_client::{Client, ClientConfig as NaiaClientConfig, Plugin as NaiaClientPlugin};
use laurelin_shared::server::protocol::protocol;
@@ 107,6 108,10 @@ fn main() {
))
.add_plugin(plugins::networking::NetworkingPlugin);
+ // Graceful exit
+ app.add_event::<GracefulExit>()
+ .add_system(handle_graceful_exit);
+
app.run();
}
@@ 121,3 126,20 @@ pub fn despawn_screen<T: Component>(to_despawn: Query<Entity, With<T>>, mut comm
commands.entity(entity).despawn_recursive();
}
}
+
+pub struct GracefulExit;
+
+/// Graceful exit, disconnects client cleanly before closing
+pub fn handle_graceful_exit(
+ mut ev: EventReader<GracefulExit>,
+ mut app_exit_events: EventWriter<AppExit>,
+ mut client: Client,
+) {
+ for _ in ev.iter() {
+ if client.is_connected() {
+ client.disconnect();
+ }
+
+ app_exit_events.send(AppExit);
+ }
+}
@@ 6,12 6,12 @@
* See LICENSE for licensing information.
*/
use bevy::ecs::system::ResMut;
use bevy::prelude::*;
use bevy_inspector_egui::bevy_egui::{egui, EguiContext};
use crate::util::egui::menuwindow;
use crate::{util::egui::menuwindow, GracefulExit};
pub fn ui(mut egui_context: ResMut<EguiContext>) {
pub fn ui(mut egui_context: ResMut<EguiContext>, mut exit_events: EventWriter<GracefulExit>) {
menuwindow(
egui_context.ctx_mut(),
"Laurelin",
@@ 30,7 30,7 @@ pub fn ui(mut egui_context: ResMut<EguiContext>) {
}
if ui.button("Quit").clicked() {
//
exit_events.send(GracefulExit);
}
},
);