From a02e44c0dbea5d343a27eef614c803a3daf2c2f5 Mon Sep 17 00:00:00 2001 From: Doru Date: Fri, 18 Nov 2022 11:24:07 +0000 Subject: [PATCH] Don't kill contributors on window squish (#6675) # 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` --- examples/games/contributors.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/games/contributors.rs b/examples/games/contributors.rs index b7f74ecce1..8f452112ac 100644 --- a/examples/games/contributors.rs +++ b/examples/games/contributors.rs @@ -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(); @@ -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();