1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::elements::{view::ViewElement, Pixel, Vec2D};

/// Contains references to all added objects. Meant to be used specifically for collision calculations
#[derive(Clone)]
pub struct CollisionContainer<'a> {
    /// The elements used to define the collision hitbox. This can be anything that implements [`ViewElement`]
    pub elements: Vec<&'a dyn ViewElement>,
}

impl<'a> CollisionContainer<'a> {
    /// Create a new `CollisionContainer`
    #[must_use]
    pub const fn new() -> CollisionContainer<'a> {
        CollisionContainer { elements: vec![] }
    }

    /// Add an element to the container
    pub fn push(&mut self, element: &'a impl ViewElement) {
        self.elements.push(element);
    }

    /// Return a list of all the positions at which the collision box is active
    #[deprecated = "This is now just a proxy for active_points, use `CollisionContainer::active_points` instead"]
    #[must_use]
    pub fn generate_collision_points(&self) -> Vec<Vec2D> {
        self.active_points()
    }

    /// Returns true if there is an element from the `CollisionContainer` at the given coordinates
    #[must_use]
    pub fn contains(&self, pos: Vec2D) -> bool {
        let collision_points = self.active_points();

        collision_points.contains(&pos)
    }

    /// Returns true if the given [`ViewElement`] is overlapping the `CollisionContainer`
    pub fn overlaps_element(&self, element: &impl ViewElement) -> bool {
        self.will_overlap_element(element, Vec2D::ZERO)
    }

    /// Returns true if the element will be overlapping the `CollisionContainer` when the offset is applied
    pub fn will_overlap_element(&self, element: &impl ViewElement, offset: Vec2D) -> bool {
        let collision_points = self.active_points();

        for element_point in element.active_points() {
            if collision_points.contains(&(element_point + offset)) {
                return true;
            }
        }

        false
    }
}

impl<'a> From<Vec<&'a dyn ViewElement>> for CollisionContainer<'a> {
    fn from(elements: Vec<&'a dyn ViewElement>) -> Self {
        Self { elements }
    }
}

impl<'a> ViewElement for CollisionContainer<'a> {
    fn active_pixels(&self) -> Vec<Pixel> {
        self.elements
            .iter()
            .flat_map(|e| e.active_pixels())
            .collect()
    }

    fn active_points(&self) -> Vec<Vec2D> {
        self.elements
            .iter()
            .flat_map(|e| e.active_points())
            .collect()
    }
}