mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-07-10 18:20:00 +00:00
dev-ops/fix-pong-tests
This commit is contained in:
@@ -117,11 +117,11 @@ pub mod collision {
|
||||
pub struct CollisionHandlerRegistry {
|
||||
handlers: HashMap<
|
||||
(String, String),
|
||||
fn(Rc<RefCell<Box<dyn GameObject>>>, Rc<RefCell<Box<dyn GameObject>>>),
|
||||
fn(&Rc<RefCell<Box<dyn GameObject>>>, &Rc<RefCell<Box<dyn GameObject>>>),
|
||||
>,
|
||||
}
|
||||
|
||||
type CollisionCallback = fn(Rc<RefCell<Box<dyn GameObject>>>, Rc<RefCell<Box<dyn GameObject>>>);
|
||||
type CollisionCallback = fn(&Rc<RefCell<Box<dyn GameObject>>>, &Rc<RefCell<Box<dyn GameObject>>>);
|
||||
|
||||
impl CollisionHandlerRegistry {
|
||||
pub fn new() -> CollisionHandlerRegistry {
|
||||
@@ -144,8 +144,8 @@ pub mod collision {
|
||||
&self,
|
||||
mapping: &(String, String),
|
||||
values: (
|
||||
Rc<RefCell<Box<dyn GameObject>>>,
|
||||
Rc<RefCell<Box<dyn GameObject>>>,
|
||||
&Rc<RefCell<Box<dyn GameObject>>>,
|
||||
&Rc<RefCell<Box<dyn GameObject>>>,
|
||||
),
|
||||
) -> bool {
|
||||
let regular = self.handlers.get(&mapping);
|
||||
@@ -182,21 +182,21 @@ pub mod collision {
|
||||
pub fn register(
|
||||
&mut self,
|
||||
mapping: (String, String),
|
||||
callback: fn(Rc<RefCell<Box<dyn GameObject>>>, Rc<RefCell<Box<dyn GameObject>>>),
|
||||
callback: fn(&Rc<RefCell<Box<dyn GameObject>>>, &Rc<RefCell<Box<dyn GameObject>>>),
|
||||
) {
|
||||
self.handlers.add(mapping, callback)
|
||||
}
|
||||
|
||||
pub fn handle(
|
||||
&self,
|
||||
obj_a: Rc<RefCell<Box<dyn GameObject>>>,
|
||||
obj_b: Rc<RefCell<Box<dyn GameObject>>>,
|
||||
obj_a: &Rc<RefCell<Box<dyn GameObject>>>,
|
||||
obj_b: &Rc<RefCell<Box<dyn GameObject>>>,
|
||||
) -> bool {
|
||||
let key = (
|
||||
RefCell::borrow(&obj_a).obj_type().to_string(),
|
||||
RefCell::borrow(&obj_b).obj_type().to_string(),
|
||||
);
|
||||
let handler_res = self.handlers.call(&key, (obj_a, obj_b));
|
||||
let handler_res = self.handlers.call(&key, (&obj_a, &obj_b));
|
||||
if !handler_res {
|
||||
self.logger
|
||||
.log(&*format!("Found no matching collision handler: {:?}", key));
|
||||
|
||||
@@ -185,7 +185,7 @@ impl Field {
|
||||
.find(|o| RefCell::borrow(o).id() == collision.1)
|
||||
.unwrap()
|
||||
.clone();
|
||||
collision_handler.handle(obj_a, obj_b);
|
||||
collision_handler.handle(&obj_a, &obj_b);
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
@@ -22,6 +22,12 @@ pub mod geom {
|
||||
Vector { x, y }
|
||||
}
|
||||
|
||||
pub fn inverted(vec: &Vector) -> Vector {
|
||||
let mut inverted = vec.clone();
|
||||
inverted.invert();
|
||||
return inverted;
|
||||
}
|
||||
|
||||
pub fn normalize(&mut self) {
|
||||
if self == &Vector::zero() {
|
||||
return;
|
||||
|
||||
@@ -6,8 +6,8 @@ pub mod pong_collisions {
|
||||
use std::rc::Rc;
|
||||
|
||||
pub fn handle_player_ball_collision(
|
||||
ball: Rc<RefCell<Box<dyn GameObject>>>,
|
||||
player: Rc<RefCell<Box<dyn GameObject>>>,
|
||||
ball: &Rc<RefCell<Box<dyn GameObject>>>,
|
||||
player: &Rc<RefCell<Box<dyn GameObject>>>,
|
||||
) {
|
||||
// reflect
|
||||
let mut ball = RefCell::borrow_mut(&ball);
|
||||
@@ -40,8 +40,8 @@ pub mod pong_collisions {
|
||||
}
|
||||
|
||||
pub fn handle_ball_bounds_collision(
|
||||
ball: Rc<RefCell<Box<dyn GameObject>>>,
|
||||
bound: Rc<RefCell<Box<dyn GameObject>>>,
|
||||
ball: &Rc<RefCell<Box<dyn GameObject>>>,
|
||||
bound: &Rc<RefCell<Box<dyn GameObject>>>,
|
||||
) {
|
||||
let mut ball = RefCell::borrow_mut(&ball);
|
||||
let bound = RefCell::borrow(&bound);
|
||||
@@ -57,8 +57,8 @@ pub mod pong_collisions {
|
||||
}
|
||||
|
||||
pub fn handle_player_bound_collision(
|
||||
player: Rc<RefCell<Box<dyn GameObject>>>,
|
||||
bound: Rc<RefCell<Box<dyn GameObject>>>,
|
||||
player: &Rc<RefCell<Box<dyn GameObject>>>,
|
||||
bound: &Rc<RefCell<Box<dyn GameObject>>>,
|
||||
) {
|
||||
let mut player = RefCell::borrow_mut(&player);
|
||||
let bound = RefCell::borrow(&bound);
|
||||
|
||||
@@ -10,77 +10,52 @@ use std::rc::Rc;
|
||||
|
||||
#[rstest]
|
||||
#[case(
|
||||
// given
|
||||
create_game_obj(1, Vector::new(1., 0.), Vector::new(1., 0.), true),
|
||||
create_game_obj(2, Vector::new(0., 0.), Vector::new(0., 1.), true),
|
||||
// expected
|
||||
create_game_obj(1, Vector::new(1., 0.), Vector::new(1., 0.), true),
|
||||
create_game_obj(2, Vector::new(0., 0.), Vector::new(0., 1.), true),
|
||||
)]
|
||||
#[case(
|
||||
// given
|
||||
create_game_obj(1, Vector::new(1., 0.), Vector::new(1., 0.), false),
|
||||
create_game_obj(2, Vector::new(0., 0.), Vector::new(0., 1.), true),
|
||||
// expected
|
||||
create_game_obj(1, Vector::new(-1., 0.), Vector::new(1., 0.), false),
|
||||
create_game_obj(2, Vector::new(0., 0.), Vector::new(0., 1.), true),
|
||||
)]
|
||||
#[case(
|
||||
// given
|
||||
create_game_obj(1, Vector::new(-1., 0.), Vector::new(-1., 0.), false),
|
||||
create_game_obj(2, Vector::new(0., 0.), Vector::new(0., 1.), true),
|
||||
// expected
|
||||
create_game_obj(1, Vector::new(1., 0.), Vector::new(-1., 0.), false),
|
||||
create_game_obj(2, Vector::new(0., 0.), Vector::new(0., 1.), true),
|
||||
)]
|
||||
#[case(
|
||||
// given
|
||||
create_game_obj(1, Vector::new(1., 1.), Vector::new(1., 1.), false),
|
||||
create_game_obj(2, Vector::new(0., 0.), Vector::new(0., 1.), true),
|
||||
// expected
|
||||
create_game_obj(1, Vector::new(-1., 1.), Vector::new(1., 1.), false),
|
||||
create_game_obj(2, Vector::new(0., 0.), Vector::new(0., 1.), true),
|
||||
)]
|
||||
#[case(
|
||||
// given
|
||||
create_game_obj(1, Vector::new(-2., 1.), Vector::new(-1., 0.), false),
|
||||
create_game_obj(2, Vector::new(0., 0.), Vector::new(0., 1.), true),
|
||||
// expected
|
||||
create_game_obj(1, Vector::new(2., 1.), Vector::new(-1., 0.), false),
|
||||
create_game_obj(2, Vector::new(0., 0.), Vector::new(0., 1.), true),
|
||||
)]
|
||||
#[case(
|
||||
// given
|
||||
create_game_obj(1, Vector::new(1., 0.), Vector::new(1., 0.), false),
|
||||
create_game_obj(2, Vector::new(0., 1.), Vector::new(0., 1.), true),
|
||||
// expected
|
||||
create_game_obj(1, Vector::new(-1., 1.), Vector::new(1., 0.), false),
|
||||
create_game_obj(2, Vector::new(0., 1.), Vector::new(0., 1.), true),
|
||||
)]
|
||||
#[case(
|
||||
// given
|
||||
create_game_obj(1, Vector::new(-2., 1.), Vector::new(-1., 0.), false),
|
||||
create_game_obj(2, Vector::new(0., 0.), Vector::new(0., 1.), true),
|
||||
// expected
|
||||
create_game_obj(1, Vector::new(2., 1.), Vector::new(-1., 0.), false),
|
||||
create_game_obj(2, Vector::new(0., 0.), Vector::new(0., 1.), true),
|
||||
)]
|
||||
pub fn should_handle_collision(
|
||||
#[case] obj_a: Rc<RefCell<Box<dyn GameObject>>>,
|
||||
#[case] obj_b: Rc<RefCell<Box<dyn GameObject>>>,
|
||||
#[case] _expected_a: Rc<RefCell<Box<dyn GameObject>>>,
|
||||
#[case] _expected_b: Rc<RefCell<Box<dyn GameObject>>>,
|
||||
#[case] obj_b: Rc<RefCell<Box<dyn GameObject>>>
|
||||
) {
|
||||
let logger = DefaultLoggerFactory::noop();
|
||||
let mut handler = CollisionHandler::new(&logger);
|
||||
handler.register((String::from("obj"), String::from("obj")), |_a, _b| {});
|
||||
let res = handler.handle(obj_a, obj_b);
|
||||
handler.register((String::from("obj"), String::from("obj")), |_a, _b| {
|
||||
let mut a_mut = RefCell::borrow_mut(_a);
|
||||
let mut vel_inverted = a_mut.vel().clone();
|
||||
vel_inverted.invert();
|
||||
*a_mut.vel_mut() = vel_inverted;
|
||||
});
|
||||
let expected_vel_a = Vector::inverted(RefCell::borrow(&obj_a).vel());
|
||||
let res = handler.handle(&obj_a, &obj_b);
|
||||
assert_eq!(true, res);
|
||||
// TODO: Fix
|
||||
// assert_eq!(RefCell::borrow(&obj_a).pos(), RefCell::borrow(&expected_a).pos());
|
||||
// assert_eq!(obj_a.vel(), expected_a.vel());
|
||||
// assert_eq!(obj_b.pos(), expected_b.pos());
|
||||
// assert_eq!(obj_b.vel(), expected_b.vel());
|
||||
assert_eq!(RefCell::borrow(&obj_a).pos(), RefCell::borrow(&obj_a).pos());
|
||||
assert_eq!(RefCell::borrow(&obj_a).vel(), &expected_vel_a);
|
||||
assert_eq!(RefCell::borrow(&obj_b).pos(), RefCell::borrow(&obj_b).pos());
|
||||
assert_eq!(RefCell::borrow(&obj_a).vel(), RefCell::borrow(&obj_a).vel());
|
||||
}
|
||||
|
||||
fn create_game_obj(
|
||||
|
||||
Reference in New Issue
Block a user