basic networking

This commit is contained in:
griffi-gh 2023-02-12 04:04:48 +01:00
parent 0c725d9408
commit c2a9609da5
5 changed files with 73 additions and 15 deletions

View file

@ -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<ClientId, EntityId, BuildNoHashHasher<ClientIdRepr>>);
impl ClientMap {
pub fn new() -> Self {

View file

@ -1 +1,2 @@
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]
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"

View file

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