diff options
-rw-r--r-- | examples/breakout/client.rs | 20 | ||||
-rw-r--r-- | examples/chat/client.rs | 37 |
2 files changed, 24 insertions, 33 deletions
diff --git a/examples/breakout/client.rs b/examples/breakout/client.rs index edb0cef..45f8bc2 100644 --- a/examples/breakout/client.rs +++ b/examples/breakout/client.rs @@ -15,9 +15,7 @@ use bevy::{ }, }; use bevy_quinnet::{ - client::{ - certificate::CertificateVerificationMode, Client, Connection, ConnectionConfiguration, - }, + client::{certificate::CertificateVerificationMode, Client, ConnectionConfiguration}, ClientId, }; @@ -92,9 +90,8 @@ struct WallBundle { sprite_bundle: SpriteBundle, } -pub(crate) fn start_connection(client: ResMut<Client>, mut commands: Commands) { - client.spawn_connection( - &mut commands, +pub(crate) fn start_connection(mut client: ResMut<Client>) { + client.open_connection( ConnectionConfiguration::new( SERVER_HOST.to_string(), SERVER_PORT, @@ -190,7 +187,7 @@ pub(crate) fn spawn_bricks( pub(crate) fn handle_server_messages( mut commands: Commands, - mut connection: Query<&mut Connection>, + mut client: ResMut<Client>, mut client_data: ResMut<ClientData>, mut entity_mapping: ResMut<NetworkMapping>, mut game_state: ResMut<State<GameState>>, @@ -200,8 +197,7 @@ pub(crate) fn handle_server_messages( mut scoreboard: ResMut<Scoreboard>, mut collision_events: EventWriter<CollisionEvent>, ) { - let mut connection = connection.single_mut(); - while let Ok(Some(message)) = connection.receive_message::<ServerMessage>() { + while let Ok(Some(message)) = client.connection_mut().receive_message::<ServerMessage>() { match message { ServerMessage::InitClient { client_id } => { client_data.self_id = client_id; @@ -287,7 +283,7 @@ pub(crate) struct PaddleState { } pub(crate) fn move_paddle( - mut connection: Query<&mut Connection>, + client: Res<Client>, keyboard_input: Res<Input<KeyCode>>, mut local: Local<PaddleState>, ) { @@ -302,8 +298,8 @@ pub(crate) fn move_paddle( } if local.current_input != paddle_input { - let connection = connection.single_mut(); - connection + client + .connection() .send_message(ClientMessage::PaddleInput { input: paddle_input.clone(), }) diff --git a/examples/chat/client.rs b/examples/chat/client.rs index 6e5005b..65f1e43 100644 --- a/examples/chat/client.rs +++ b/examples/chat/client.rs @@ -8,14 +8,14 @@ use bevy::{ app::{AppExit, ScheduleRunnerPlugin}, log::LogPlugin, prelude::{ - info, warn, App, Commands, CoreStage, Deref, DerefMut, EventReader, EventWriter, Query, + info, warn, App, Commands, CoreStage, Deref, DerefMut, EventReader, EventWriter, Res, ResMut, Resource, }, }; use bevy_quinnet::{ client::{ - certificate::CertificateVerificationMode, Client, Connection, ConnectionConfiguration, - ConnectionEvent, QuinnetClientPlugin, + certificate::CertificateVerificationMode, Client, ConnectionConfiguration, ConnectionEvent, + QuinnetClientPlugin, }, ClientId, }; @@ -35,10 +35,10 @@ struct Users { #[derive(Resource, Deref, DerefMut)] struct TerminalReceiver(mpsc::Receiver<String>); -pub fn on_app_exit(app_exit_events: EventReader<AppExit>, mut connection: Query<&Connection>) { +pub fn on_app_exit(app_exit_events: EventReader<AppExit>, client: Res<Client>) { if !app_exit_events.is_empty() { - let connection = connection.single_mut(); - connection + client + .connection() .send_message(ClientMessage::Disconnect {}) .unwrap(); // TODO Clean: event to let the async client send his last messages. @@ -46,9 +46,8 @@ pub fn on_app_exit(app_exit_events: EventReader<AppExit>, mut connection: Query< } } -fn handle_server_messages(mut users: ResMut<Users>, mut connection: Query<&mut Connection>) { - let mut connection = connection.single_mut(); - while let Ok(Some(message)) = connection.receive_message::<ServerMessage>() { +fn handle_server_messages(mut users: ResMut<Users>, mut client: ResMut<Client>) { + while let Ok(Some(message)) = client.connection_mut().receive_message::<ServerMessage>() { match message { ServerMessage::ClientConnected { client_id, @@ -87,14 +86,14 @@ fn handle_server_messages(mut users: ResMut<Users>, mut connection: Query<&mut C fn handle_terminal_messages( mut terminal_messages: ResMut<TerminalReceiver>, mut app_exit_events: EventWriter<AppExit>, - mut connection: Query<&Connection>, + client: Res<Client>, ) { - let connection = connection.single_mut(); while let Ok(message) = terminal_messages.try_recv() { if message == "quit" { app_exit_events.send(AppExit); } else { - connection + client + .connection() .send_message(ClientMessage::ChatMessage { message: message }) .expect("Failed to send chat message"); } @@ -115,9 +114,8 @@ fn start_terminal_listener(mut commands: Commands) { commands.insert_resource(TerminalReceiver(from_terminal_receiver)); } -fn start_connection(mut commands: Commands, client: ResMut<Client>) { - client.spawn_connection( - &mut commands, +fn start_connection(mut client: ResMut<Client>) { + client.open_connection( ConnectionConfiguration::new("127.0.0.1".to_string(), 6000, "0.0.0.0".to_string(), 0), CertificateVerificationMode::SkipVerification, ); @@ -125,10 +123,7 @@ fn start_connection(mut commands: Commands, client: ResMut<Client>) { // You can already send message(s) even before being connected, they will be buffered. In this example we will wait for a ConnectionEvent. } -fn handle_client_events( - connection_events: EventReader<ConnectionEvent>, - mut connection: Query<&Connection>, -) { +fn handle_client_events(connection_events: EventReader<ConnectionEvent>, client: ResMut<Client>) { if !connection_events.is_empty() { // We are connected let username: String = rand::thread_rng() @@ -140,8 +135,8 @@ fn handle_client_events( println!("--- Joining with name: {}", username); println!("--- Type 'quit' to disconnect"); - let connection = connection.single_mut(); - connection + client + .connection() .send_message(ClientMessage::Join { name: username }) .unwrap(); |