diff options
Diffstat (limited to 'examples/breakout/client.rs')
-rw-r--r-- | examples/breakout/client.rs | 17 |
1 files changed, 14 insertions, 3 deletions
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( |