From 376c37a0055f79b587918f98063000ab4200b3e6 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 20 Dec 2024 18:17:24 +0100 Subject: [PATCH 1/6] upgrade to replicon 0.29 --- Cargo.toml | 20 +++------- examples/interpolated.rs | 46 +++++++++++----------- examples/no_interpolation_or_prediction.rs | 37 ++++++++--------- examples/owner_predicted.rs | 34 +++++++--------- src/interpolation.rs | 31 ++++++++------- src/prediction.rs | 14 +++---- 6 files changed, 82 insertions(+), 100 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f352b08..92fecc8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bevy_replicon_snap" -version = "0.2.5" +version = "0.2.6" edition = "2021" authors = ["Ben Dzaebel "] hompage = "bendz.dev" @@ -13,26 +13,16 @@ description = "High-level networking crate that extends the bevy_replicon crate # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bevy = { version = "0.14", default_features = false } -bevy_replicon = "0.28.3" +bevy = { version = "0.15", default_features = false } +bevy_replicon = "0.29.1" serde = "1.0" bevy_replicon_snap_macros = { version = "0.2.0", path = "macros" } [dev-dependencies] clap = { version = "4.1", features = ["derive"] } -bevy = { version = "0.14", default-features = false, features = [ - "bevy_asset", - "bevy_core_pipeline", - "bevy_render", - "bevy_sprite", - "bevy_text", - "bevy_ui", - "bevy_gizmos", - "x11", - "default_font", -] } -bevy_replicon_renet = "0.5.0" +bevy = { version = "0.15", default-features = true } +bevy_replicon_renet = { git = "https://github.com/matoous/bevy_replicon_renet.git", branch = "md/bevy-0.15" } [package.metadata.commands] example_interpolation = "cargo run --example interpolated -- server & cargo run --example interpolated -- client && fg" diff --git a/examples/interpolated.rs b/examples/interpolated.rs index 9ad8dff..4030adc 100644 --- a/examples/interpolated.rs +++ b/examples/interpolated.rs @@ -6,17 +6,16 @@ use std::{ time::SystemTime, }; -use bevy::{prelude::*, winit::UpdateMode::Continuous, winit::WinitSettings}; +use bevy::{ + color::palettes::css::{GREEN, WHITE}, + prelude::*, + winit::{UpdateMode::Continuous, WinitSettings}, +}; use bevy_replicon::prelude::*; use bevy_replicon_renet::{ - renet::{ - transport::{ - ClientAuthentication, NetcodeClientTransport, NetcodeServerTransport, - ServerAuthentication, ServerConfig, - }, + netcode::{ClientAuthentication, NetcodeClientTransport, NetcodeServerTransport, ServerAuthentication, ServerConfig}, renet::{ ConnectionConfig, RenetClient, RenetServer, - }, - RenetChannelsExt, RepliconRenetPlugins, + }, RenetChannelsExt, RepliconRenetPlugins }; use bevy_replicon_snap::{ interpolation::{AppInterpolationExt, Interpolated}, @@ -67,7 +66,7 @@ impl Plugin for SimpleBoxPlugin { .add_systems( Update, ( - Self::movement_system.run_if(has_authority), // Runs only on the server or a single player. + Self::movement_system.run_if(server_or_singleplayer), // Runs only on the server or a single player. Self::server_event_system.run_if(server_running), // Runs only on the server. (Self::draw_boxes_system, Self::input_system), ), @@ -86,7 +85,7 @@ impl SimpleBoxPlugin { commands.spawn(PlayerBundle::new( ClientId::SERVER, Vec2::ZERO, - bevy::color::palettes::css::GREEN.into(), + GREEN.into(), )); } Cli::Server { port } => { @@ -114,18 +113,18 @@ impl SimpleBoxPlugin { commands.insert_resource(server); commands.insert_resource(transport); - commands.spawn(TextBundle::from_section( - "Server", - TextStyle { + commands.spawn(( + Text::new("Server"), + TextFont { font_size: 30.0, - color: Color::WHITE, ..default() }, + TextColor(WHITE.into()), )); commands.spawn(PlayerBundle::new( ClientId::SERVER, Vec2::ZERO, - bevy::color::palettes::css::GREEN.into(), + GREEN.into(), )); } Cli::Client { port, ip } => { @@ -153,13 +152,13 @@ impl SimpleBoxPlugin { commands.insert_resource(client); commands.insert_resource(transport); - commands.spawn(TextBundle::from_section( - format!("Client: {client_id:?}"), - TextStyle { + commands.spawn(( + Text::new(format!("Client: {client_id:?}")), + TextFont { font_size: 30.0, - color: Color::WHITE, ..default() }, + TextColor::WHITE, )); } } @@ -168,7 +167,7 @@ impl SimpleBoxPlugin { } fn init_system(mut commands: Commands) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); } /// Logs server events and spawns a new player whenever a client connects. @@ -196,9 +195,8 @@ impl SimpleBoxPlugin { fn draw_boxes_system(mut gizmos: Gizmos, players: Query<(&PlayerPosition, &PlayerColor)>) { for (position, color) in &players { - gizmos.rect( - Vec3::new(position.x, position.y, 0.0), - Quat::IDENTITY, + gizmos.rect_2d( + Isometry2d::from_xy(position.x, position.y), Vec2::ONE * 50.0, color.0, ); @@ -238,7 +236,7 @@ impl SimpleBoxPlugin { for FromClient { client_id, event } in move_events.read() { for (player, mut position) in &mut players { if client_id.get() == player.0 { - **position += event.0 * time.delta_seconds() * MOVE_SPEED; + **position += event.0 * time.delta_secs() * MOVE_SPEED; } } } diff --git a/examples/no_interpolation_or_prediction.rs b/examples/no_interpolation_or_prediction.rs index cc2d6c4..1ceaed7 100644 --- a/examples/no_interpolation_or_prediction.rs +++ b/examples/no_interpolation_or_prediction.rs @@ -12,13 +12,11 @@ use serde::{Deserialize, Serialize}; use bevy_replicon::prelude::*; use bevy_replicon_renet::{ - renet::{ - transport::{ - ClientAuthentication, NetcodeClientTransport, NetcodeServerTransport, - ServerAuthentication, ServerConfig, - }, - ConnectionConfig, RenetClient, RenetServer, + netcode::{ + ClientAuthentication, NetcodeClientTransport, NetcodeServerTransport, ServerAuthentication, + ServerConfig, }, + renet::{ConnectionConfig, RenetClient, RenetServer}, RenetChannelsExt, RepliconRenetPlugins, }; @@ -60,7 +58,7 @@ impl Plugin for SimpleBoxPlugin { .add_systems( Update, ( - Self::movement_system.run_if(has_authority), // Runs only on the server or a single player. + Self::movement_system.run_if(server_or_singleplayer), // Runs only on the server or a single player. Self::server_event_system.run_if(resource_exists::), // Runs only on the server. (Self::draw_boxes_system, Self::input_system), ), @@ -107,13 +105,13 @@ impl SimpleBoxPlugin { commands.insert_resource(server); commands.insert_resource(transport); - commands.spawn(TextBundle::from_section( - "Server", - TextStyle { + commands.spawn(( + Text::new("Server"), + TextFont { font_size: 30.0, - color: Color::WHITE, ..default() }, + TextColor::WHITE, )); commands.spawn(PlayerBundle::new( ClientId::SERVER, @@ -146,13 +144,13 @@ impl SimpleBoxPlugin { commands.insert_resource(client); commands.insert_resource(transport); - commands.spawn(TextBundle::from_section( - format!("Client: {client_id:?}"), - TextStyle { + commands.spawn(( + Text::new(format!("Client: {client_id:?}")), + TextFont { font_size: 30.0, - color: Color::WHITE, ..default() }, + TextColor::WHITE, )); } } @@ -161,7 +159,7 @@ impl SimpleBoxPlugin { } fn init_system(mut commands: Commands) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); } /// Logs server events and spawns a new player whenever a client connects. @@ -189,9 +187,8 @@ impl SimpleBoxPlugin { fn draw_boxes_system(mut gizmos: Gizmos, players: Query<(&PlayerPosition, &PlayerColor)>) { for (position, color) in &players { - gizmos.rect( - Vec3::new(position.x, position.y, 0.0), - Quat::IDENTITY, + gizmos.rect_2d( + Isometry2d::from_xy(position.x, position.y), Vec2::ONE * 50.0, color.0, ); @@ -232,7 +229,7 @@ impl SimpleBoxPlugin { info!("received event {event:?} from client {client_id:?}"); for (player, mut position) in &mut players { if *client_id == player.0 { - **position += event.0 * time.delta_seconds() * MOVE_SPEED; + **position += event.0 * time.delta_secs() * MOVE_SPEED; } } } diff --git a/examples/owner_predicted.rs b/examples/owner_predicted.rs index 0ca668e..24480ca 100644 --- a/examples/owner_predicted.rs +++ b/examples/owner_predicted.rs @@ -11,13 +11,11 @@ use std::{ use bevy::{prelude::*, winit::UpdateMode::Continuous, winit::WinitSettings}; use bevy_replicon::prelude::*; use bevy_replicon_renet::{ - renet::{ - transport::{ - ClientAuthentication, NetcodeClientTransport, NetcodeServerTransport, - ServerAuthentication, ServerConfig, - }, - ConnectionConfig, RenetClient, RenetServer, + netcode::{ + ClientAuthentication, NetcodeClientTransport, NetcodeServerTransport, ServerAuthentication, + ServerConfig, }, + renet::{ConnectionConfig, RenetClient, RenetServer}, RenetChannelsExt, RepliconRenetPlugins, }; use bevy_replicon_snap::{ @@ -118,13 +116,13 @@ impl SimpleBoxPlugin { commands.insert_resource(server); commands.insert_resource(transport); - commands.spawn(TextBundle::from_section( - "Server", - TextStyle { + commands.spawn(( + Text::new("Server"), + TextFont { font_size: 30.0, - color: Color::WHITE, ..default() }, + TextColor::WHITE, )); commands.spawn(PlayerBundle::new( ClientId::SERVER, @@ -156,14 +154,13 @@ impl SimpleBoxPlugin { commands.insert_resource(client); commands.insert_resource(transport); - - commands.spawn(TextBundle::from_section( - format!("Client: {client_id:?}"), - TextStyle { + commands.spawn(( + Text::new(format!("Client: {client_id:?}")), + TextFont { font_size: 30.0, - color: Color::WHITE, ..default() }, + TextColor::WHITE, )); } } @@ -172,7 +169,7 @@ impl SimpleBoxPlugin { } fn init_system(mut commands: Commands) { - commands.spawn(Camera2dBundle::default()); + commands.spawn(Camera2d); } /// Logs server events and spawns a new player whenever a client connects. @@ -200,9 +197,8 @@ impl SimpleBoxPlugin { fn draw_boxes_system(mut gizmos: Gizmos, players: Query<(&PlayerPosition, &PlayerColor)>) { for (position, color) in &players { - gizmos.rect( - Vec3::new(position.x, position.y, 0.0), - Quat::IDENTITY, + gizmos.rect_2d( + Isometry2d::from_xy(position.x, position.y), Vec2::ONE * 50.0, color.0, ); diff --git a/src/interpolation.rs b/src/interpolation.rs index 8278830..3ea601f 100644 --- a/src/interpolation.rs +++ b/src/interpolation.rs @@ -6,23 +6,25 @@ use bevy::{ component::Component, entity::Entity, query::{Added, Or, With, Without}, - schedule::{common_conditions::resource_exists, IntoSystemConfigs}, - system::{Commands, Query, Res, Resource}, - world::EntityMut, + schedule::IntoSystemConfigs, + system::{Commands, Query, Res}, }, + prelude::Resource, reflect::Reflect, time::Time, utils::default, }; use bevy_replicon::{ bincode, - core::{ - command_markers::{AppMarkerExt, MarkerConfig}, - common_conditions::client_connected, - ctx::{RemoveCtx, WriteCtx}, - replication_registry::rule_fns::RuleFns, - replication_rules::AppRuleExt, + core::replication::{ + command_markers::MarkerConfig, + deferred_entity::DeferredEntity, + replication_registry::{ + ctx::{RemoveCtx, WriteCtx}, + rule_fns::RuleFns, + }, }, + prelude::{client_connected, AppMarkerExt, AppRuleExt}, }; use serde::{de::DeserializeOwned, Deserialize, Serialize}; @@ -104,13 +106,13 @@ pub fn snapshot_interpolation_system( let tick_duration = 1.0 / (config.max_tick_rate as f32); - if elapsed > tick_duration + time.delta_seconds() { + if elapsed > tick_duration + time.delta_secs() { continue; } let t = (elapsed / tick_duration).clamp(0., 1.); *component = buffer[0].interpolate(buffer[1].clone(), t); - snapshot_buffer.time_since_last_snapshot += time.delta_seconds(); + snapshot_buffer.time_since_last_snapshot += time.delta_secs(); } } @@ -127,7 +129,7 @@ pub fn snapshot_buffer_init_system( pub fn write_snap_component( ctx: &mut WriteCtx, rule_fns: &RuleFns, - entity: &mut EntityMut, + entity: &mut DeferredEntity, cursor: &mut Cursor<&[u8]>, ) -> bincode::Result<()> { let component: C = rule_fns.deserialize(ctx, cursor)?; @@ -144,13 +146,14 @@ pub fn write_snap_component( ctx: &mut RemoveCtx, - entity: &mut EntityMut, + entity: &mut DeferredEntity, ) { ctx.commands .entity(entity.id()) .remove::>() .remove::(); } + pub trait AppInterpolationExt { /// Register a component to be replicated and interpolated between server updates /// Requires the component to implement the Interpolate trait @@ -186,7 +189,7 @@ impl AppInterpolationExt for App { ..default() }) .set_marker_fns::( - write_snap_component::, + write_snap_component, remove_snap_component::, ) } diff --git a/src/prediction.rs b/src/prediction.rs index 4f31462..d5a8668 100644 --- a/src/prediction.rs +++ b/src/prediction.rs @@ -14,12 +14,10 @@ use bevy::{ use bevy_replicon::{ client::confirm_history::ConfirmHistory, core::{ - channels::RepliconChannel, - common_conditions::{client_connected, has_authority}, - replication_rules::AppRuleExt, + channels::RepliconChannel, common_conditions::client_connected, replicon_client::RepliconClient, }, - prelude::{ClientEventAppExt, FromClient}, + prelude::{server_or_singleplayer, AppRuleExt, ClientEventAppExt, FromClient}, }; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use std::collections::vec_deque::Iter; @@ -106,7 +104,7 @@ pub fn predicted_snapshot_system( time: Res