Lightyear setup (#55)

This commit is contained in:
extrawurst
2025-07-10 23:21:11 +02:00
committed by GitHub
parent 691b9eed33
commit 78b09b33d6
26 changed files with 2515 additions and 242 deletions

View File

@@ -0,0 +1,45 @@
use bevy::prelude::*;
use lightyear::prelude::{
server::{NetcodeConfig, NetcodeServer, ServerUdpIo},
*,
};
use shared::utils::commands::IsServer;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
pub fn plugin(app: &mut App) {
app.init_resource::<IsServer>();
app.add_systems(Startup, start_server);
app.add_observer(handle_new_client);
}
fn handle_new_client(
trigger: Trigger<OnAdd, Connected>,
mut commands: Commands,
id: Query<&PeerAddr>,
) -> Result {
let id = id.get(trigger.target())?;
info!("Client connected on IP: {}", id.ip());
commands
.entity(trigger.target())
.insert(ReplicationSender::default());
Ok(())
}
fn start_server(mut commands: Commands) -> Result {
let server_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 25565);
commands
.spawn((
Name::from("Server"),
LocalAddr(server_addr),
ServerUdpIo::default(),
NetcodeServer::new(NetcodeConfig::default()),
))
.trigger(server::Start);
Ok(())
}