diff options
author | Henauxg <19689618+Henauxg@users.noreply.github.com> | 2022-11-03 15:06:44 +0100 |
---|---|---|
committer | Henauxg <19689618+Henauxg@users.noreply.github.com> | 2022-11-03 15:06:44 +0100 |
commit | d2b244966e1ccdad8e8a025b3cf3c65fd8520f20 (patch) | |
tree | 75c9e625f8bc77e1d1d5b5bd180ea5684f3e47f6 /examples | |
parent | 6708e7c4bd6d7c43788549c30e52624e0b77346d (diff) |
[example:breakout] Networked paddle movement
Diffstat (limited to 'examples')
-rw-r--r-- | examples/breakout/breakout.rs | 51 | ||||
-rw-r--r-- | examples/breakout/client.rs | 15 | ||||
-rw-r--r-- | examples/breakout/server.rs | 2 |
3 files changed, 37 insertions, 31 deletions
diff --git a/examples/breakout/breakout.rs b/examples/breakout/breakout.rs index f6fd3ff..1ee350b 100644 --- a/examples/breakout/breakout.rs +++ b/examples/breakout/breakout.rs @@ -67,10 +67,10 @@ fn main() { .add_plugin(QuinnetClientPlugin::default()) .add_event::<CollisionEvent>() .add_state(GameState::MainMenu) + // Resources .insert_resource(Scoreboard { score: 0 }) .insert_resource(ClearColor(BACKGROUND_COLOR)) - // Resources - .insert_resource(server::Players::default()) //TODO Move ? + .insert_resource(server::Players::default()) .insert_resource(client::ClientData::default()) .insert_resource(NetworkMapping::default()) // Main menu @@ -94,7 +94,8 @@ fn main() { .add_system_set( SystemSet::on_update(GameState::JoiningLobby).with_system(handle_server_messages), ) - // Running the game + // Running the game. + // Every app is a client .add_system_set(SystemSet::on_enter(GameState::Running).with_system(setup_breakout)) .add_system_set( SystemSet::new() @@ -105,33 +106,36 @@ fn main() { _ => ShouldRun::No, }, )) - .with_system(check_for_collisions) - .with_system(move_paddle.before(check_for_collisions)) - .with_system(apply_velocity.before(check_for_collisions)) - .with_system(play_collision_sound.after(check_for_collisions)) + .with_system(handle_server_messages) + // .with_system(check_for_collisions) + // .with_system(move_paddle.before(check_for_collisions)) + .with_system(move_paddle) + // .with_system(apply_velocity.before(check_for_collisions)) + // .with_system(play_collision_sound.after(check_for_collisions)) .with_system(update_scoreboard), ) - // Every app is a client - .add_system_set( - SystemSet::on_update(GameState::Running).with_system(handle_server_messages), - ) // But hosting apps are also a server .add_system_set( SystemSet::new() // https://github.com/bevyengine/bevy/issues/1839 - .with_run_criteria(run_if_host.chain( - |In(input): In<ShouldRun>, state: Res<State<GameState>>| match state.current() { - GameState::Running => input, + // Run on a fixed Timestep, only for the server, in GameState::Running + .with_run_criteria(FixedTimestep::step(TIME_STEP as f64).chain( + |In(input): In<ShouldRun>, + state: Res<State<GameState>>, + server: Res<Server>| match state.current() { + GameState::Running => match server.is_listening() { + true => input, + false => ShouldRun::Yes, + }, _ => ShouldRun::No, }, )) - // .with_system(check_for_collisions) + .with_system(handle_client_messages.before(update_paddles)) .with_system(update_paddles.before(check_for_collisions)) - // .with_system(apply_velocity.before(check_for_collisions)) - // .with_system(play_collision_sound.after(check_for_collisions)) - // .with_system(update_scoreboard) - .with_system(handle_client_messages) - .with_system(handle_server_events), + .with_system(check_for_collisions), + // .with_system(apply_velocity.before(check_for_collisions)) + // .with_system(play_collision_sound.after(check_for_collisions)) + // .with_system(update_scoreboard) ) .add_system(bevy::window::close_on_esc) .run(); @@ -198,13 +202,6 @@ struct Scoreboard { score: usize, } -fn run_if_host(server: Res<Server>) -> ShouldRun { - match server.is_listening() { - true => ShouldRun::No, - false => ShouldRun::Yes, - } -} - fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>) { for (mut transform, velocity) in &mut query { transform.translation.x += velocity.x * TIME_STEP; diff --git a/examples/breakout/client.rs b/examples/breakout/client.rs index e45c035..1596287 100644 --- a/examples/breakout/client.rs +++ b/examples/breakout/client.rs @@ -55,9 +55,12 @@ pub(crate) struct NetworkMapping { map: HashMap<Entity, Entity>, } +#[derive(Component)] +pub(crate) struct Paddle; + /// The buttons in the main menu. #[derive(Clone, Copy, Component)] -pub enum MenuItem { +pub(crate) enum MenuItem { Host, Join, } @@ -98,6 +101,7 @@ fn spawn_paddle(commands: &mut Commands, position: &Vec3) -> Entity { ..default() }) .insert(Collider) + .insert(Paddle) .id() } @@ -127,6 +131,7 @@ pub(crate) fn handle_server_messages( mut client_data: ResMut<ClientData>, mut entity_mapping: ResMut<NetworkMapping>, mut game_state: ResMut<State<GameState>>, + mut paddles: Query<&mut Transform, With<Paddle>>, ) { while let Ok(Some(message)) = client.receive_message::<ServerMessage>() { match message { @@ -152,7 +157,13 @@ pub(crate) fn handle_server_messages( ServerMessage::StartGame {} => game_state.set(GameState::Running).unwrap(), ServerMessage::BrickDestroyed { client_id } => todo!(), ServerMessage::BallPosition { entity, position } => todo!(), - ServerMessage::PaddlePosition { entity, position } => todo!(), + ServerMessage::PaddlePosition { entity, position } => { + if let Some(local_paddle) = entity_mapping.map.get(&entity) { + if let Ok(mut paddle_transform) = paddles.get_mut(*local_paddle) { + paddle_transform.translation = position; + } + } + } } } } diff --git a/examples/breakout/server.rs b/examples/breakout/server.rs index 556fc4b..8d79523 100644 --- a/examples/breakout/server.rs +++ b/examples/breakout/server.rs @@ -42,7 +42,6 @@ const PADDLES_STARTING_POSITION: [Vec3; 2] = [ #[derive(Debug, Clone, Default)] pub(crate) struct Player { - // paddle: Option<Entity>, input: PaddleInput, score: u64, } @@ -108,7 +107,6 @@ pub(crate) fn handle_server_events( pub(crate) fn update_paddles( mut server: ResMut<Server>, players: ResMut<Players>, - // mut query: Query<&mut Transform, With<Paddle>>, mut paddles: Query<(&mut Transform, &Paddle, Entity)>, ) { for (mut paddle_transform, paddle, paddle_entity) in paddles.iter_mut() { |