diff --git a/bevy_matchbox/examples/hello.rs b/bevy_matchbox/examples/hello.rs index a27f6c8e..8757e6cc 100644 --- a/bevy_matchbox/examples/hello.rs +++ b/bevy_matchbox/examples/hello.rs @@ -4,6 +4,8 @@ use bevy::{prelude::*, time::common_conditions::on_timer, utils::Duration}; use bevy_matchbox::prelude::*; +const CHANNEL_ID: usize = 0; + fn main() { App::new() .add_plugins(DefaultPlugins) @@ -21,22 +23,24 @@ fn start_socket(mut commands: Commands) { commands.insert_resource(socket); } -fn send_message(mut socket: ResMut>) { +fn send_message(mut socket: ResMut) { let peers: Vec<_> = socket.connected_peers().collect(); for peer in peers { let message = "Hello"; info!("Sending message: {message:?} to {peer}"); - socket.send(message.as_bytes().into(), peer); + socket + .channel_mut(CHANNEL_ID) + .send(message.as_bytes().into(), peer); } } -fn receive_messages(mut socket: ResMut>) { +fn receive_messages(mut socket: ResMut) { for (peer, state) in socket.update_peers() { info!("{peer}: {state:?}"); } - for (_id, message) in socket.receive() { + for (_id, message) in socket.channel_mut(CHANNEL_ID).receive() { match std::str::from_utf8(&message) { Ok(message) => info!("Received message: {message:?}"), Err(e) => error!("Failed to convert message to string: {e}"), diff --git a/bevy_matchbox/examples/hello_host.rs b/bevy_matchbox/examples/hello_host.rs index 27170b3f..895d53e8 100644 --- a/bevy_matchbox/examples/hello_host.rs +++ b/bevy_matchbox/examples/hello_host.rs @@ -60,22 +60,22 @@ fn start_host_socket(mut commands: Commands) { commands.insert_resource(socket); } -fn send_message(mut socket: ResMut>) { +fn send_message(mut socket: ResMut) { let peers: Vec<_> = socket.connected_peers().collect(); for peer in peers { let message = "Hello, I'm the host"; info!("Sending message: {message:?} to {peer}"); - socket.send(message.as_bytes().into(), peer); + socket.channel_mut(0).send(message.as_bytes().into(), peer); } } -fn receive_messages(mut socket: ResMut>) { +fn receive_messages(mut socket: ResMut) { for (peer, state) in socket.update_peers() { info!("{peer}: {state:?}"); } - for (_id, message) in socket.receive() { + for (_id, message) in socket.channel_mut(0).receive() { match std::str::from_utf8(&message) { Ok(message) => info!("Received message: {message:?}"), Err(e) => error!("Failed to convert message to string: {e}"), diff --git a/bevy_matchbox/src/lib.rs b/bevy_matchbox/src/lib.rs index a12dc796..4d64faf0 100644 --- a/bevy_matchbox/src/lib.rs +++ b/bevy_matchbox/src/lib.rs @@ -18,10 +18,7 @@ cfg_if! { pub mod prelude { pub use crate::{CloseSocketExt, MatchboxSocket, OpenSocketExt}; use cfg_if::cfg_if; - pub use matchbox_socket::{ - BuildablePlurality, ChannelConfig, MultipleChannels, PeerId, PeerState, SingleChannel, - WebRtcSocketBuilder, - }; + pub use matchbox_socket::{ChannelConfig, PeerId, PeerState, WebRtcSocketBuilder}; cfg_if! { if #[cfg(all(not(target_arch = "wasm32"), feature = "signaling"))] { diff --git a/bevy_matchbox/src/signaling.rs b/bevy_matchbox/src/signaling.rs index f1ab5796..9c1e2a11 100644 --- a/bevy_matchbox/src/signaling.rs +++ b/bevy_matchbox/src/signaling.rs @@ -163,8 +163,10 @@ impl MatchboxServer { #[cfg(test)] mod tests { - use crate::matchbox_signaling::topologies::client_server::{ClientServer, ClientServerState}; - use crate::prelude::*; + use crate::{ + matchbox_signaling::topologies::client_server::{ClientServer, ClientServerState}, + prelude::*, + }; use bevy::prelude::*; use std::net::Ipv4Addr; diff --git a/bevy_matchbox/src/socket.rs b/bevy_matchbox/src/socket.rs index d285f56f..e4e1c457 100644 --- a/bevy_matchbox/src/socket.rs +++ b/bevy_matchbox/src/socket.rs @@ -4,12 +4,9 @@ use bevy::{ tasks::IoTaskPool, }; pub use matchbox_socket; -use matchbox_socket::{ - BuildablePlurality, MessageLoopFuture, SingleChannel, WebRtcSocket, WebRtcSocketBuilder, -}; +use matchbox_socket::{MessageLoopFuture, WebRtcSocket, WebRtcSocketBuilder}; use std::{ fmt::Debug, - marker::PhantomData, ops::{Deref, DerefMut}, }; @@ -28,7 +25,7 @@ use std::{ /// /// fn close_socket_system( /// mut commands: Commands, -/// socket: Query>> +/// socket: Query> /// ) { /// let socket = socket.single(); /// commands.entity(socket).despawn(); @@ -46,7 +43,7 @@ use std::{ /// } /// /// fn close_socket_system(mut commands: Commands) { -/// commands.close_socket::(); +/// commands.close_socket(); /// } /// ``` /// @@ -58,7 +55,7 @@ use std::{ /// fn open_socket_system(mut commands: Commands) { /// let room_url = "wss://matchbox.example.com"; /// -/// let socket: MatchboxSocket = WebRtcSocketBuilder::new(room_url) +/// let socket: MatchboxSocket = WebRtcSocketBuilder::new(room_url) /// .add_channel(ChannelConfig::reliable()) /// .into(); /// @@ -66,35 +63,35 @@ use std::{ /// } /// /// fn close_socket_system(mut commands: Commands) { -/// commands.remove_resource::>(); +/// commands.remove_resource::(); /// } /// ``` #[derive(Resource, Component, Debug)] #[allow(dead_code)] // keep the task alive so it doesn't drop before the socket -pub struct MatchboxSocket(WebRtcSocket, Box); +pub struct MatchboxSocket(WebRtcSocket, Box); -impl Deref for MatchboxSocket { - type Target = WebRtcSocket; +impl Deref for MatchboxSocket { + type Target = WebRtcSocket; fn deref(&self) -> &Self::Target { &self.0 } } -impl DerefMut for MatchboxSocket { +impl DerefMut for MatchboxSocket { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } -impl From> for MatchboxSocket { - fn from(builder: WebRtcSocketBuilder) -> Self { +impl From for MatchboxSocket { + fn from(builder: WebRtcSocketBuilder) -> Self { Self::from(builder.build()) } } -impl From<(WebRtcSocket, MessageLoopFuture)> for MatchboxSocket { - fn from((socket, message_loop_fut): (WebRtcSocket, MessageLoopFuture)) -> Self { +impl From<(WebRtcSocket, MessageLoopFuture)> for MatchboxSocket { + fn from((socket, message_loop_fut): (WebRtcSocket, MessageLoopFuture)) -> Self { let task_pool = IoTaskPool::get(); let task = task_pool.spawn(message_loop_fut); MatchboxSocket(socket, Box::new(task)) @@ -102,32 +99,32 @@ impl From<(WebRtcSocket, MessageLoopFuture)> for Match } /// A [`Command`] used to open a [`MatchboxSocket`] and allocate it as a resource. -struct OpenSocket(WebRtcSocketBuilder); +struct OpenSocket(WebRtcSocketBuilder); -impl Command for OpenSocket { +impl Command for OpenSocket { fn apply(self, world: &mut World) { world.insert_resource(MatchboxSocket::from(self.0)); } } /// A [`Commands`] extension used to open a [`MatchboxSocket`] and allocate it as a resource. -pub trait OpenSocketExt { +pub trait OpenSocketExt { /// Opens a [`MatchboxSocket`] and allocates it as a resource. - fn open_socket(&mut self, socket_builder: WebRtcSocketBuilder); + fn open_socket(&mut self, socket_builder: WebRtcSocketBuilder); } -impl OpenSocketExt for Commands<'_, '_> { - fn open_socket(&mut self, socket_builder: WebRtcSocketBuilder) { +impl OpenSocketExt for Commands<'_, '_> { + fn open_socket(&mut self, socket_builder: WebRtcSocketBuilder) { self.add(OpenSocket(socket_builder)) } } /// A [`Command`] used to close a [`WebRtcSocket`], deleting the [`MatchboxSocket`] resource. -struct CloseSocket(PhantomData); +struct CloseSocket; -impl Command for CloseSocket { +impl Command for CloseSocket { fn apply(self, world: &mut World) { - world.remove_resource::>(); + world.remove_resource::(); } } @@ -135,16 +132,16 @@ impl Command for CloseSocket { /// resource. pub trait CloseSocketExt { /// Delete the [`MatchboxSocket`] resource. - fn close_socket(&mut self); + fn close_socket(&mut self); } impl CloseSocketExt for Commands<'_, '_> { - fn close_socket(&mut self) { - self.add(CloseSocket::(PhantomData)) + fn close_socket(&mut self) { + self.add(CloseSocket) } } -impl MatchboxSocket { +impl MatchboxSocket { /// Create a new socket with a single unreliable channel /// /// ```rust @@ -157,7 +154,7 @@ impl MatchboxSocket { /// commands.spawn(socket); /// } /// ``` - pub fn new_unreliable(room_url: impl Into) -> MatchboxSocket { + pub fn new_unreliable(room_url: impl Into) -> MatchboxSocket { Self::from(WebRtcSocket::new_unreliable(room_url)) } @@ -173,24 +170,7 @@ impl MatchboxSocket { /// commands.spawn(socket); /// } /// ``` - pub fn new_reliable(room_url: impl Into) -> MatchboxSocket { + pub fn new_reliable(room_url: impl Into) -> MatchboxSocket { Self::from(WebRtcSocket::new_reliable(room_url)) } - - /// Create a new socket with a single ggrs-compatible channel - /// - /// ```rust - /// use bevy_matchbox::prelude::*; - /// use bevy::prelude::*; - /// - /// fn open_channel_system(mut commands: Commands) { - /// let room_url = "wss://matchbox.example.com"; - /// let socket = MatchboxSocket::new_ggrs(room_url); - /// commands.spawn(socket); - /// } - /// ``` - #[cfg(feature = "ggrs")] - pub fn new_ggrs(room_url: impl Into) -> MatchboxSocket { - Self::from(WebRtcSocket::new_ggrs(room_url)) - } } diff --git a/examples/bevy_ggrs/src/box_game.rs b/examples/bevy_ggrs/src/box_game.rs index 3de7ca71..5aa5090c 100644 --- a/examples/bevy_ggrs/src/box_game.rs +++ b/examples/bevy_ggrs/src/box_game.rs @@ -24,8 +24,8 @@ const FRICTION: f32 = 0.0018; const PLANE_SIZE: f32 = 5.0; const CUBE_SIZE: f32 = 0.2; -// You need to define a config struct to bundle all the generics of GGRS. bevy_ggrs provides a sensible default in `GgrsConfig`. -// (optional) You can define a type here for brevity. +// You need to define a config struct to bundle all the generics of GGRS. bevy_ggrs provides a +// sensible default in `GgrsConfig`. (optional) You can define a type here for brevity. pub type BoxConfig = GgrsConfig; #[repr(C)] @@ -58,7 +58,8 @@ pub struct FrameCount { pub frame: u32, } -/// Collects player inputs during [`ReadInputs`](`bevy_ggrs::ReadInputs`) and creates a [`LocalInputs`] resource. +/// Collects player inputs during [`ReadInputs`](`bevy_ggrs::ReadInputs`) and creates a +/// [`LocalInputs`] resource. pub fn read_local_inputs( mut commands: Commands, keyboard_input: Res>, @@ -154,8 +155,9 @@ pub fn setup_scene( } // Example system, manipulating a resource, will be added to the rollback schedule. -// Increases the frame count by 1 every update step. If loading and saving resources works correctly, -// you should see this resource rolling back, counting back up and finally increasing by 1 every update step +// Increases the frame count by 1 every update step. If loading and saving resources works +// correctly, you should see this resource rolling back, counting back up and finally increasing by +// 1 every update step #[allow(dead_code)] pub fn increase_frame_system(mut frame_count: ResMut) { frame_count.frame += 1; @@ -167,7 +169,8 @@ pub fn increase_frame_system(mut frame_count: ResMut) { #[allow(dead_code)] pub fn move_cube_system( mut query: Query<(&mut Transform, &mut Velocity, &Player), With>, - // ^------^ Added by `add_rollback` earlier + // ^------^ Added by + // `add_rollback` earlier inputs: Res>, // Thanks to RollbackTimePlugin, this is rollback safe time: Res