mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-07-11 02:29:25 +00:00
wip - fixing tests:
This commit is contained in:
@@ -135,6 +135,7 @@ pub mod geom {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BoundingBox {
|
||||
top_left: Vector,
|
||||
top_right: Vector,
|
||||
|
||||
@@ -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<GameObject>,
|
||||
#[case] objs: Vec<Box<dyn GameObject>>,
|
||||
#[case] expected_collisions: Vec<Collision>,
|
||||
) {
|
||||
let detector = CollisionDetector::new();
|
||||
@@ -44,3 +45,56 @@ pub fn should_detect_collisions(
|
||||
expected_collisions.iter().collect::<Vec<&Collision>>()
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MockGameObject {
|
||||
id: u16,
|
||||
bounding_box: BoundingBox
|
||||
}
|
||||
|
||||
impl MockGameObject {
|
||||
pub fn new(id: u16, bounding_box: BoundingBox) -> Box<dyn GameObject> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user