From c2a9609da5af0ec6354adabc9f96338028f64585 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 12 Feb 2023 04:04:48 +0100 Subject: [PATCH] basic networking --- kubi-server/src/client.rs | 12 ------ kubi-shared/src/networking.rs | 1 + kubi-shared/src/networking/state.rs | 14 +++++++ kubi/Cargo.toml | 2 +- kubi/src/networking.rs | 59 ++++++++++++++++++++++++++++- 5 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 kubi-shared/src/networking/state.rs diff --git a/kubi-server/src/client.rs b/kubi-server/src/client.rs index 2508fa1..23587b7 100644 --- a/kubi-server/src/client.rs +++ b/kubi-server/src/client.rs @@ -6,18 +6,6 @@ use kubi_udp::{ClientId, ClientIdRepr}; #[derive(Component)] pub struct Client(ClientId); - -// disconnected => connect => join => load => ingame -#[derive(Component)] -pub enum ClientJoinState { - /// Client has connected to the game, but haven't authenticated yet - Connected, - /// Client has joined the game, but haven't loaded the world yet - Joined, - /// Client is currently ingame - InGame, -} - pub struct ClientMap(HashMap>); impl ClientMap { pub fn new() -> Self { diff --git a/kubi-shared/src/networking.rs b/kubi-shared/src/networking.rs index ba63992..4945d0c 100644 --- a/kubi-shared/src/networking.rs +++ b/kubi-shared/src/networking.rs @@ -1 +1,2 @@ pub mod messages; +pub mod state; diff --git a/kubi-shared/src/networking/state.rs b/kubi-shared/src/networking/state.rs new file mode 100644 index 0000000..dbed8e1 --- /dev/null +++ b/kubi-shared/src/networking/state.rs @@ -0,0 +1,14 @@ +use shipyard::{Unique, Component}; + +// disconnected => connect => join => load => ingame +#[derive(Unique, Component)] +pub enum ClientJoinState { + /// Not connected yet + Disconnected, + /// Client has connected to the game, but hasn't authenticated yet + Connected, + /// Client has joined the game, but haven't loaded the world yet + Joined, + /// Client is currently ingame + InGame, +} diff --git a/kubi/Cargo.toml b/kubi/Cargo.toml index c88d405..90d9250 100644 --- a/kubi/Cargo.toml +++ b/kubi/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" [dependencies] kubi-shared = { path = "../kubi-shared" } -kubi-udp = { path = "../kubi-udp", optional = true } +kubi-udp = { path = "../kubi-udp" } kubi-logging = { path = "../kubi-logging" } log = "*" glium = "0.32" diff --git a/kubi/src/networking.rs b/kubi/src/networking.rs index 3a3bea4..39c9989 100644 --- a/kubi/src/networking.rs +++ b/kubi/src/networking.rs @@ -1,6 +1,10 @@ +use shipyard::{Unique, AllStoragesView, UniqueView, UniqueViewMut, Workload, IntoWorkload, WorkloadModificator}; use std::net::SocketAddr; - -use shipyard::Unique; +use kubi_udp::client::{Client, ClientConfig}; +use kubi_shared::networking::{ + messages::{ClientToServerMessage, ServerToClientMessage}, + state::ClientJoinState +}; #[derive(Unique, Clone, Copy, PartialEq, Eq)] pub enum GameType { @@ -10,3 +14,54 @@ pub enum GameType { #[derive(Unique, Clone, Copy, PartialEq, Eq)] pub struct ServerAddress(pub SocketAddr); + +#[derive(Unique)] +pub struct UdpClient(pub Client); + +pub fn create_client( + storages: AllStoragesView +) { + let address = storages.borrow::>().unwrap(); + storages.add_unique(UdpClient(Client::new( + address.0, + ClientConfig::default() + ).unwrap())); + storages.add_unique(ClientJoinState::Disconnected); +} + +pub fn client_connect( + mut client: UniqueViewMut +) { + client.0.connect().unwrap(); +} + +pub fn update_client( + mut client: UniqueViewMut +) { + client.0.update().unwrap(); +} + +pub fn init_networking() -> Workload { + ( + create_client, + client_connect, + ).into_workload().run_if(is_multiplayer) +} + +pub fn update_networking() -> Workload { + ( + update_client, + ).into_workload().run_if(is_multiplayer) +} + +pub fn is_multiplayer( + game_type: UniqueView +) -> bool { + *game_type == GameType::Muliplayer +} + +pub fn is_singleplayer( + game_type: UniqueView +) -> bool { + *game_type == GameType::Singleplayer +}