Skip to content

Commit

Permalink
Merge branch 'master' into agnostic3
Browse files Browse the repository at this point in the history
  • Loading branch information
UkoeHB authored Feb 23, 2024
2 parents a2cf4e1 + 957a0ce commit a541506
Show file tree
Hide file tree
Showing 25 changed files with 281 additions and 167 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let send_type = SendType::ReliableOrdered {
resend_time: Duration::from_millis(300)
};

// Garantee of message delivery but not order
// Guarantee of message delivery but not order
let send_type = SendType::ReliableUnordered {
resend_time: Duration::from_millis(300)
};
Expand Down Expand Up @@ -190,6 +190,8 @@ Simple chat application made with egui to demonstrate how you could handle error

Checkout [bevy_renet](https://github.com/lucaspoffo/renet/tree/master/bevy_renet) if you want to use renet as a plugin with the [Bevy engine](https://bevyengine.org/).

Checkout [renet_steam](https://github.com/lucaspoffo/renet/tree/master/renet_steam) if you want to use the steam transport layer instead of the default one.

## Visualizer

Checkout [renet_visualizer](https://github.com/lucaspoffo/renet/tree/master/renet_visualizer) for a egui plugin to plot metrics data from renet clients and servers:
Expand Down
14 changes: 6 additions & 8 deletions bevy_renet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@ keywords = ["gamedev", "networking"]
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/lucaspoffo/renet"
version = "0.0.10"
version = "0.0.11"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["transport"]
serde = ["renet/serde"]
transport = ["renet/transport"]
steam = ["renet_steam"]

[[example]]
name = "simple"
required-features = ["serde", "transport"]

[dependencies]
bevy = {version = "0.12", default-features = false}
renet = {path = "../renet", version = "0.0.14", default-features=false, features = ["bevy"]}
renet_steam = { path = "../renet_steam", version = "0.0.1", features = [ "bevy" ], optional = true }
bevy = {version = "0.13", default-features = false}
renet = {path = "../renet", version = "0.0.15", default-features=false, features = ["bevy"]}

[dev-dependencies]
bevy = {version = "0.12", default-features = false, features = ["bevy_core_pipeline", "bevy_render", "bevy_asset", "bevy_pbr", "x11", "tonemapping_luts", "ktx2", "zstd"]}
bincode = "1.3.1"
env_logger = "0.10.0"
bevy = {version = "0.13", default-features = false, features = ["bevy_core_pipeline", "bevy_render", "bevy_asset", "bevy_pbr", "x11", "tonemapping_luts", "ktx2", "zstd"]}
bincode = "1.3"
env_logger = "0.11"
serde = {version = "1.0", features = ["derive"]}
1 change: 1 addition & 0 deletions bevy_renet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ If you want a more complex example you can checkout the [demo_bevy](https://gith

|bevy|bevy_renet|
|---|---|
|0.13|0.0.11|
|0.12|0.0.10|
|0.11|0.0.9|
|0.10|0.0.8|
Expand Down
28 changes: 14 additions & 14 deletions bevy_renet/examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::prelude::{shape::Plane, *};
use bevy::{prelude::*, render::mesh::PlaneMeshBuilder};
use bevy_renet::{
client_connected,
renet::{
Expand Down Expand Up @@ -107,7 +107,7 @@ fn main() {

app.add_systems(
Update,
(server_update_system, server_sync_players, move_players_system).run_if(resource_exists::<RenetServer>()),
(server_update_system, server_sync_players, move_players_system).run_if(resource_exists::<RenetServer>),
);
} else {
app.add_plugins(RenetClientPlugin);
Expand All @@ -119,7 +119,7 @@ fn main() {

app.add_systems(
Update,
(player_input, client_send_input, client_sync_players).run_if(client_connected()),
(player_input, client_send_input, client_sync_players).run_if(client_connected),
);
}

Expand All @@ -144,8 +144,8 @@ fn server_update_system(
// Spawn player cube
let player_entity = commands
.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
mesh: meshes.add(Cuboid::from_size(Vec3::splat(1.0))),
material: materials.add(Color::rgb(0.8, 0.7, 0.6)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
})
Expand Down Expand Up @@ -211,8 +211,8 @@ fn client_sync_players(
println!("Player {} connected.", id);
let player_entity = commands
.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
mesh: meshes.add(Cuboid::from_size(Vec3::splat(1.0))),
material: materials.add(Color::rgb(0.8, 0.7, 0.6)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
})
Expand Down Expand Up @@ -247,8 +247,8 @@ fn client_sync_players(
fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<StandardMaterial>>) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(Plane::from_size(5.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
mesh: meshes.add(Mesh::from(PlaneMeshBuilder::from_size(Vec2::splat(5.0)))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3)),
..Default::default()
});
// light
Expand All @@ -268,11 +268,11 @@ fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials
});
}

fn player_input(keyboard_input: Res<Input<KeyCode>>, mut player_input: ResMut<PlayerInput>) {
player_input.left = keyboard_input.pressed(KeyCode::A) || keyboard_input.pressed(KeyCode::Left);
player_input.right = keyboard_input.pressed(KeyCode::D) || keyboard_input.pressed(KeyCode::Right);
player_input.up = keyboard_input.pressed(KeyCode::W) || keyboard_input.pressed(KeyCode::Up);
player_input.down = keyboard_input.pressed(KeyCode::S) || keyboard_input.pressed(KeyCode::Down);
fn player_input(keyboard_input: Res<ButtonInput<KeyCode>>, mut player_input: ResMut<PlayerInput>) {
player_input.left = keyboard_input.pressed(KeyCode::KeyA) || keyboard_input.pressed(KeyCode::ArrowLeft);
player_input.right = keyboard_input.pressed(KeyCode::KeyD) || keyboard_input.pressed(KeyCode::ArrowRight);
player_input.up = keyboard_input.pressed(KeyCode::KeyW) || keyboard_input.pressed(KeyCode::ArrowUp);
player_input.down = keyboard_input.pressed(KeyCode::KeyS) || keyboard_input.pressed(KeyCode::ArrowDown);
}

fn client_send_input(player_input: Res<PlayerInput>, mut client: ResMut<RenetClient>) {
Expand Down
46 changes: 20 additions & 26 deletions bevy_renet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ use renet::{RenetClient, RenetServer, ServerEvent};
#[cfg(feature = "transport")]
pub mod transport;

#[cfg(feature = "steam")]
pub mod steam;

/// This system set is where all transports receive messages
///
/// If you want to ensure data has arrived in the [`RenetClient`] or [`RenetServer`], then schedule your
Expand All @@ -35,11 +32,12 @@ pub struct RenetClientPlugin;
impl Plugin for RenetServerPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<Events<ServerEvent>>();
app.add_systems(PreUpdate, Self::update_system.run_if(resource_exists::<RenetServer>()));
app.add_systems(PreUpdate, Self::update_system.run_if(resource_exists::<RenetServer>));
app.add_systems(
PreUpdate,
Self::emit_server_events_system
.run_if(resource_exists::<RenetServer>())
.in_set(RenetReceive)
.run_if(resource_exists::<RenetServer>)
.after(Self::update_system),
);
}
Expand All @@ -59,7 +57,7 @@ impl RenetServerPlugin {

impl Plugin for RenetClientPlugin {
fn build(&self, app: &mut App) {
app.add_systems(PreUpdate, Self::update_system.run_if(resource_exists::<RenetClient>()));
app.add_systems(PreUpdate, Self::update_system.run_if(resource_exists::<RenetClient>));
}
}

Expand All @@ -69,43 +67,39 @@ impl RenetClientPlugin {
}
}

pub fn client_connected() -> impl FnMut(Option<Res<RenetClient>>) -> bool {
|client| match client {
pub fn client_connected(client: Option<Res<RenetClient>>) -> bool {
match client {
Some(client) => client.is_connected(),
None => false,
}
}

pub fn client_disconnected() -> impl FnMut(Option<Res<RenetClient>>) -> bool {
|client| match client {
pub fn client_disconnected(client: Option<Res<RenetClient>>) -> bool {
match client {
Some(client) => client.is_disconnected(),
None => true,
}
}

pub fn client_connecting() -> impl FnMut(Option<Res<RenetClient>>) -> bool {
|client| match client {
pub fn client_connecting(client: Option<Res<RenetClient>>) -> bool {
match client {
Some(client) => client.is_connecting(),
None => false,
}
}

pub fn client_just_connected() -> impl FnMut(Local<bool>, Option<Res<RenetClient>>) -> bool {
|mut last_connected: Local<bool>, client| {
let connected = client.map(|client| client.is_connected()).unwrap_or(false);
pub fn client_just_connected(mut last_connected: Local<bool>, client: Option<Res<RenetClient>>) -> bool {
let connected = client.map(|client| client.is_connected()).unwrap_or(false);

let just_connected = !*last_connected && connected;
*last_connected = connected;
just_connected
}
let just_connected = !*last_connected && connected;
*last_connected = connected;
just_connected
}

pub fn client_just_disconnected() -> impl FnMut(Local<bool>, Option<Res<RenetClient>>) -> bool {
|mut last_connected: Local<bool>, client| {
let disconnected = client.map(|client| client.is_disconnected()).unwrap_or(true);
pub fn client_just_disconnected(mut last_connected: Local<bool>, client: Option<Res<RenetClient>>) -> bool {
let disconnected = client.map(|client| client.is_disconnected()).unwrap_or(true);

let just_disconnected = *last_connected && disconnected;
*last_connected = !disconnected;
just_disconnected
}
let just_disconnected = *last_connected && disconnected;
*last_connected = !disconnected;
just_disconnected
}
26 changes: 13 additions & 13 deletions bevy_renet/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ impl Plugin for NetcodeServerPlugin {
PreUpdate,
Self::update_system
.in_set(RenetReceive)
.run_if(resource_exists::<NetcodeServerTransport>())
.run_if(resource_exists::<RenetServer>())
.run_if(resource_exists::<NetcodeServerTransport>)
.run_if(resource_exists::<RenetServer>)
.after(RenetServerPlugin::update_system)
.before(RenetServerPlugin::emit_server_events_system),
);

app.add_systems(
PostUpdate,
(Self::send_packets.in_set(RenetSend), Self::disconnect_on_exit)
.run_if(resource_exists::<NetcodeServerTransport>())
.run_if(resource_exists::<RenetServer>()),
.run_if(resource_exists::<NetcodeServerTransport>)
.run_if(resource_exists::<RenetServer>),
);
}
}

impl NetcodeServerPlugin {
fn update_system(
pub fn update_system(
mut transport: ResMut<NetcodeServerTransport>,
mut server: ResMut<RenetServer>,
time: Res<Time>,
Expand All @@ -46,7 +46,7 @@ impl NetcodeServerPlugin {
}
}

fn send_packets(mut transport: ResMut<NetcodeServerTransport>, mut server: ResMut<RenetServer>) {
pub fn send_packets(mut transport: ResMut<NetcodeServerTransport>, mut server: ResMut<RenetServer>) {
transport.send_packets(&mut server);
}

Expand All @@ -65,21 +65,21 @@ impl Plugin for NetcodeClientPlugin {
PreUpdate,
Self::update_system
.in_set(RenetReceive)
.run_if(resource_exists::<NetcodeClientTransport>())
.run_if(resource_exists::<RenetClient>())
.run_if(resource_exists::<NetcodeClientTransport>)
.run_if(resource_exists::<RenetClient>)
.after(RenetClientPlugin::update_system),
);
app.add_systems(
PostUpdate,
(Self::send_packets.in_set(RenetSend), Self::disconnect_on_exit)
.run_if(resource_exists::<NetcodeClientTransport>())
.run_if(resource_exists::<RenetClient>()),
.run_if(resource_exists::<NetcodeClientTransport>)
.run_if(resource_exists::<RenetClient>),
);
}
}

impl NetcodeClientPlugin {
fn update_system(
pub fn update_system(
mut transport: ResMut<NetcodeClientTransport>,
mut client: ResMut<RenetClient>,
time: Res<Time>,
Expand All @@ -90,7 +90,7 @@ impl NetcodeClientPlugin {
}
}

fn send_packets(
pub fn send_packets(
mut transport: ResMut<NetcodeClientTransport>,
mut client: ResMut<RenetClient>,
mut transport_errors: EventWriter<NetcodeTransportError>,
Expand All @@ -100,7 +100,7 @@ impl NetcodeClientPlugin {
}
}

fn disconnect_on_exit(exit: EventReader<AppExit>, mut transport: ResMut<NetcodeClientTransport>) {
pub fn disconnect_on_exit(exit: EventReader<AppExit>, mut transport: ResMut<NetcodeClientTransport>) {
if !exit.is_empty() {
transport.disconnect();
}
Expand Down
25 changes: 17 additions & 8 deletions demo_bevy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@ path = "src/bin/server.rs"

[features]
transport = ["bevy_renet/transport"]
steam = ["bevy_renet/steam", "steamworks"]
steam = ["dep:renet_steam", "steamworks"]

[dependencies]
bevy = {version = "0.12", default-features = false, features = ["bevy_core_pipeline", "bevy_render", "bevy_asset", "bevy_pbr", "x11", "tonemapping_luts", "ktx2", "zstd"]}
bevy = { version = "0.13", default-features = false, features = [
"bevy_core_pipeline",
"bevy_render",
"bevy_asset",
"bevy_pbr",
"x11",
"tonemapping_luts",
"ktx2",
"zstd",
] }
bevy_renet = { path = "../bevy_renet", features = ["serde"] }
renet_steam = { path = "../renet_steam", features = ["bevy"], optional = true }
serde = { version = "1.0", features = ["derive"] }
bincode = "1.3.1"
bevy_egui = "0.23.0"
bincode = "1.3"
bevy_egui = "0.25"
renet_visualizer = { path = "../renet_visualizer", features = ["bevy"] }
smooth-bevy-cameras = "0.10"
fastrand = "2.0.0"

steamworks = { git = "https://github.com/Noxime/steamworks-rs", rev = "a4dfe2a", optional = true }
smooth-bevy-cameras = "0.11"
fastrand = "2.0"
steamworks = { version = "0.11", optional = true }
Loading

0 comments on commit a541506

Please sign in to comment.