basic networking

This commit is contained in:
griffi-gh 2023-02-12 04:04:48 +01:00
parent 6c27d63bdf
commit 05444bd4b8
5 changed files with 73 additions and 15 deletions

View file

@ -6,18 +6,6 @@ use kubi_udp::{ClientId, ClientIdRepr};
#[derive(Component)] #[derive(Component)]
pub struct Client(ClientId); 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<ClientId, EntityId, BuildNoHashHasher<ClientIdRepr>>); pub struct ClientMap(HashMap<ClientId, EntityId, BuildNoHashHasher<ClientIdRepr>>);
impl ClientMap { impl ClientMap {
pub fn new() -> Self { pub fn new() -> Self {

View file

@ -1 +1,2 @@
pub mod messages; pub mod messages;
pub mod state;

View file

@ -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,
}

View file

@ -5,7 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
kubi-shared = { path = "../kubi-shared" } kubi-shared = { path = "../kubi-shared" }
kubi-udp = { path = "../kubi-udp", optional = true } kubi-udp = { path = "../kubi-udp" }
kubi-logging = { path = "../kubi-logging" } kubi-logging = { path = "../kubi-logging" }
log = "*" log = "*"
glium = "0.32" glium = "0.32"

View file

@ -1,6 +1,10 @@
use shipyard::{Unique, AllStoragesView, UniqueView, UniqueViewMut, Workload, IntoWorkload, WorkloadModificator};
use std::net::SocketAddr; use std::net::SocketAddr;
use kubi_udp::client::{Client, ClientConfig};
use shipyard::Unique; use kubi_shared::networking::{
messages::{ClientToServerMessage, ServerToClientMessage},
state::ClientJoinState
};
#[derive(Unique, Clone, Copy, PartialEq, Eq)] #[derive(Unique, Clone, Copy, PartialEq, Eq)]
pub enum GameType { pub enum GameType {
@ -10,3 +14,54 @@ pub enum GameType {
#[derive(Unique, Clone, Copy, PartialEq, Eq)] #[derive(Unique, Clone, Copy, PartialEq, Eq)]
pub struct ServerAddress(pub SocketAddr); pub struct ServerAddress(pub SocketAddr);
#[derive(Unique)]
pub struct UdpClient(pub Client<ClientToServerMessage, ServerToClientMessage>);
pub fn create_client(
storages: AllStoragesView
) {
let address = storages.borrow::<UniqueView<ServerAddress>>().unwrap();
storages.add_unique(UdpClient(Client::new(
address.0,
ClientConfig::default()
).unwrap()));
storages.add_unique(ClientJoinState::Disconnected);
}
pub fn client_connect(
mut client: UniqueViewMut<UdpClient>
) {
client.0.connect().unwrap();
}
pub fn update_client(
mut client: UniqueViewMut<UdpClient>
) {
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<GameType>
) -> bool {
*game_type == GameType::Muliplayer
}
pub fn is_singleplayer(
game_type: UniqueView<GameType>
) -> bool {
*game_type == GameType::Singleplayer
}