gemini_engine/primitives/
rect.rsuse crate::{containers::CanCollide, core::{CanDraw, ColChar, Vec2D}};
pub struct Rect {
pub pos: Vec2D,
pub size: Vec2D,
pub fill_char: ColChar,
}
impl Rect {
#[must_use]
pub const fn new(pos: Vec2D, size: Vec2D, fill_char: ColChar) -> Self {
Self {
pos,
size,
fill_char,
}
}
#[must_use]
pub fn new_from_to(top_left: Vec2D, bottom_right: Vec2D, fill_char: ColChar) -> Self {
Self::new(top_left, bottom_right - top_left + Vec2D::ONE, fill_char)
}
#[must_use]
pub fn bottom_right(&self) -> Vec2D {
self.pos + self.size - Vec2D::ONE
}
}
impl CanDraw for Rect {
fn draw_to(&self, canvas: &mut impl crate::core::Canvas) {
for x in 0..self.size.x {
for y in 0..self.size.y {
canvas.plot(self.pos + Vec2D::new(x, y), self.fill_char);
}
}
}
}
impl CanCollide for Rect {
fn collides_with_pos(&self, pos: Vec2D) -> bool {
pos.cmpge(self.pos).all() && pos.cmple(self.bottom_right()).all()
}
}