#[derive(Debug, Clone, Copy)]
pub struct Rect {
pub x: f32,
pub y: f32,
pub w: f32,
pub h: f32,
}
impl Rect {
pub fn new(x: f32, y: f32, w: f32, h: f32) -> Self {
Self { x, y, w, h }
}
pub fn intersects(&self, other: &Rect) -> bool {
self.x < other.x + other.w
&& self.x + self.w > other.x
&& self.y < other.y + other.h
&& self.y + self.h > other.y
}
pub fn contains_point(&self, x: f32, y: f32) -> bool {
x >= self.x && x <= self.x + self.w && y >= self.y && y <= self.y + self.h
}
pub fn center(&self) -> (f32, f32) {
(self.x + self.w / 2.0, self.y + self.h / 2.0)
}
pub fn overlap(&self, other: &Rect) -> Option<(f32, f32)> {
if !self.intersects(other) {
return None;
}
let overlap_x = (self.x + self.w).min(other.x + other.w) - self.x.max(other.x);
let overlap_y = (self.y + self.h).min(other.y + other.h) - self.y.max(other.y);
Some((overlap_x, overlap_y))
}
}
#[derive(Debug, Clone, Copy)]
pub struct Collider {
pub offset: (f32, f32), pub size: (f32, f32),
pub is_trigger: bool, }
impl Collider {
pub fn new(width: f32, height: f32) -> Self {
Self {
offset: (0.0, 0.0),
size: (width, height),
is_trigger: false,
}
}
pub fn with_offset(mut self, x: f32, y: f32) -> Self {
self.offset = (x, y);
self
}
pub fn as_trigger(mut self) -> Self {
self.is_trigger = true;
self
}
pub fn get_rect(&self, object_x: f32, object_y: f32) -> Rect {
Rect::new(
object_x + self.offset.0,
object_y + self.offset.1,
self.size.0,
self.size.1,
)
}
}