diff --git a/pong/src/game_field.rs b/pong/src/game_field.rs index 7a12a04..a292cb8 100644 --- a/pong/src/game_field.rs +++ b/pong/src/game_field.rs @@ -2,7 +2,7 @@ use crate::collision::collision::{Collision, CollisionDetector, CollisionHandler use crate::game_object::components::{DefaultGeomComp, DefaultPhysicsComp}; use crate::game_object::game_object::{DefaultGameObject, GameObject}; use crate::geom::geom::Vector; -use crate::geom::shape::{Circle, Rect}; +use crate::geom::shape::{Shape, ShapeType}; use crate::utils::utils::{Logger, NoopLogger}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -170,7 +170,7 @@ impl Player { obj: Box::new(DefaultGameObject::new( id, Box::new(DefaultGeomComp::new( - Box::new(Rect::new(Vector { x: x as f64, y: y as f64 }, Vector::new(0., 1.), (field.width as f64) / 25., (field.height as f64) / 5.)) + Shape::rect(Vector { x: x as f64, y: y as f64 }, Vector::new(0., 1.), (field.width as f64) / 25., (field.height as f64) / 5.) )), Box::new(DefaultPhysicsComp::new( Vector::zero(), @@ -193,8 +193,8 @@ impl Ball { obj: Box::new(DefaultGameObject::new( id, Box::new(DefaultGeomComp::new( - Box::new(Circle::new(Vector { x: x as f64, y: y as f64 }, Vector::zero(), (field.width as f64) / 80.) - ))), + Shape::circle(Vector { x: x as f64, y: y as f64 }, Vector::zero(), (field.width as f64) / 80.) + )), Box::new(DefaultPhysicsComp::new( Vector::zero(), false @@ -217,32 +217,32 @@ impl Bounds { Box::new(DefaultGameObject::new( 90, Box::new(DefaultGeomComp::new( - Box::new(Rect::new(Vector {x: (width / 2) as f64, y: 0 as f64}, Vector::new(1., 0.), width as f64, 2.) - ))), + Shape::rect(Vector {x: (width / 2) as f64, y: 0 as f64}, Vector::new(1., 0.), width as f64, 2.) + )), Box::new(DefaultPhysicsComp::new_static()) )), // bottom Box::new(DefaultGameObject::new( 91, Box::new(DefaultGeomComp::new( - Box::new(Rect::new(Vector {x: (width / 2) as f64, y: height as f64}, Vector::new(-1., 0.), width as f64, 2.) - ))), + Shape::rect(Vector {x: (width / 2) as f64, y: height as f64}, Vector::new(-1., 0.), width as f64, 2.) + )), Box::new(DefaultPhysicsComp::new_static()) )), // left Box::new(DefaultGameObject::new( 92, Box::new(DefaultGeomComp::new( - Box::new(Rect::new(Vector {x: 0 as f64, y: (height / 2) as f64}, Vector::new(0., 1.), 2., height as f64) - ))), + Shape::rect(Vector {x: 0 as f64, y: (height / 2) as f64}, Vector::new(0., 1.), 2., height as f64) + )), Box::new(DefaultPhysicsComp::new_static()) )), // right Box::new(DefaultGameObject::new( 93, Box::new(DefaultGeomComp::new( - Box::new(Rect::new(Vector {x: width as f64, y: (height / 2) as f64}, Vector::new(0., -1.), 2., height as f64) - ))), + Shape::rect(Vector {x: width as f64, y: (height / 2) as f64}, Vector::new(0., -1.), 2., height as f64) + )), Box::new(DefaultPhysicsComp::new_static()) )) ], diff --git a/pong/src/game_object.rs b/pong/src/game_object.rs index b26c44a..16e4878 100644 --- a/pong/src/game_object.rs +++ b/pong/src/game_object.rs @@ -6,7 +6,7 @@ pub mod game_object { pub trait GameObject : Debug { fn id(&self) -> u16; - fn shape(&self) -> &Box; + fn shape(&self) -> &ShapeType; fn pos(&self) -> &Vector; fn pos_mut(&mut self) -> &mut Vector; fn orientation(&self) -> &Vector; @@ -36,7 +36,7 @@ pub mod game_object { self.id } - fn shape(&self) -> &Box { + fn shape(&self) -> &ShapeType { self.geom.shape() } @@ -88,10 +88,10 @@ pub mod game_object { pub mod components { use std::fmt::Debug; use crate::geom::geom::{BoundingBox, Vector}; - use crate::geom::shape::Shape; + use crate::geom::shape::{get_bounding_box, get_center, get_center_mut, get_orientation, get_orientation_mut, Shape, ShapeType}; pub trait GeomComp : Debug { - fn shape(&self) -> &Box; + fn shape(&self) -> &ShapeType; fn orientation(&self) -> &Vector; fn orientation_mut(&mut self) -> &mut Vector; fn center(&self) -> &Vector; @@ -101,38 +101,38 @@ pub mod components { #[derive(Debug)] pub struct DefaultGeomComp { - shape: Box + shape: ShapeType } impl DefaultGeomComp { - pub fn new(shape: Box) -> DefaultGeomComp { + pub fn new(shape: ShapeType) -> DefaultGeomComp { DefaultGeomComp {shape} } } impl GeomComp for DefaultGeomComp { - fn shape(&self) -> &Box { + fn shape(&self) -> &ShapeType { &self.shape } fn orientation(&self) -> &Vector { - &self.shape.orientation() + get_orientation(&self.shape) } fn orientation_mut(&mut self) -> &mut Vector { - self.shape.orientation_mut() + get_orientation_mut(&mut self.shape) } fn center(&self) -> &Vector { - &self.shape.center() + get_center(&self.shape) } fn center_mut(&mut self) -> &mut Vector { - self.shape.center_mut() + get_center_mut(&mut self.shape) } fn bounding_box(&self) -> BoundingBox { - self.shape.bounding_box() + get_bounding_box(&self.shape) } } diff --git a/pong/src/geom.rs b/pong/src/geom.rs index c30f6bc..20ca72d 100644 --- a/pong/src/geom.rs +++ b/pong/src/geom.rs @@ -245,36 +245,29 @@ pub mod shape { #[derive(Clone, Debug, PartialEq)] pub enum ShapeType { - Rect = 0, - Circle = 1, + Rect(Shape, f64, f64), + Circle(Shape, f64), } - 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; - } - - #[derive(Debug)] - pub struct Rect { - pub width: f64, - pub height: f64, + #[derive(Clone, Debug, PartialEq)] + pub struct Shape { center: Vector, orientation: Vector } - impl Rect { - pub fn new(center: Vector, orientation: Vector, width: f64, height: f64) -> Rect { - Rect { - center, orientation, width, height - } + impl Shape { + pub fn rect(center: Vector, orientation: Vector, width: f64, height: f64) -> ShapeType { + ShapeType::Rect(Shape { + center, orientation + }, width, height) + } + + pub fn circle(center: Vector, orientation: Vector, radius: f64) -> ShapeType { + ShapeType::Circle(Shape { + center, orientation + }, radius) } - } - impl Shape for Rect { fn center(&self) -> &Vector { &self.center } @@ -290,54 +283,40 @@ pub mod shape { fn orientation_mut(&mut self) -> &mut Vector { &mut self.orientation } + } - fn shape_type(&self) -> ShapeType { - ShapeType::Rect - } - - fn bounding_box(&self) -> BoundingBox { - BoundingBox::create(self.center(), self.width, self.height) + pub fn get_center(shape: &ShapeType) -> &Vector { + match shape { + ShapeType::Rect(ref s, _, _) => &s.center, + ShapeType::Circle(ref s, _) => &s.center } } - #[derive(Debug)] - pub struct Circle { - pub radius: f64, - center: Vector, - orientation: Vector, - } - - impl Circle { - pub fn new(center: Vector, orientation: Vector, radius: f64) -> Circle { - Circle { - center, orientation, radius - } + pub fn get_center_mut(shape: &mut ShapeType) -> &mut Vector { + match shape { + ShapeType::Rect(ref mut s, _, _) => &mut s.center, + ShapeType::Circle(ref mut s, _) => &mut s.center } } - impl Shape for Circle { - fn center(&self) -> &Vector { - &self.center + pub fn get_orientation(shape: &ShapeType) -> &Vector { + match shape { + ShapeType::Rect(s, _, _) => &s.orientation, + ShapeType::Circle(s, _) => &s.orientation } + } - fn center_mut(&mut self) -> &mut Vector { - &mut self.center + pub fn get_orientation_mut(shape: &mut ShapeType) -> &mut Vector { + match shape { + ShapeType::Rect(ref mut s, _, _) => &mut s.orientation, + ShapeType::Circle(ref mut s, _) => &mut s.orientation } + } - fn orientation(&self) -> &Vector { - &self.orientation - } - - fn orientation_mut(&mut self) -> &mut Vector { - &mut self.orientation - } - - fn shape_type(&self) -> ShapeType { - ShapeType::Circle - } - - fn bounding_box(&self) -> BoundingBox { - BoundingBox::create(&self.center(), self.radius * 2., self.radius * 2.) + pub fn get_bounding_box(shape: &ShapeType) -> BoundingBox { + match shape { + ShapeType::Rect(s, width, height) => BoundingBox::create(&s.center, *width, *height), + ShapeType::Circle(s, radius) => BoundingBox::create(&s.center, *radius * 2., *radius * 2.) } } } diff --git a/pong/tests/collision_handler_test.rs b/pong/tests/collision_handler_test.rs index 4a30153..acdb243 100644 --- a/pong/tests/collision_handler_test.rs +++ b/pong/tests/collision_handler_test.rs @@ -3,7 +3,7 @@ use pong::collision::collision::CollisionHandler; use pong::game_object::components::{DefaultGeomComp, DefaultPhysicsComp}; use pong::game_object::game_object::{DefaultGameObject, GameObject}; use pong::geom::geom::Vector; -use pong::geom::shape::Rect; +use pong::geom::shape::Shape; #[rstest] #[case( @@ -80,9 +80,8 @@ fn create_game_obj(id: u16, vel: Vector, orientation: Vector, is_static: bool) - Box::new(DefaultGameObject::new( id, Box::new(DefaultGeomComp::new( - Box::new( - Rect::new(Vector::zero(), orientation, 20., 20.) - ))), + Shape::rect(Vector::zero(), orientation, 20., 20.) + )), Box::new(DefaultPhysicsComp::new( vel, is_static )) diff --git a/pong/tests/collision_tests.rs b/pong/tests/collision_tests.rs index bca0779..07548af 100644 --- a/pong/tests/collision_tests.rs +++ b/pong/tests/collision_tests.rs @@ -2,6 +2,7 @@ use rstest::rstest; use pong::collision::collision::{Collision, CollisionDetector}; use pong::game_object::game_object::GameObject; use pong::geom::geom::{BoundingBox, Vector}; +use pong::geom::shape::ShapeType; #[rstest] @@ -68,19 +69,24 @@ impl GameObject for MockGameObject { self.id } + fn shape(&self) -> &ShapeType { + todo!() + } + fn pos(&self) -> &Vector { - &self.zero_vec + todo!() } fn pos_mut(&mut self) -> &mut Vector { - &mut self.zero_vec + todo!() } fn orientation(&self) -> &Vector { - &self.zero_vec + todo!() } fn update_pos(&mut self) { + todo!() } fn bounding_box(&self) -> BoundingBox { @@ -88,14 +94,14 @@ impl GameObject for MockGameObject { } fn vel(&self) -> &Vector { - &self.zero_vec + todo!() } fn vel_mut(&mut self) -> &mut Vector { - &mut self.zero_vec + todo!() } fn is_static(&self) -> bool { - return false; + todo!() } } diff --git a/pong/tests/game_object_tests.rs b/pong/tests/game_object_tests.rs index c74607e..71f7c5c 100644 --- a/pong/tests/game_object_tests.rs +++ b/pong/tests/game_object_tests.rs @@ -2,7 +2,7 @@ use rstest::rstest; use pong::game_object::components::{DefaultGeomComp, DefaultPhysicsComp}; use pong::game_object::game_object::{DefaultGameObject, GameObject}; use pong::geom::geom::{Vector}; -use pong::geom::shape::{Rect, Shape}; +use pong::geom::shape::Shape; #[rstest] #[case(Vector::new(100., 100.), Vector::new(-1., 1.), Vector::new(99., 101.))] @@ -10,7 +10,7 @@ pub fn should_update_pos(#[case] start_pos: Vector, #[case] vel: Vector, #[case] let mut obj = DefaultGameObject::new( 1, Box::new(DefaultGeomComp::new( - Box::new(Rect::new(Vector::new(start_pos.x as f64, start_pos.y as f64), Vector::new(1., 0.), 0., 0.)) + Shape::rect(Vector::new(start_pos.x as f64, start_pos.y as f64), Vector::new(1., 0.), 0., 0.) )), Box::new(DefaultPhysicsComp::new( vel, false