diff options
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | examples/breakout/server.rs | 31 |
2 files changed, 21 insertions, 12 deletions
@@ -300,6 +300,8 @@ This demo is a modification of the classic [Bevy breakout](https://bevyengine.or It hosts a local server from inside a client, instead of a dedicated headless server as in the chat demo. You can find a [server module](examples/breakout/server.rs), a [client module](examples/breakout/client.rs), a shared [protocol](examples/breakout/protocol.rs) and the [bevy app schedule](examples/breakout/breakout.rs). +It also makes uses of [`Channels`](#channels). The server broadcasts the paddle position every tick via the `PaddleMoved` message on an `Unreliable` channel, the `BrickDestroyed` and `BallCollided` events are emitted on an `UnorderedReliable` channel, while the game setup and start are using the default `OrdrerdReliable` channel. + Start two clients with `cargo run --example breakout`, "Host" on one and "Join" on the other.  diff --git a/examples/breakout/server.rs b/examples/breakout/server.rs index 4ad2538..4164f7e 100644 --- a/examples/breakout/server.rs +++ b/examples/breakout/server.rs @@ -12,7 +12,7 @@ use bevy_quinnet::{ server::{ certificate::CertificateRetrievalMode, ConnectionEvent, Server, ServerConfigurationData, }, - shared::ClientId, + shared::{channel::ChannelId, ClientId}, }; use crate::{ @@ -158,8 +158,9 @@ pub(crate) fn update_paddles( paddle_transform.translation.x = new_paddle_position.clamp(left_bound, right_bound); - server.endpoint().try_send_group_message( + server.endpoint().try_send_group_message_on( players.map.keys().into_iter(), + ChannelId::Unreliable, ServerMessage::PaddleMoved { entity: paddle_entity, position: paddle_transform.translation, @@ -198,10 +199,13 @@ pub(crate) fn check_for_collisions( if let Some(brick) = maybe_brick { commands.entity(collider_entity).despawn(); - endpoint.try_broadcast_message(ServerMessage::BrickDestroyed { - by_client_id: ball.last_hit_by, - brick_id: brick.0, - }); + endpoint.try_broadcast_message_on( + ChannelId::UnorderedReliable, + ServerMessage::BrickDestroyed { + by_client_id: ball.last_hit_by, + brick_id: brick.0, + }, + ); } // reflect the ball when it collides @@ -228,12 +232,15 @@ pub(crate) fn check_for_collisions( ball_velocity.y = -ball_velocity.y; } - endpoint.try_broadcast_message(ServerMessage::BallCollided { - owner_client_id: ball.last_hit_by, - entity: ball_entity, - position: ball_transform.translation, - velocity: ball_velocity.0, - }); + endpoint.try_broadcast_message_on( + ChannelId::UnorderedReliable, + ServerMessage::BallCollided { + owner_client_id: ball.last_hit_by, + entity: ball_entity, + position: ball_transform.translation, + velocity: ball_velocity.0, + }, + ); } } } |