mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-07-11 02:29:25 +00:00
compiling code (deactivated inter ball collisions for now)
This commit is contained in:
@@ -10,7 +10,7 @@ pub mod collision {
|
||||
CollisionDetector {}
|
||||
}
|
||||
|
||||
pub fn detect_collisions(&self, objs: Vec<&Box<dyn GameObject>>) -> Box<dyn CollisionRegistry> {
|
||||
pub fn detect_collisions(&self, objs: &Vec<&Box<dyn GameObject>>) -> Box<dyn CollisionRegistry> {
|
||||
if objs.is_empty() {
|
||||
return Box::new(Collisions::new(vec![]));
|
||||
}
|
||||
|
||||
@@ -103,30 +103,27 @@ impl Field {
|
||||
ball.obj.update_pos()
|
||||
}
|
||||
|
||||
let mut objs: Vec<Box<dyn GameObject>> = vec![];
|
||||
let mut objs: Vec<&Box<dyn GameObject>> = vec![];
|
||||
objs.extend(
|
||||
self.players
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|p| p.obj)
|
||||
.collect::<Vec<Box<dyn GameObject>>>(),
|
||||
);
|
||||
objs.extend(
|
||||
self.balls
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|b| b.obj)
|
||||
.collect::<Vec<Box<dyn GameObject>>>(),
|
||||
.iter()
|
||||
.map(|p| &p.obj)
|
||||
.collect::<Vec<&Box<dyn GameObject>>>(),
|
||||
);
|
||||
// objs.extend(
|
||||
// self.balls
|
||||
// .iter()
|
||||
// .map(|b| &b.obj)
|
||||
// .collect::<Vec<&Box<dyn GameObject>>>(),
|
||||
// );
|
||||
objs.extend(
|
||||
self.bounds.objs
|
||||
.clone()
|
||||
.into_iter()
|
||||
.collect::<Vec<Box<dyn GameObject>>>()
|
||||
.iter()
|
||||
.collect::<Vec<&Box<dyn GameObject>>>()
|
||||
);
|
||||
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());
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user