aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHenauxg <19689618+Henauxg@users.noreply.github.com>2022-11-03 18:11:37 +0100
committerHenauxg <19689618+Henauxg@users.noreply.github.com>2022-11-03 18:11:37 +0100
commit39faea4d08d006508842f8ceddecd9e4e3ad4d19 (patch)
treee6b7f25e38c328ce10c4ef602241adcd5af24ab2 /examples
parente104ce1299a64fb2b575a2193ec2151b48667165 (diff)
[example:breakout] Use owner_client_id to visualize balls ownership
Diffstat (limited to 'examples')
-rw-r--r--examples/breakout/breakout.rs2
-rw-r--r--examples/breakout/client.rs49
-rw-r--r--examples/breakout/protocol.rs2
-rw-r--r--examples/breakout/server.rs14
4 files changed, 55 insertions, 12 deletions
diff --git a/examples/breakout/breakout.rs b/examples/breakout/breakout.rs
index 894364a..761b636 100644
--- a/examples/breakout/breakout.rs
+++ b/examples/breakout/breakout.rs
@@ -6,7 +6,7 @@ use bevy_quinnet::{
client::QuinnetClientPlugin,
server::{QuinnetServerPlugin, Server},
};
-use client::{NetworkMapping, BACKGROUND_COLOR};
+use client::BACKGROUND_COLOR;
mod client;
mod protocol;
diff --git a/examples/breakout/client.rs b/examples/breakout/client.rs
index 4b757c4..0c9b2f9 100644
--- a/examples/breakout/client.rs
+++ b/examples/breakout/client.rs
@@ -30,7 +30,9 @@ const SCOREBOARD_TEXT_PADDING: Val = Val::Px(5.0);
pub(crate) const BACKGROUND_COLOR: Color = Color::rgb(0.9, 0.9, 0.9);
const PADDLE_COLOR: Color = Color::rgb(0.3, 0.3, 0.7);
-const BALL_COLOR: Color = Color::rgb(1.0, 0.5, 0.5);
+const OPPONENT_PADDLE_COLOR: Color = Color::rgb(1.0, 0.5, 0.5);
+const BALL_COLOR: Color = Color::rgb(0.35, 0.35, 0.6);
+const OPPONENT_BALL_COLOR: Color = Color::rgb(0.9, 0.6, 0.6);
const BRICK_COLOR: Color = Color::rgb(0.5, 0.5, 1.0);
const WALL_COLOR: Color = Color::rgb(0.8, 0.8, 0.8);
const TEXT_COLOR: Color = Color::rgb(0.5, 0.5, 1.0);
@@ -96,7 +98,7 @@ pub(crate) fn start_connection(client: ResMut<Client>) {
.unwrap();
}
-fn spawn_paddle(commands: &mut Commands, position: &Vec3) -> Entity {
+fn spawn_paddle(commands: &mut Commands, position: &Vec3, owned: bool) -> Entity {
commands
.spawn()
.insert_bundle(SpriteBundle {
@@ -106,7 +108,11 @@ fn spawn_paddle(commands: &mut Commands, position: &Vec3) -> Entity {
..default()
},
sprite: Sprite {
- color: PADDLE_COLOR,
+ color: if owned {
+ PADDLE_COLOR
+ } else {
+ OPPONENT_PADDLE_COLOR
+ },
..default()
},
..default()
@@ -115,7 +121,7 @@ fn spawn_paddle(commands: &mut Commands, position: &Vec3) -> Entity {
.id()
}
-fn spawn_ball(commands: &mut Commands, pos: &Vec3, direction: &Vec2) -> Entity {
+fn spawn_ball(commands: &mut Commands, pos: &Vec3, direction: &Vec2, owned: bool) -> Entity {
commands
.spawn()
.insert(Ball)
@@ -126,7 +132,7 @@ fn spawn_ball(commands: &mut Commands, pos: &Vec3, direction: &Vec2) -> Entity {
..default()
},
sprite: Sprite {
- color: BALL_COLOR,
+ color: ball_color_from_bool(owned),
..default()
},
..default()
@@ -179,8 +185,7 @@ pub(crate) fn handle_server_messages(
mut entity_mapping: ResMut<NetworkMapping>,
mut game_state: ResMut<State<GameState>>,
mut paddles: Query<&mut Transform, With<Paddle>>,
- mut balls: Query<(&mut Transform, &mut Velocity), (With<Ball>, Without<Paddle>)>,
- // mut bricks: Query<(&mut Transform, &mut Velocity, Entity, &Brick)>, // (With<Brick>, Without<Paddle>, Without<Ball>),
+ mut balls: Query<(&mut Transform, &mut Velocity, &mut Sprite), (With<Ball>, Without<Paddle>)>,
mut bricks: ResMut<BricksMapping>,
mut collision_events: EventWriter<CollisionEvent>,
) {
@@ -190,19 +195,29 @@ pub(crate) fn handle_server_messages(
client_data.self_id = client_id;
}
ServerMessage::SpawnPaddle {
- owner_client_id: client_id,
+ owner_client_id,
entity,
position,
} => {
- let paddle = spawn_paddle(&mut commands, &position);
+ let paddle = spawn_paddle(
+ &mut commands,
+ &position,
+ owner_client_id == client_data.self_id,
+ );
entity_mapping.map.insert(entity, paddle);
}
ServerMessage::SpawnBall {
+ owner_client_id,
entity,
position,
direction,
} => {
- let ball = spawn_ball(&mut commands, &position, &direction);
+ let ball = spawn_ball(
+ &mut commands,
+ &position,
+ &direction,
+ owner_client_id == client_data.self_id,
+ );
entity_mapping.map.insert(entity, ball);
}
ServerMessage::SpawnBricks {
@@ -220,15 +235,19 @@ pub(crate) fn handle_server_messages(
}
}
ServerMessage::BallCollided {
+ owner_client_id,
entity,
position,
velocity,
} => {
if let Some(local_ball) = entity_mapping.map.get(&entity) {
- if let Ok((mut ball_transform, mut ball_velocity)) = balls.get_mut(*local_ball)
+ if let Ok((mut ball_transform, mut ball_velocity, mut ball_sprite)) =
+ balls.get_mut(*local_ball)
{
ball_transform.translation = position;
ball_velocity.0 = velocity;
+ ball_sprite.color =
+ ball_color_from_bool(owner_client_id == client_data.self_id);
}
}
// Sends a collision event so that other systems can react to the collision
@@ -419,6 +438,14 @@ pub(crate) fn apply_velocity(mut query: Query<(&mut Transform, &Velocity), With<
}
}
+fn ball_color_from_bool(owned: bool) -> Color {
+ if owned {
+ BALL_COLOR
+ } else {
+ OPPONENT_BALL_COLOR
+ }
+}
+
impl WallBundle {
fn new(location: WallLocation) -> WallBundle {
WallBundle {
diff --git a/examples/breakout/protocol.rs b/examples/breakout/protocol.rs
index 92a0cfb..9cd4caa 100644
--- a/examples/breakout/protocol.rs
+++ b/examples/breakout/protocol.rs
@@ -30,6 +30,7 @@ pub(crate) enum ServerMessage {
position: Vec3,
},
SpawnBall {
+ owner_client_id: ClientId,
entity: Entity,
position: Vec3,
direction: Vec2,
@@ -45,6 +46,7 @@ pub(crate) enum ServerMessage {
brick_id: BrickId,
},
BallCollided {
+ owner_client_id: ClientId,
entity: Entity,
position: Vec3,
velocity: Vec2,
diff --git a/examples/breakout/server.rs b/examples/breakout/server.rs
index 8ab796a..9aa43f0 100644
--- a/examples/breakout/server.rs
+++ b/examples/breakout/server.rs
@@ -232,6 +232,7 @@ pub(crate) fn check_for_collisions(
server
.broadcast_message(ServerMessage::BallCollided {
+ owner_client_id: ball.last_hit_by,
entity: ball_entity,
position: ball_transform.translation,
velocity: ball_velocity.0,
@@ -250,6 +251,18 @@ pub(crate) fn apply_velocity(mut query: Query<(&mut Transform, &Velocity), With<
}
fn start_game(commands: &mut Commands, server: &mut ResMut<Server>, players: &ResMut<Players>) {
+ // Assign ids
+ for client_id in players.map.keys().into_iter() {
+ server
+ .send_message(
+ *client_id,
+ ServerMessage::InitClient {
+ client_id: *client_id,
+ },
+ )
+ .unwrap();
+ }
+
// Spawn paddles
for (position, client_id) in PADDLES_STARTING_POSITION
.iter()
@@ -279,6 +292,7 @@ fn start_game(commands: &mut Commands, server: &mut ResMut<Server>, players: &Re
.send_group_message(
players.map.keys().into_iter(),
ServerMessage::SpawnBall {
+ owner_client_id: *client_id,
entity: ball,
position: *position,
direction: *direction,