wip - ignoring trait issues for now

This commit is contained in:
Thilo Behnke
2022-04-24 20:16:50 +02:00
parent 80da680532
commit 6d76400dc0
4 changed files with 43 additions and 25 deletions

View File

@@ -26,7 +26,7 @@ pub mod collision {
if !has_collision {
continue;
}
collisions.push(Collision(obj.id, other.id))
collisions.push(Collision(obj.id(), other.id()))
}
if i >= objs.len() {
break;
@@ -75,18 +75,18 @@ pub mod collision {
CollisionHandler {}
}
pub fn handle(&self, obj_a: &mut dyn GameObject, obj_b: &dyn GameObject) {
if !obj_a.is_static {
obj_a.vel.reflect(&obj_b.orientation);
if obj_b.vel != Vector::zero() {
let mut adjusted = obj_b.vel.clone();
if !obj_a.is_static() {
obj_a.vel_mut().reflect(&obj_b.orientation());
if *obj_b.vel() != Vector::zero() {
let mut adjusted = obj_b.vel().clone();
adjusted.normalize();
obj_a.vel.add(&adjusted);
obj_a.vel_mut().add(&adjusted);
}
}
let mut b_to_a = obj_a.pos.clone();
b_to_a.sub(&obj_b.pos);
let mut b_to_a = obj_a.pos().clone();
b_to_a.sub(&obj_b.pos());
b_to_a.normalize();
obj_a.pos.add(&b_to_a);
obj_a.pos_mut().add(&b_to_a);
}
}
}

View File

@@ -103,7 +103,7 @@ impl Field {
ball.obj.update_pos()
}
let mut objs: Vec<GameObject> = vec![];
let mut objs: Vec<Box<dyn GameObject>> = vec![];
objs.extend(
self.players
.clone()

View File

@@ -5,14 +5,18 @@ pub mod game_object {
use crate::geom::shape::ShapeType;
pub trait GameObject {
fn id(&self) -> u16;
fn pos(&self) -> &Vector;
fn pos_mut(&mut self) -> &mut Vector;
fn orientation(&self) -> &Vector;
fn update_pos(&mut self);
fn vel(&self) -> &Vector;
fn set_vel_x(&mut self, x: f64);
fn set_vel_y(&mut self, y: f64);
fn bounding_box(&self) -> BoundingBox;
fn vel(&self) -> &Vector;
fn vel_mut(&mut self) -> &mut Vector;
fn is_static(&self) -> bool;
}
#[derive(Clone, Debug, PartialEq)]
// #[derive(Clone, Debug, PartialEq)]
pub struct DefaultGameObject {
pub id: u16,
geom: Box<dyn GeomComp>,
@@ -26,6 +30,22 @@ pub mod game_object {
}
impl GameObject for DefaultGameObject {
fn id(&self) -> u16 {
self.id
}
fn pos(&self) -> &Vector {
self.geom.center()
}
fn pos_mut(&mut self) -> &mut Vector {
self.geom.center_mut()
}
fn orientation(&self) -> &Vector {
self.geom.orientation()
}
fn update_pos(&mut self) {
let vel = self.vel();
let center = self.geom.center_mut();
@@ -41,22 +61,20 @@ pub mod game_object {
orientation.y = updated_orientation.y;
}
fn bounding_box(&self) -> BoundingBox {
self.geom.bounding_box()
}
fn vel(&self) -> &Vector {
&self.physics.vel()
}
fn set_vel_x(&mut self, x: f64) {
let vel = self.physics.vel_mut();
vel.x = x;
fn vel_mut(&mut self) -> &mut Vector {
self.physics.vel_mut()
}
fn set_vel_y(&mut self, y: f64) {
let vel = self.physics.vel_mut();
vel.y = y;
}
fn bounding_box(&self) -> BoundingBox {
self.geom.bounding_box()
fn is_static(&self) -> bool {
self.physics.is_static()
}
}
}

View File

@@ -315,7 +315,7 @@ pub mod shape {
}
fn bounding_box(&self) -> BoundingBox {
BoundingBox::create(&self.center(), self.radius * 2, self.radius * 2)
BoundingBox::create(&self.center(), self.radius * 2., self.radius * 2.)
}
}
}