From 38249a4adbadcd733ecbf2c891fef1e13c861185 Mon Sep 17 00:00:00 2001 From: Thilo Behnke Date: Sun, 24 Apr 2022 20:36:51 +0200 Subject: [PATCH] compiling code (deactivated inter ball collisions for now) --- pong/src/collision.rs | 2 +- pong/src/game_field.rs | 29 +++++++++++++---------------- pong/src/game_object.rs | 10 +++++----- pong/src/geom.rs | 18 ++++++++++++++++++ 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/pong/src/collision.rs b/pong/src/collision.rs index 574ec59..201272c 100644 --- a/pong/src/collision.rs +++ b/pong/src/collision.rs @@ -10,7 +10,7 @@ pub mod collision { CollisionDetector {} } - pub fn detect_collisions(&self, objs: Vec<&Box>) -> Box { + pub fn detect_collisions(&self, objs: &Vec<&Box>) -> Box { if objs.is_empty() { return Box::new(Collisions::new(vec![])); } diff --git a/pong/src/game_field.rs b/pong/src/game_field.rs index 7561661..7a12a04 100644 --- a/pong/src/game_field.rs +++ b/pong/src/game_field.rs @@ -103,30 +103,27 @@ impl Field { ball.obj.update_pos() } - let mut objs: Vec> = vec![]; + let mut objs: Vec<&Box> = vec![]; objs.extend( self.players - .clone() - .into_iter() - .map(|p| p.obj) - .collect::>>(), - ); - objs.extend( - self.balls - .clone() - .into_iter() - .map(|b| b.obj) - .collect::>>(), + .iter() + .map(|p| &p.obj) + .collect::>>(), ); + // objs.extend( + // self.balls + // .iter() + // .map(|b| &b.obj) + // .collect::>>(), + // ); objs.extend( self.bounds.objs - .clone() - .into_iter() - .collect::>>() + .iter() + .collect::>>() ); let collision_detector = CollisionDetector::new(); let collision_handler = CollisionHandler::new(); - self.collisions = collision_detector.detect_collisions(objs.iter().collect()); + self.collisions = collision_detector.detect_collisions(&objs); for ball in self.balls.iter_mut() { let collisions = self.collisions.get_collisions_by_id(ball.obj.id()); diff --git a/pong/src/game_object.rs b/pong/src/game_object.rs index 833f7b0..827b4ce 100644 --- a/pong/src/game_object.rs +++ b/pong/src/game_object.rs @@ -48,11 +48,11 @@ pub mod game_object { } fn update_pos(&mut self) { - let vel = self.vel(); + let vel = self.vel().clone(); let center = self.geom.center_mut(); - center.add(vel); + center.add(&vel); // Keep last orientation if vel is now zero. - if *vel == Vector::zero() { + if vel == Vector::zero() { return; } let mut updated_orientation = vel.clone(); @@ -110,7 +110,7 @@ pub mod components { } fn orientation_mut(&mut self) -> &mut Vector { - &mut self.shape.orientation() + self.shape.orientation_mut() } fn center(&self) -> &Vector { @@ -118,7 +118,7 @@ pub mod components { } fn center_mut(&mut self) -> &mut Vector { - &mut self.shape.center() + self.shape.center_mut() } fn bounding_box(&self) -> BoundingBox { diff --git a/pong/src/geom.rs b/pong/src/geom.rs index 5a14219..2ab8e2c 100644 --- a/pong/src/geom.rs +++ b/pong/src/geom.rs @@ -250,7 +250,9 @@ pub mod shape { pub trait Shape : Debug { fn center(&self) -> &Vector; + fn center_mut(&mut self) -> &mut Vector; fn orientation(&self) -> &Vector; + fn orientation_mut(&mut self) -> &mut Vector; fn shape_type(&self) -> ShapeType; fn bounding_box(&self) -> BoundingBox; } @@ -276,10 +278,18 @@ pub mod shape { &self.center } + fn center_mut(&mut self) -> &mut Vector { + &mut self.center + } + fn orientation(&self) -> &Vector { &self.orientation } + fn orientation_mut(&mut self) -> &mut Vector { + &mut self.orientation + } + fn shape_type(&self) -> ShapeType { ShapeType::Rect } @@ -309,10 +319,18 @@ pub mod shape { &self.center } + fn center_mut(&mut self) -> &mut Vector { + &mut self.center + } + fn orientation(&self) -> &Vector { &self.orientation } + fn orientation_mut(&mut self) -> &mut Vector { + &mut self.orientation + } + fn shape_type(&self) -> ShapeType { ShapeType::Circle }