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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
pub trait TwoDimensional {
    type number;

    fn get_x(&self) -> Self::number;
    fn get_y(&self) -> Self::number;
    fn get_coordinates(&self) -> (Self::number, Self::number) {
        (self.get_x(), self.get_y())
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Point {
    x: i32,
    y: i32,
}

impl Point {
    pub fn new(x: i32, y: i32) -> Point {
        Point { x, y }
    }
    pub fn set_x(&mut self, x: i32) {
        self.x = x;
    }
    pub fn set_y(&mut self, y: i32) {
        self.y = y;
    }
}

impl TwoDimensional for Point {
    type number = i32;

    fn get_x(&self) -> i32 {
        self.x
    }
    fn get_y(&self) -> i32 {
        self.y
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Dimension {
    width: u32,
    height: u32,
}

impl Dimension {
    pub fn new(width: u32, height: u32) -> Dimension {
        Dimension { width, height }
    }
    pub fn get_height(&self) -> u32 {
        self.height
    }
    pub fn get_width(&self) -> u32 {
        self.width
    }
    pub fn set_width(&mut self, width: u32) {
        self.width = width;
    }
    pub fn set_height(&mut self, height: u32) {
        self.height = height;
    }
}

impl TwoDimensional for Dimension {
    type number = u32;

    fn get_x(&self) -> u32 {
        self.get_width()
    }
    fn get_y(&self) -> u32 {
        self.get_height()
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Rectangle {
    point: Point,
    dimension: Dimension,
}

impl Rectangle {
    pub fn new(x: i32, y: i32, width: u32, height: u32) -> Rectangle {
        Rectangle {
            point: Point::new(x, y),
            dimension: Dimension::new(width, height),
        }
    }
    pub fn get_point(&self) -> Point {
        self.point
    }
    pub fn get_dimension(&self) -> Dimension {
        self.dimension
    }
    pub fn move_x(&mut self, amount_x: i32) {
        self.point.set_x(amount_x + self.get_point().get_x());
    }
    pub fn move_y(&mut self, amount_y: i32) {
        self.point.set_y(amount_y + self.get_point().get_y());
    }
    pub fn move_xy(&mut self, amount: Point) {
        self.move_x(amount.get_x());
        self.move_y(amount.get_y());
    }
    pub fn x(&self) -> i32 {
        self.get_point().get_x()
    }
    pub fn y(&self) -> i32 {
        self.get_point().get_y()
    }
    pub fn width(&self) -> u32 {
        self.get_dimension().get_width()
    }
    pub fn height(&self) -> u32 {
        self.get_dimension().get_height()
    }
    // create collision enum
    pub fn is_colliding(&self, other_rectangle: Rectangle) -> bool {
        if self.y_max() < other_rectangle.y_min() {
            return false;
        }
        if self.y_min() > other_rectangle.y_max() {
            return false;
        }
        if self.x_max() >= other_rectangle.x_min() && self.x_min() <= other_rectangle.x_max() {
            return true;
        }
        false
    }
    pub fn is_not_colliding(&self, other_rectangle: Rectangle) -> bool {
        !self.is_colliding(other_rectangle)
    }
    pub fn y_max(&self) -> i32 {
        self.y() + self.height() as i32 - 1
    }
    pub fn x_max(&self) -> i32 {
        self.x() + self.width() as i32 - 1
    }
    pub fn y_min(&self) -> i32 {
        self.y()
    }
    pub fn x_min(&self) -> i32 {
        self.x()
    }
}