clone a boxed trait object? or wrong approach?

This commit is contained in:
Thilo Behnke
2022-04-24 23:51:55 +02:00
parent 2be20b40a5
commit 9cfd250b81
3 changed files with 22 additions and 19 deletions

View File

@@ -106,19 +106,22 @@ impl Field {
let mut objs: Vec<&Box<dyn GameObject>> = vec![];
objs.extend(
self.players
.iter()
.clone()
.into_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.balls
.clone()
.into_iter()
.map(|b| &b.obj)
.collect::<Vec<&Box<dyn GameObject>>>(),
);
objs.extend(
self.bounds.objs
.iter()
.clone()
.into_iter()
.collect::<Vec<&Box<dyn GameObject>>>()
);
let collision_detector = CollisionDetector::new();
@@ -137,7 +140,6 @@ impl Field {
collision => objs.iter().find(|o| o.id() == collision.0).unwrap(),
};
self.logger.log("### BEFORE COLLISION ###");
self.logger.log(&*format!("{:?}", ball.obj));
self.logger.log(&*format!("{:?}", other));

View File

@@ -4,7 +4,7 @@ pub mod game_object {
use crate::geom::geom::{BoundingBox, Vector};
use crate::geom::shape::{Shape, ShapeType};
pub trait GameObject : Debug {
pub trait GameObject : Debug + Clone {
fn id(&self) -> u16;
fn shape(&self) -> &ShapeType;
fn pos(&self) -> &Vector;
@@ -18,7 +18,7 @@ pub mod game_object {
}
// #[derive(Clone, Debug, PartialEq)]
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct DefaultGameObject {
pub id: u16,
geom: Box<dyn GeomComp>,

View File

@@ -6,8 +6,9 @@ use std::cmp::{max, min};
use wasm_bindgen::prelude::*;
use pong::collision::collision::{Collision, CollisionDetector};
use pong::game_field::{Field, Input, InputType};
use pong::game_object::game_object::{GameObject, ShapeType};
use pong::game_object::game_object::{GameObject};
use pong::geom::geom::Vector;
use pong::geom::shape::ShapeType;
use pong::utils::utils::Logger;
extern crate serde_json;
@@ -45,13 +46,13 @@ impl GameObjectDTO {
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_1: match shape {
ShapeType::Rect(_, width, _) => *width as u16,
ShapeType::Circle(_, radius) => *radius as u16
},
shape_param_2: match obj.shape_params[..] {
[_, p2] => p2,
_ => 0,
shape_param_2: match shape {
ShapeType::Rect(_, _, height) => *height as u16,
ShapeType::Circle(_, _) => 0
},
};
}
@@ -115,7 +116,7 @@ impl FieldWrapper {
let input_dtos: Vec<InputDTO> = inputs_js.into_serde().unwrap();
let inputs = input_dtos.into_iter().map(|i| i.to_input()).collect::<Vec<Input>>();
self.field.tick(inputs);
// log!("{:?}", self.field.collisions);
log!("{:?}", self.field.collisions);
}
pub fn objects(&self) -> *const GameObjectDTO {