Skip to content

Commit

Permalink
Don't kill contributors on window squish (#6675)
Browse files Browse the repository at this point in the history
# Objective

- The `contributors` example panics when attempting to generate an empty range if the window height is smaller than the sprites
- Don't do that

## Solution

- Clamp the bounce height to be 0 minimum, and generate an inclusive range when passing it to `rng.gen_range`
  • Loading branch information
Dorumin committed Nov 18, 2022
1 parent 9f51651 commit a02e44c
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions examples/games/contributors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ fn collision_system(
let wall_right = window.width() / 2.;

// The maximum height the birbs should try to reach is one birb below the top of the window.
let max_bounce_height = window.height() - SPRITE_SIZE * 2.0;
let max_bounce_height = (window.height() - SPRITE_SIZE * 2.0).max(0.0);

let mut rng = rand::thread_rng();

Expand All @@ -274,7 +274,7 @@ fn collision_system(
transform.translation.y = ground + SPRITE_SIZE / 2.0;

// How high this birb will bounce.
let bounce_height = rng.gen_range((max_bounce_height * 0.4)..max_bounce_height);
let bounce_height = rng.gen_range((max_bounce_height * 0.4)..=max_bounce_height);

// Apply the velocity that would bounce the birb up to bounce_height.
velocity.translation.y = (bounce_height * GRAVITY * 2.).sqrt();
Expand Down

0 comments on commit a02e44c

Please sign in to comment.