sevenx_engine 0.2.11

Engine de jogos 2D/3D completa com suporte Android, física, áudio, partículas, tilemap, UI, eventos e sistema 3D avançado com PBR.
Documentation
/// Representa um retângulo de colisão (AABB - Axis-Aligned Bounding Box).
#[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 }
    }

    /// Verifica se este retângulo intersecta com outro.
    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
    }

    /// Verifica se um ponto está dentro do retângulo.
    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
    }

    /// Retorna o centro do retângulo.
    pub fn center(&self) -> (f32, f32) {
        (self.x + self.w / 2.0, self.y + self.h / 2.0)
    }

    /// Calcula a sobreposição com outro retângulo (retorna None se não houver sobreposição).
    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))
    }
}

/// Componente de colisão para objetos.
#[derive(Debug, Clone, Copy)]
pub struct Collider {
    pub offset: (f32, f32),  // Offset em relação à posição do objeto
    pub size: (f32, f32),
    pub is_trigger: bool,  // Se true, detecta colisões mas não bloqueia movimento
}

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
    }

    /// Obtém o retângulo de colisão baseado na posição do objeto.
    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,
        )
    }
}