Skip to content

Commit

Permalink
Update to Bevy 0.15 (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: Hennadii Chernyshchyk <genaloner@gmail.com>
  • Loading branch information
matoous and Shatur authored Dec 25, 2024
1 parent 210b272 commit 474df96
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 249 deletions.
25 changes: 13 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_replicon_renet"
version = "0.5.1"
version = "0.6.0"
authors = [
"Hennadii Chernyshchyk <genaloner@gmail.com>",
"koe <ukoe@protonmail.com>",
Expand All @@ -25,39 +25,40 @@ rustdoc-args = ["-Zunstable-options", "--cfg", "docsrs"]
all-features = true

[dependencies]
bevy_replicon = { version = "0.28", default-features = false }
bevy_renet = { version = "0.0.12", default-features = false }
bevy = { version = "0.14", default-features = false }
bevy_replicon = { version = "0.29", default-features = false }
bevy_renet = { version = "1.0", default-features = false }
bevy = { version = "0.15", default-features = false }

[dev-dependencies]
serde = "1.0"
clap = { version = "4.1", features = ["derive"] }
bevy = { version = "0.14", default-features = false, features = [
bevy = { version = "0.15", default-features = false, features = [
"bevy_text",
"bevy_ui",
"bevy_gizmos",
"bevy_state",
"bevy_window",
"x11",
"default_font",
] }

[features]
default = ["client", "server", "renet_serde", "renet_transport"]
default = ["client", "server", "renet_netcode"]
server = ["bevy_replicon/server"]
client = ["bevy_replicon/client"]

# Re-exports of renet features
renet_serde = ["bevy_renet/serde"]
renet_transport = ["bevy_renet/transport"]
renet_netcode = ["bevy_renet/netcode"]
renet_steam = ["bevy_renet/steam"]

[[test]]
name = "transport"
required-features = ["server", "client", "renet_transport"]
name = "netcode"
required-features = ["server", "client", "renet_netcode"]

[[example]]
name = "simple_box"
required-features = ["server", "client", "renet_transport"]
required-features = ["server", "client", "renet_netcode"]

[[example]]
name = "tic_tac_toe"
required-features = ["server", "client", "renet_transport"]
required-features = ["server", "client", "renet_netcode"]
77 changes: 22 additions & 55 deletions examples/simple_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ use bevy::{
};
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 clap::Parser;
Expand Down Expand Up @@ -73,11 +71,7 @@ impl SimpleBoxPlugin {
) -> Result<(), Box<dyn Error>> {
match *cli {
Cli::SinglePlayer => {
commands.spawn(PlayerBundle::new(
ClientId::SERVER,
Vec2::ZERO,
GREEN.into(),
));
commands.spawn((Player(ClientId::SERVER), PlayerColor(GREEN.into())));
}
Cli::Server { port } => {
let server_channels_config = channels.get_server_configs();
Expand All @@ -103,19 +97,15 @@ 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()
..Default::default()
},
TextColor::WHITE,
));
commands.spawn(PlayerBundle::new(
ClientId::SERVER,
Vec2::ZERO,
GREEN.into(),
));
commands.spawn((Player(ClientId::SERVER), PlayerColor(GREEN.into())));
}
Cli::Client { port, ip } => {
let server_channels_config = channels.get_server_configs();
Expand All @@ -142,13 +132,13 @@ impl SimpleBoxPlugin {
commands.insert_resource(client);
commands.insert_resource(transport);

commands.spawn(TextBundle::from_section(
format!("Client: {client_id:?}"),
TextStyle {
commands.spawn((
Text(format!("Client: {client_id:?}")),
TextFont {
font_size: 30.0,
color: Color::WHITE,
..default()
},
TextColor::WHITE,
));
}
}
Expand All @@ -157,7 +147,7 @@ impl SimpleBoxPlugin {
}

fn spawn_camera(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);
}

/// Logs server events and spawns a new player whenever a client connects.
Expand All @@ -170,11 +160,7 @@ impl SimpleBoxPlugin {
let r = ((client_id.get() % 23) as f32) / 23.0;
let g = ((client_id.get() % 27) as f32) / 27.0;
let b = ((client_id.get() % 39) as f32) / 39.0;
commands.spawn(PlayerBundle::new(
*client_id,
Vec2::ZERO,
Color::srgb(r, g, b),
));
commands.spawn((Player(*client_id), PlayerColor(Color::srgb(r, g, b))));
}
ServerEvent::ClientDisconnected { client_id, reason } => {
info!("{client_id:?} disconnected: {reason}");
Expand All @@ -187,7 +173,6 @@ impl SimpleBoxPlugin {
for (position, color) in &players {
gizmos.rect(
Vec3::new(position.x, position.y, 0.0),
Quat::IDENTITY,
Vec2::ONE * 50.0,
color.0,
);
Expand Down Expand Up @@ -228,7 +213,7 @@ impl SimpleBoxPlugin {
info!("received event {event:?} from {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;
}
}
}
Expand Down Expand Up @@ -260,33 +245,15 @@ impl Default for Cli {
}
}

#[derive(Bundle)]
struct PlayerBundle {
player: Player,
position: PlayerPosition,
color: PlayerColor,
replicated: Replicated,
}

impl PlayerBundle {
fn new(client_id: ClientId, position: Vec2, color: Color) -> Self {
Self {
player: Player(client_id),
position: PlayerPosition(position),
color: PlayerColor(color),
replicated: Replicated,
}
}
}

/// Contains the client ID of a player.
/// Contains player ID.
#[derive(Component, Serialize, Deserialize)]
#[require(PlayerPosition, PlayerColor, Replicated)]
struct Player(ClientId);

#[derive(Component, Deserialize, Serialize, Deref, DerefMut)]
#[derive(Component, Deserialize, Serialize, Deref, DerefMut, Default)]
struct PlayerPosition(Vec2);

#[derive(Component, Deserialize, Serialize)]
#[derive(Component, Deserialize, Serialize, Default)]
struct PlayerColor(Color);

/// A movement event for the controlled box.
Expand Down
Loading

0 comments on commit 474df96

Please sign in to comment.