mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-12-25 21:28:20 -06:00
basic networking
This commit is contained in:
parent
0c725d9408
commit
c2a9609da5
|
@ -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 {
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
pub mod messages;
|
pub mod messages;
|
||||||
|
pub mod state;
|
||||||
|
|
14
kubi-shared/src/networking/state.rs
Normal file
14
kubi-shared/src/networking/state.rs
Normal 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,
|
||||||
|
}
|
|
@ -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"
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue