From d34c324faf3016c49bfa53ad253f8b02920fa3bd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Sun, 17 Jan 2021 18:58:49 +0100 Subject: [PATCH] Bolls bounce on walls --- src/core/game.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/core/game.rs b/src/core/game.rs index f938245..8474fab 100644 --- a/src/core/game.rs +++ b/src/core/game.rs @@ -238,7 +238,7 @@ pub struct Boll { } impl Object for Boll { - fn update(&mut self, _objects: &mut Objects, lvl: &Level, _dt: Duration) -> ObjectState { + fn update(&mut self, objects: &mut Objects, lvl: &Level, _dt: Duration) -> ObjectState { self.vel += lvl.gravity; self.pos += self.vel; @@ -252,6 +252,18 @@ impl Object for Boll { } } + if self.pos.x <= 0.0 || self.pos.x >= 1280.0 { // only for testing + self.pos.x = self.pos.x.max(0.0).min(1280.0); + self.vel.x = -self.vel.x; + self.bounces = 0; + use rand::distributions::{Distribution, Normal}; + let normal = Normal::new(0.5, 0.4); + objects.push(Box::new(Boll { + vel: self.vel * normal.sample(&mut rand::thread_rng()), + ..*self + })); + } + Alive } -- 2.11.0