Controller Replication (#57)
This commit is contained in:
@@ -1,20 +1,37 @@
|
||||
use bevy::prelude::*;
|
||||
use lightyear::{
|
||||
connection::client::ClientState,
|
||||
netcode::Key,
|
||||
prelude::{client::NetcodeConfig, *},
|
||||
};
|
||||
use shared::{GameState, heads_database::HeadsDatabase, tb_entities::SpawnPoint};
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.add_systems(Startup, temp_connect_on_startup);
|
||||
app.add_systems(
|
||||
FixedUpdate,
|
||||
spawn_disconnected_player.run_if(in_state(GameState::Playing)),
|
||||
);
|
||||
}
|
||||
|
||||
fn temp_connect_on_startup(mut commands: Commands) -> Result {
|
||||
let client_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 25564);
|
||||
let mut args = std::env::args();
|
||||
let client_port = loop {
|
||||
match args.next().as_deref() {
|
||||
Some("--port") => {
|
||||
break args.next().unwrap().parse::<u16>().unwrap();
|
||||
}
|
||||
Some(_) => (),
|
||||
None => break 25564,
|
||||
}
|
||||
};
|
||||
|
||||
let client_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), client_port);
|
||||
let server_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 25565);
|
||||
let auth = Authentication::Manual {
|
||||
server_addr,
|
||||
client_id: 0,
|
||||
client_id: client_port as u64,
|
||||
private_key: Key::default(),
|
||||
protocol_id: 0,
|
||||
};
|
||||
@@ -26,10 +43,28 @@ fn temp_connect_on_startup(mut commands: Commands) -> Result {
|
||||
PeerAddr(server_addr),
|
||||
Link::new(None),
|
||||
ReplicationReceiver::default(),
|
||||
client::NetcodeClient::new(auth, NetcodeConfig::default())?,
|
||||
client::NetcodeClient::new(
|
||||
auth,
|
||||
NetcodeConfig {
|
||||
client_timeout_secs: 1,
|
||||
..default()
|
||||
},
|
||||
)?,
|
||||
UdpIo::default(),
|
||||
))
|
||||
.trigger(Connect);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn spawn_disconnected_player(
|
||||
disconnected: Single<&Client, Changed<Client>>,
|
||||
commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
query: Query<&Transform, With<SpawnPoint>>,
|
||||
heads_db: Res<HeadsDatabase>,
|
||||
) {
|
||||
if disconnected.state == ClientState::Disconnected {
|
||||
shared::player::spawn(commands, query, asset_server, heads_db)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user