diff options
-rw-r--r-- | examples/breakout/breakout.rs | 9 | ||||
-rw-r--r-- | examples/breakout/client.rs | 17 | ||||
-rw-r--r-- | examples/breakout/server.rs | 8 |
3 files changed, 17 insertions, 17 deletions
diff --git a/examples/breakout/breakout.rs b/examples/breakout/breakout.rs index 761b636..123817c 100644 --- a/examples/breakout/breakout.rs +++ b/examples/breakout/breakout.rs @@ -101,11 +101,6 @@ impl WallLocation { } } -// This resource tracks the game's score -struct Scoreboard { - score: usize, -} - fn main() { App::new() .add_plugins(DefaultPlugins) @@ -114,9 +109,9 @@ fn main() { .add_event::<CollisionEvent>() .add_state(GameState::MainMenu) // Resources - .insert_resource(Scoreboard { score: 0 }) .insert_resource(ClearColor(BACKGROUND_COLOR)) .insert_resource(server::Players::default()) + .insert_resource(client::Scoreboard { score: 0 }) .insert_resource(client::ClientData::default()) .insert_resource(client::NetworkMapping::default()) .insert_resource(client::BricksMapping::default()) @@ -189,8 +184,6 @@ fn main() { .with_system(server::update_paddles.before(server::check_for_collisions)) .with_system(server::apply_velocity.before(server::check_for_collisions)) .with_system(server::check_for_collisions), - // .with_system(play_collision_sound.after(check_for_collisions)) - // .with_system(update_scoreboard) ) .add_system(bevy::window::close_on_esc) .run(); diff --git a/examples/breakout/client.rs b/examples/breakout/client.rs index 0c9b2f9..31b001b 100644 --- a/examples/breakout/client.rs +++ b/examples/breakout/client.rs @@ -20,9 +20,8 @@ use bevy_quinnet::{ use crate::{ protocol::{ClientMessage, PaddleInput, ServerMessage}, - BrickId, CollisionEvent, CollisionSound, GameState, Score, Scoreboard, Velocity, WallLocation, - BALL_SIZE, BALL_SPEED, BRICK_SIZE, GAP_BETWEEN_BRICKS, PADDLE_SIZE, SERVER_HOST, SERVER_PORT, - TIME_STEP, + BrickId, CollisionEvent, CollisionSound, GameState, Score, Velocity, WallLocation, BALL_SIZE, + BALL_SPEED, BRICK_SIZE, GAP_BETWEEN_BRICKS, PADDLE_SIZE, SERVER_HOST, SERVER_PORT, TIME_STEP, }; const SCOREBOARD_FONT_SIZE: f32 = 40.0; @@ -61,6 +60,11 @@ pub struct BricksMapping { map: HashMap<BrickId, Entity>, } +// This resource tracks the game's score +pub(crate) struct Scoreboard { + pub(crate) score: i32, +} + #[derive(Component)] pub(crate) struct Paddle; @@ -187,6 +191,7 @@ pub(crate) fn handle_server_messages( mut paddles: Query<&mut Transform, With<Paddle>>, mut balls: Query<(&mut Transform, &mut Velocity, &mut Sprite), (With<Ball>, Without<Paddle>)>, mut bricks: ResMut<BricksMapping>, + mut scoreboard: ResMut<Scoreboard>, mut collision_events: EventWriter<CollisionEvent>, ) { while let Ok(Some(message)) = client.receive_message::<ServerMessage>() { @@ -230,6 +235,11 @@ pub(crate) fn handle_server_messages( by_client_id, brick_id, } => { + if by_client_id == client_data.self_id { + scoreboard.score += 1; + } else { + scoreboard.score -= 1; + } if let Some(brick_entity) = bricks.map.get(&brick_id) { commands.entity(*brick_entity).despawn(); } @@ -300,6 +310,7 @@ pub(crate) fn update_scoreboard( ) { let mut text = query.single_mut(); text.sections[1].value = scoreboard.score.to_string(); + text.sections[1].style.color = ball_color_from_bool(scoreboard.score >= 0); } pub(crate) fn play_collision_sound( diff --git a/examples/breakout/server.rs b/examples/breakout/server.rs index 9aa43f0..18ff58f 100644 --- a/examples/breakout/server.rs +++ b/examples/breakout/server.rs @@ -15,7 +15,7 @@ use bevy_quinnet::{ use crate::{ protocol::{ClientMessage, PaddleInput, ServerMessage}, - BrickId, Scoreboard, Velocity, WallLocation, BALL_SIZE, BALL_SPEED, BOTTOM_WALL, BRICK_SIZE, + BrickId, Velocity, WallLocation, BALL_SIZE, BALL_SPEED, BOTTOM_WALL, BRICK_SIZE, GAP_BETWEEN_BRICKS, GAP_BETWEEN_BRICKS_AND_SIDES, GAP_BETWEEN_PADDLE_AND_BRICKS, GAP_BETWEEN_PADDLE_AND_FLOOR, LEFT_WALL, PADDLE_PADDING, PADDLE_SIZE, PADDLE_SPEED, RIGHT_WALL, SERVER_HOST, SERVER_PORT, TIME_STEP, TOP_WALL, WALL_THICKNESS, @@ -46,7 +46,6 @@ const PADDLES_STARTING_POSITION: [Vec3; 2] = [ #[derive(Debug, Clone, Default)] pub(crate) struct Player { input: PaddleInput, - score: u64, } #[derive(Debug, Clone, Default)] @@ -117,7 +116,6 @@ pub(crate) fn handle_server_events( players.map.insert( client.id, Player { - score: 0, input: PaddleInput::None, }, ); @@ -172,7 +170,6 @@ pub(crate) fn update_paddles( pub(crate) fn check_for_collisions( mut commands: Commands, mut server: ResMut<Server>, - mut scoreboard: ResMut<Scoreboard>, mut ball_query: Query<(&mut Velocity, &Transform, Entity, &mut Ball)>, collider_query: Query<(Entity, &Transform, Option<&Brick>, Option<&Paddle>), With<Collider>>, ) { @@ -193,9 +190,8 @@ pub(crate) fn check_for_collisions( ball.last_hit_by = paddle.player_id; } - // Bricks should be despawned and increment the scoreboard on collision + // Bricks should be despawned on collision if let Some(brick) = maybe_brick { - scoreboard.score += 1; commands.entity(collider_entity).despawn(); server |