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

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2024"
[features]
default = ["lightyear/server"]
dbg = ["avian3d/debug-plugin", "dep:bevy-inspector-egui", "shared/dbg"]
[dependencies]
@@ -19,6 +20,8 @@ bevy_debug_log = { workspace = true }
bevy_sprite3d = { workspace = true }
bevy_trenchbroom = { workspace = true }
happy_feet = { workspace = true }
lightyear = { workspace = true }
lightyear_avian3d = { workspace = true }
nil = { workspace = true }
rand = { workspace = true }
ron = { workspace = true }

View File

@@ -6,9 +6,13 @@ use bevy_sprite3d::Sprite3dPlugin;
use bevy_trenchbroom::prelude::*;
use bevy_ui_gradients::UiGradientsPlugin;
use heads_database::HeadDatabaseAsset;
use lightyear::prelude::server::ServerPlugins;
use shared::*;
use std::time::Duration;
use utils::{billboards, sprite_3d_animation, squish_animation, trail};
mod server;
fn main() {
let mut app = App::new();
@@ -44,6 +48,9 @@ fn main() {
bevy_debug_log::LogViewerPlugin::default()
.auto_open_threshold(bevy::log::tracing::level_filters::LevelFilter::OFF),
);
app.add_plugins(ServerPlugins {
tick_duration: Duration::from_secs_f32(1.0 / 60.0),
});
app.add_plugins(PhysicsPlugins::default());
app.add_plugins(Sprite3dPlugin);
app.add_plugins(TrenchBroomPlugins(
@@ -77,6 +84,8 @@ fn main() {
app.add_plugins(movables::plugin);
app.add_plugins(billboards::plugin);
app.add_plugins(aim::plugin);
app.add_plugins(protocol::plugin);
app.add_plugins(server::plugin);
app.add_plugins(npc::plugin);
app.add_plugins(keys::plugin);
app.add_plugins(squish_animation::plugin);
@@ -92,7 +101,7 @@ fn main() {
app.add_plugins(heads::plugin);
app.add_plugins(hitpoints::plugin);
app.add_plugins(cash_heal::plugin);
app.add_plugins(utils::observers::plugin);
app.add_plugins(utils::plugin);
app.add_plugins(water::plugin);
app.add_plugins(head_drop::plugin);
app.add_plugins(trail::plugin);

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(())
}