pub trait Object {
fn boundary(&self) -> Boundary;
fn does_collide(&self, objects: &[Box<dyn Object>]) -> bool {
for object in objects.iter() {
let this = self.boundary();
let other = object.boundary();
if this.collides(&other) && this.is_duplicate(&other) == false {
return true;
}
}
false
}
}
#[derive(Debug, Copy, Clone)]
pub struct Boundary {
x: f32,
y: f32,
width: f32,
height: f32,
}
impl Boundary {
pub fn new(x: f32, y: f32, width: f32, height: f32) -> Boundary {
Boundary {
x,
y,
width,
height,
}
}
pub fn collides(&self, other: &Boundary) -> bool {
if self.x < other.x + other.width
&& self.x + self.width > other.x
&& self.y < other.y + other.height
&& self.y + self.height > other.y
{
return true;
}
false
}
pub fn is_duplicate(&self, other: &Boundary) -> bool {
self.x == other.x
&& self.y == other.y
&& self.width == other.width
&& self.height == other.height
}
}