Skip to content

Commit

Permalink
BevyRenet: update to bevy 0.13 (#147)
Browse files Browse the repository at this point in the history
Also updates renet_visualizer egui to 0.26
  • Loading branch information
dgsantana authored Feb 21, 2024
1 parent 6e93d29 commit a5a8f8c
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 116 deletions.
12 changes: 6 additions & 6 deletions bevy_renet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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

Expand All @@ -20,11 +20,11 @@ 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"]}
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"]}
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 @@ -106,7 +106,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 @@ -118,7 +118,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 @@ -143,8 +143,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 @@ -210,8 +210,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 @@ -246,8 +246,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 @@ -267,11 +267,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
42 changes: 19 additions & 23 deletions bevy_renet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +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
.in_set(RenetReceive)
.run_if(resource_exists::<RenetServer>())
.run_if(resource_exists::<RenetServer>)
.after(Self::update_system),
);
}
Expand All @@ -57,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 @@ -67,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
}
16 changes: 8 additions & 8 deletions bevy_renet/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ 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>),
);
}
}
Expand Down Expand Up @@ -65,15 +65,15 @@ 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>),
);
}
}
Expand Down
19 changes: 14 additions & 5 deletions demo_bevy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ transport = ["bevy_renet/transport"]
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"
smooth-bevy-cameras = "0.11"
fastrand = "2.0"

steamworks = { git = "https://github.com/Noxime/steamworks-rs", rev = "a4dfe2a", optional = true }
42 changes: 15 additions & 27 deletions demo_bevy/src/bin/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::{shape::Icosphere, *},
prelude::*,
window::PrimaryWindow,
};
use bevy_egui::{EguiContexts, EguiPlugin};
Expand Down Expand Up @@ -161,7 +161,7 @@ fn update_visulizer_system(
mut visualizer: ResMut<RenetClientVisualizer<200>>,
client: Res<RenetClient>,
mut show_visualizer: Local<bool>,
keyboard_input: Res<Input<KeyCode>>,
keyboard_input: Res<ButtonInput<KeyCode>>,
) {
visualizer.add_network_info(client.network_info());
if keyboard_input.just_pressed(KeyCode::F1) {
Expand All @@ -173,16 +173,16 @@ fn update_visulizer_system(
}

fn player_input(
keyboard_input: Res<Input<KeyCode>>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut player_input: ResMut<PlayerInput>,
mouse_button_input: Res<Input<MouseButton>>,
mouse_button_input: Res<ButtonInput<MouseButton>>,
target_query: Query<&Transform, With<Target>>,
mut player_commands: EventWriter<PlayerCommand>,
) {
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);
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);

if mouse_button_input.just_pressed(MouseButton::Left) {
let target_transform = target_query.single();
Expand Down Expand Up @@ -221,8 +221,8 @@ fn client_sync_players(
ServerMessages::PlayerCreate { id, translation, entity } => {
println!("Player {} connected.", id);
let mut client_entity = commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Capsule::default())),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
mesh: meshes.add(Mesh::from(Capsule3d::default())),
material: materials.add(Color::rgb(0.8, 0.7, 0.6)),
transform: Transform::from_xyz(translation[0], translation[1], translation[2]),
..Default::default()
});
Expand Down Expand Up @@ -251,14 +251,8 @@ fn client_sync_players(
}
ServerMessages::SpawnProjectile { entity, translation } => {
let projectile_entity = commands.spawn(PbrBundle {
mesh: meshes.add(
Mesh::try_from(Icosphere {
radius: 0.1,
subdivisions: 5,
})
.unwrap(),
),
material: materials.add(Color::rgb(1.0, 0.0, 0.0).into()),
mesh: meshes.add(Mesh::from(Sphere::new(0.1))),
material: materials.add(Color::rgb(1.0, 0.0, 0.0)),
transform: Transform::from_translation(translation.into()),
..Default::default()
});
Expand Down Expand Up @@ -300,7 +294,7 @@ fn update_target_system(
let mut target_transform = target_query.single_mut();
if let Some(cursor_pos) = primary_window.single().cursor_position() {
if let Some(ray) = camera.viewport_to_world(camera_transform, cursor_pos) {
if let Some(distance) = ray.intersect_plane(Vec3::Y, Vec3::Y) {
if let Some(distance) = ray.intersect_plane(Vec3::Y, Plane3d::new(Vec3::Y)) {
target_transform.translation = ray.direction * distance + ray.origin;
}
}
Expand All @@ -326,14 +320,8 @@ fn setup_camera(mut commands: Commands) {
fn setup_target(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<StandardMaterial>>) {
commands
.spawn(PbrBundle {
mesh: meshes.add(
Mesh::try_from(Icosphere {
radius: 0.1,
subdivisions: 5,
})
.unwrap(),
),
material: materials.add(Color::rgb(1.0, 0.0, 0.0).into()),
mesh: meshes.add(Mesh::from(Sphere::new(0.1))),
material: materials.add(Color::rgb(1.0, 0.0, 0.0)),
transform: Transform::from_xyz(0.0, 0., 0.0),
..Default::default()
})
Expand Down
10 changes: 5 additions & 5 deletions demo_bevy/src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ fn server_update_system(
let transform = Transform::from_xyz((fastrand::f32() - 0.5) * 40., 0.51, (fastrand::f32() - 0.5) * 40.);
let player_entity = commands
.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Capsule::default())),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
mesh: meshes.add(Mesh::from(Capsule3d::default())),
material: materials.add(Color::rgb(0.8, 0.7, 0.6)),
transform,
..Default::default()
})
Expand Down Expand Up @@ -291,7 +291,7 @@ fn projectile_on_removal_system(mut server: ResMut<RenetServer>, mut removed_pro
}

fn spawn_bot(
keyboard_input: Res<Input<KeyCode>>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut lobby: ResMut<ServerLobby>,
Expand All @@ -306,8 +306,8 @@ fn spawn_bot(
let transform = Transform::from_xyz((fastrand::f32() - 0.5) * 40., 0.51, (fastrand::f32() - 0.5) * 40.);
let player_entity = commands
.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Capsule::default())),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
mesh: meshes.add(Mesh::from(Capsule3d::default())),
material: materials.add(Color::rgb(0.8, 0.7, 0.6)),
transform,
..Default::default()
})
Expand Down
Loading

0 comments on commit a5a8f8c

Please sign in to comment.