From a0f53ec2c7e558f9fb6a643e22fa715d32fcff4b Mon Sep 17 00:00:00 2001 From: Thilo Behnke Date: Sun, 24 Apr 2022 23:23:30 +0200 Subject: [PATCH] trait matching does not exist --- pong/src/game_object.rs | 12 +++++++++++- src/lib.rs | 17 +++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/pong/src/game_object.rs b/pong/src/game_object.rs index 81c57f9..b26c44a 100644 --- a/pong/src/game_object.rs +++ b/pong/src/game_object.rs @@ -2,10 +2,11 @@ pub mod game_object { use std::fmt::Debug; use crate::game_object::components::{GeomComp, PhysicsComp}; use crate::geom::geom::{BoundingBox, Vector}; - use crate::geom::shape::ShapeType; + use crate::geom::shape::{Shape, ShapeType}; pub trait GameObject : Debug { fn id(&self) -> u16; + fn shape(&self) -> &Box; fn pos(&self) -> &Vector; fn pos_mut(&mut self) -> &mut Vector; fn orientation(&self) -> &Vector; @@ -35,6 +36,10 @@ pub mod game_object { self.id } + fn shape(&self) -> &Box { + self.geom.shape() + } + fn pos(&self) -> &Vector { self.geom.center() } @@ -86,6 +91,7 @@ pub mod components { use crate::geom::shape::Shape; pub trait GeomComp : Debug { + fn shape(&self) -> &Box; fn orientation(&self) -> &Vector; fn orientation_mut(&mut self) -> &mut Vector; fn center(&self) -> &Vector; @@ -105,6 +111,10 @@ pub mod components { } impl GeomComp for DefaultGeomComp { + fn shape(&self) -> &Box { + &self.shape + } + fn orientation(&self) -> &Vector { &self.shape.orientation() } diff --git a/src/lib.rs b/src/lib.rs index 57020ea..dd2e99e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,15 +38,16 @@ pub struct GameObjectDTO { } impl GameObjectDTO { - pub fn from(obj: &GameObject) -> GameObjectDTO { + pub fn from(obj: &Box) -> GameObjectDTO { + let pos = obj.pos(); + let shape = obj.shape(); return GameObjectDTO { - id: obj.id, - x: obj.pos.x as u16, - y: obj.pos.y as u16, - shape_param_1: match obj.shape_params[..] { - [p1, _] => p1, - [p1] => p1, - _ => 0, + id: obj.id(), + x: pos.x as u16, + y: pos.y as u16, + shape_param_1: match shape.shape_type() { + ShapeType::Rect => shape., + ShapeType::Circle => }, shape_param_2: match obj.shape_params[..] { [_, p2] => p2,