aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHenauxg <19689618+Henauxg@users.noreply.github.com>2022-11-03 16:37:26 +0100
committerHenauxg <19689618+Henauxg@users.noreply.github.com>2022-11-03 16:37:26 +0100
commit4103125aa0f3ad3368ccf97fbdae5eedbe6e7233 (patch)
treea8633a116f21b07216f2e8251cc659464ec683cb /examples
parent1a2ff09fc27f10a779aac875a55dbdd0d8791b96 (diff)
[example:breakout] Spawn bricks at the right place
Diffstat (limited to 'examples')
-rw-r--r--examples/breakout/breakout.rs3
-rw-r--r--examples/breakout/client.rs15
2 files changed, 12 insertions, 6 deletions
diff --git a/examples/breakout/breakout.rs b/examples/breakout/breakout.rs
index 1511037..1183dbb 100644
--- a/examples/breakout/breakout.rs
+++ b/examples/breakout/breakout.rs
@@ -36,10 +36,9 @@ const TOP_WALL: f32 = 300.;
const BRICK_SIZE: Vec2 = Vec2::new(100., 30.);
// These values are exact
-const GAP_BETWEEN_PADDLE_AND_BRICKS: f32 = 270.0;
+const GAP_BETWEEN_PADDLE_AND_BRICKS: f32 = 140.0;
const GAP_BETWEEN_BRICKS: f32 = 5.0;
// These values are lower bounds, as the number of bricks is computed
-const GAP_BETWEEN_BRICKS_AND_CEILING: f32 = 20.0;
const GAP_BETWEEN_BRICKS_AND_SIDES: f32 = 20.0;
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
diff --git a/examples/breakout/client.rs b/examples/breakout/client.rs
index ff55257..a7b549b 100644
--- a/examples/breakout/client.rs
+++ b/examples/breakout/client.rs
@@ -363,14 +363,19 @@ pub(crate) fn setup_breakout(mut commands: Commands, asset_server: Res<AssetServ
let total_width_of_bricks = (RIGHT_WALL - LEFT_WALL) - 2. * GAP_BETWEEN_BRICKS_AND_SIDES;
let bottom_edge_of_bricks =
BOTTOM_WALL + GAP_BETWEEN_PADDLE_AND_FLOOR + GAP_BETWEEN_PADDLE_AND_BRICKS;
- let total_height_of_bricks = TOP_WALL - bottom_edge_of_bricks - GAP_BETWEEN_BRICKS_AND_CEILING;
+ let available_height_for_bricks = TOP_WALL
+ - bottom_edge_of_bricks
+ - (GAP_BETWEEN_PADDLE_AND_FLOOR + GAP_BETWEEN_PADDLE_AND_BRICKS);
assert!(total_width_of_bricks > 0.0);
- assert!(total_height_of_bricks > 0.0);
+ assert!(available_height_for_bricks > 0.0);
// Given the space available, compute how many rows and columns of bricks we can fit
let n_columns = (total_width_of_bricks / (BRICK_SIZE.x + GAP_BETWEEN_BRICKS)).floor() as usize;
- let n_rows = (total_height_of_bricks / (BRICK_SIZE.y + GAP_BETWEEN_BRICKS)).floor() as usize;
+ let n_rows =
+ (available_height_for_bricks / (BRICK_SIZE.y + GAP_BETWEEN_BRICKS)).floor() as usize;
+ let height_occupied_by_bricks =
+ n_rows as f32 * (BRICK_SIZE.y + GAP_BETWEEN_BRICKS) - GAP_BETWEEN_BRICKS;
let n_vertical_gaps = n_columns - 1;
// Because we need to round the number of columns,
@@ -385,7 +390,9 @@ pub(crate) fn setup_breakout(mut commands: Commands, asset_server: Res<AssetServ
// In Bevy, the `translation` of an entity describes the center point,
// not its bottom-left corner
let offset_x = left_edge_of_bricks + BRICK_SIZE.x / 2.;
- let offset_y = bottom_edge_of_bricks + BRICK_SIZE.y / 2.;
+ let offset_y = bottom_edge_of_bricks
+ + BRICK_SIZE.y / 2.
+ + (available_height_for_bricks - height_occupied_by_bricks) / 2.; // Offset so that both players are at an equal distance of the bricks
for row in 0..n_rows {
for column in 0..n_columns {