~starkingdoms/starkingdoms

ref: f02a26613d42d88c6c9c33a4ce599605d9977462 starkingdoms/crates/unified/src/client/mod.rs -rw-r--r-- 4.1 KiB
f02a2661 — core feat(netcode-rewrite): re-add networking 29 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use aeronet::io::{Session, SessionEndpoint};
use aeronet::io::connection::{DisconnectReason, Disconnected};
use aeronet_transport::{Transport, TransportConfig};
use aeronet_websocket::client::{ClientConfig, WebSocketClient};
use crate::client::crafting::ui::crafting_ui_plugin;
use crate::client::key_input::key_input_plugin;
use crate::client::parts::parts_plugin;
use crate::client::planet::indicators::indicators_plugin;
use crate::client::starfield::starfield_plugin;
use crate::client::starguide::orbit::starguide_orbit_plugin;
use crate::client::ui::ui_plugin;
use crate::client::zoom::zoom_plugin;
use crate::client::starguide::init::starguide_init_plugin;
use crate::client::starguide::input::starguide_input_plugin;
use starguide::components::StarguideGizmos;
use bevy::dev_tools::picking_debug::DebugPickingMode;
use crate::prelude::*;
use planet::incoming_planets::incoming_planets_plugin;
use crate::client::ship::attachment::client_attachment_plugin;
use crate::shared::ecs::GameplayState;
use crate::shared::net::LANES;

pub mod colors;
pub mod key_input;
pub mod parts;
pub mod planet;
pub mod starfield;
pub mod ui;
pub mod zoom;
pub mod ship;
pub mod rendering;
pub mod input;
pub mod starguide;
pub mod crafting;
pub mod components;
pub mod plugins;

pub struct ClientPlugin {
    pub server: Option<String>
}

impl Plugin for ClientPlugin {
    fn build(&self, app: &mut App) {
        app
            .init_gizmo_group::<StarguideGizmos>()
            .add_plugins(rendering::render_plugin)
            .add_plugins(input::input_plugin)
            .add_plugins(ship::thrusters::client_thrusters_plugin)
            .add_plugins((incoming_planets_plugin, indicators_plugin))
            .add_plugins(parts_plugin)
            .add_plugins(key_input_plugin)
            .add_plugins(starfield_plugin)
            .add_plugins(ui_plugin)
            .add_plugins(zoom_plugin)
            .add_plugins(client_attachment_plugin)
            .add_plugins(starguide_init_plugin)
            .add_plugins(starguide_input_plugin)
            .add_plugins(starguide_orbit_plugin)
            .add_plugins(crafting_ui_plugin)
            .insert_state(GameplayState::Main)
            .insert_resource(DebugPickingMode::Disabled);

        let server = self.server.clone();
        app.add_systems(Startup, move |mut commands: Commands| {
            let Some(server) = server.as_ref() else { return };
            commands.spawn((Name::new("default-session"), TransportConfig { max_memory_usage: 536_870_912, ..default() }))
                .queue(WebSocketClient::connect(ClientConfig::builder().with_no_cert_validation(), server.clone()));
        });
        if self.server.is_some() {
            app.add_observer(on_connecting);
            app.add_observer(on_connected);
            app.add_observer(on_disconnected);
        }
    }
}

pub fn on_connecting(
    trigger: On<Add, SessionEndpoint>,
    names: Query<&Name>,
    mut commands: Commands,
) {
    let entity = trigger.event_target();
    let name = names.get(entity).unwrap();
    info!("{name} is connecting");
}
pub fn on_connected(trigger: On<Add, Session>, names: Query<&Name>, mut sessions: Query<(&Session)>, mut commands: Commands) {
    let entity = trigger.event_target();
    let name = names.get(entity).unwrap();
    info!("{name} is connected");
    let session = sessions.get_mut(entity).expect("should exist");
    commands.entity(entity).insert(Transport::new(
        session,
        LANES,
        LANES,
        bevy::platform::time::Instant::now()
    ).expect("MTU cannot support"));
}
pub fn on_disconnected(trigger: On<Disconnected>, names: Query<&Name>) {
    let session = trigger.event_target();
    let name = names.get(session).unwrap();

    match &trigger.reason {
        DisconnectReason::ByUser(reason) => {
            info!(?name, ?reason, "session disconnected by user");
        }
        DisconnectReason::ByPeer(reason) => {
            info!(?name, ?reason, "session disconnected by peer");
        }
        DisconnectReason::ByError(err) => {
            warn!(?name, "session disconnected due to error: {err:?}");
        }
    }
}