DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

0cc6737453188e1baa069e7c6546b5e3fd7ccdd3 — Jonni Liljamo 1 year, 9 months ago 842b6a0
feat(client): graceful exit with disconnect
2 files changed, 27 insertions(+), 5 deletions(-)

M client/src/main.rs
M client/src/plugins/menu/ui/menu.rs
M client/src/main.rs => client/src/main.rs +23 -1
@@ 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);
    }
}

M client/src/plugins/menu/ui/menu.rs => client/src/plugins/menu/ui/menu.rs +4 -4
@@ 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);
            }
        },
    );