diff --git a/pong/src/geom.rs b/pong/src/geom.rs index 2ab8e2c..f25138c 100644 --- a/pong/src/geom.rs +++ b/pong/src/geom.rs @@ -135,6 +135,7 @@ pub mod geom { } } + #[derive(Clone)] pub struct BoundingBox { top_left: Vector, top_right: Vector, diff --git a/pong/tests/collision_tests.rs b/pong/tests/collision_tests.rs index fbf9b99..7e429df 100644 --- a/pong/tests/collision_tests.rs +++ b/pong/tests/collision_tests.rs @@ -1,40 +1,41 @@ use rstest::rstest; use pong::collision::collision::{Collision, CollisionDetector}; -use pong::game_object::game_object::{GameObject, ShapeType}; -use pong::geom::geom::{Vector}; +use pong::game_object::game_object::GameObject; +use pong::geom::geom::{BoundingBox, Vector}; + #[rstest] #[case(vec![], vec![])] #[case( vec![ - GameObject{id: 1, pos: Vector{x: 50., y: 50.}, shape: Shape::Rect, shape_params: vec![20, 20], vel: Vector::zero(), is_static: false, orientation: Vector::new(1., 1.)}, - GameObject{id: 2, pos: Vector{x: 50., y: 50.}, shape: Shape::Rect, shape_params: vec![20, 20], vel: Vector::zero(), is_static: false, orientation: Vector::new(1., 1.)} + MockGameObject::new(1, BoundingBox::new(Vector{x: 50., y: 50.}, 20., 20.)), + MockGameObject::new(2, BoundingBox::new(Vector{x: 50., y: 50.}, 20., 20.)) ], vec![Collision(1, 2)] )] #[case( vec![ - GameObject{id: 1, pos: Vector{x: 60., y: 65.}, shape: Shape::Rect, shape_params: vec![20, 20], vel: Vector::zero(), is_static: false, orientation: Vector::new(1., 1.)}, - GameObject{id: 2, pos: Vector{x: 50., y: 50.}, shape: Shape::Rect, shape_params: vec![20, 20], vel: Vector::zero(), is_static: false, orientation: Vector::new(1., 1.)} + MockGameObject::new(1, BoundingBox::new(Vector{x: 60., y: 65.}, 20., 20.)), + MockGameObject::new(2, BoundingBox::new(Vector{x: 50., y: 50.}, 20., 20.)), ], vec![Collision(1, 2)] )] #[case( vec![ - GameObject{id: 1, pos: Vector{x: 50., y: 50.}, shape: Shape::Rect, shape_params: vec![20, 20], vel: Vector::zero(), is_static: false, orientation: Vector::new(1., 1.)}, - GameObject{id: 2, pos: Vector{x: 80., y: 80.}, shape: Shape::Rect, shape_params: vec![20, 20], vel: Vector::zero(), is_static: false, orientation: Vector::new(1., 1.)} + MockGameObject::new(1, BoundingBox::new(Vector{x: 50., y: 50.}, 20., 20.)), + MockGameObject::new(2, BoundingBox::new(Vector{x: 80., y: 80.}, 20., 20.)), ], vec![] )] #[case( vec![ - GameObject{id: 1, pos: Vector{x: 50., y: 50.}, shape: Shape::Rect, shape_params: vec![50, 50], vel: Vector::zero(), is_static: false, orientation: Vector::new(1., 1.)}, - GameObject{id: 2, pos: Vector{x: 500., y: 50.}, shape: Shape::Rect, shape_params: vec![50, 50], vel: Vector::zero(), is_static: false, orientation: Vector::new(1., 1.)} + MockGameObject::new(1, BoundingBox::new(Vector{x: 50., y: 50.}, 50., 50.)), + MockGameObject::new(2, BoundingBox::new(Vector{x: 500., y: 50.}, 50., 50.)), ], vec![] )] pub fn should_detect_collisions( - #[case] objs: Vec, + #[case] objs: Vec>, #[case] expected_collisions: Vec, ) { let detector = CollisionDetector::new(); @@ -44,3 +45,56 @@ pub fn should_detect_collisions( expected_collisions.iter().collect::>() ); } + +#[derive(Debug)] +pub struct MockGameObject { + id: u16, + bounding_box: BoundingBox +} + +impl MockGameObject { + pub fn new(id: u16, bounding_box: BoundingBox) -> Box { + Box::new( + MockGameObject { + id, bounding_box + } + ) + } +} + +impl GameObject for MockGameObject { + fn id(&self) -> u16 { + self.id + } + + fn pos(&self) -> &Vector { + &Vector::zero() + } + + fn pos_mut(&mut self) -> &mut Vector { + &mut Vector::zero() + } + + fn orientation(&self) -> &Vector { + &Vector::zero() + } + + fn update_pos(&mut self) { + } + + fn bounding_box(&self) -> BoundingBox { + self.bounding_box.clone() + } + + fn vel(&self) -> &Vector { + &Vector::zero() + } + + fn vel_mut(&mut self) -> &mut Vector { + &mut Vector::zero() + } + + fn is_static(&self) -> bool { + return false; + } +} diff --git a/pong/tests/game_field_test.rs b/pong/tests/game_field_test.rs index 54ce890..cb85dc6 100644 --- a/pong/tests/game_field_test.rs +++ b/pong/tests/game_field_test.rs @@ -14,7 +14,7 @@ mod game_field_tests { field.tick(inputs); let players = field.players(); let player = players.first().unwrap(); - assert_eq!(player.obj.pos.y, height as f64 / 2. + 1.); + assert_eq!(player.obj.pos().y, height as f64 / 2. + 1.); } #[test] @@ -29,7 +29,7 @@ mod game_field_tests { field.tick(inputs); let players = field.players(); let player = players.first().unwrap(); - assert_eq!(player.obj.pos.y, height as f64 / 2. - 1.); + assert_eq!(player.obj.pos().y, height as f64 / 2. - 1.); } #[test] @@ -44,7 +44,7 @@ mod game_field_tests { field.tick(inputs); let players = field.players(); let player = players.first().unwrap(); - assert_eq!(player.obj.pos.y, height as f64 - height as f64 / 5. / 2.); + assert_eq!(player.obj.pos().y, height as f64 - height as f64 / 5. / 2.); } #[test] @@ -59,6 +59,6 @@ mod game_field_tests { field.tick(inputs); let players = field.players(); let player = players.first().unwrap(); - assert_eq!(player.obj.pos.y, height as f64 / 5. / 2.); + assert_eq!(player.obj.pos().y, height as f64 / 5. / 2.); } }