1 | <?php |
||
8 | class Box |
||
9 | { |
||
10 | /** @var float */ |
||
11 | protected $x; |
||
12 | |||
13 | /** @var float */ |
||
14 | protected $y; |
||
15 | |||
16 | /** @var float */ |
||
17 | protected $width; |
||
18 | |||
19 | /** @var float */ |
||
20 | protected $height; |
||
21 | |||
22 | /** @var float */ |
||
23 | protected $top; |
||
24 | |||
25 | /** @var float */ |
||
26 | protected $bottom; |
||
27 | |||
28 | /** @var float */ |
||
29 | protected $left; |
||
30 | |||
31 | /** @var float */ |
||
32 | protected $right; |
||
33 | |||
34 | /** |
||
35 | * Constructor |
||
36 | * @param float $x |
||
37 | * @param float $y |
||
38 | * @param float $width |
||
39 | * @param float $height |
||
40 | */ |
||
41 | 26 | public function __construct($x, $y, $width, $height) |
|
50 | |||
51 | 10 | protected function strictComparator($x, $y) { |
|
54 | |||
55 | 5 | protected function nonStrictComparator($x, $y) { |
|
58 | |||
59 | /** |
||
60 | * Factory method. |
||
61 | * @param float $x |
||
62 | * @param float $y |
||
63 | * @param float $width |
||
64 | * @param float $height |
||
65 | * @return Box |
||
66 | */ |
||
67 | 5 | public static function create($x, $y, $width, $height) : Box |
|
71 | |||
72 | /** |
||
73 | * Update the left, right, top, and bottom coordinates. |
||
74 | */ |
||
75 | 26 | public function update() |
|
82 | |||
83 | /** |
||
84 | * Detect box collision |
||
85 | * This algorithm only works with Axis-Aligned boxes! |
||
86 | * @param Box $box The other rectangle to test collision with |
||
87 | * @param bool $strict If true, boxes "touching" each other are not intersecting, otherwise they are |
||
88 | * @return boolean True is the boxes collide, false otherwise |
||
89 | */ |
||
90 | 9 | public function intersects(Box $box, $strict = true) : bool |
|
99 | |||
100 | /** |
||
101 | * @param Box $box |
||
102 | * @return bool |
||
103 | */ |
||
104 | 6 | public function inside(Box $box, $strict = false) : bool |
|
113 | |||
114 | /** |
||
115 | * @param float $deltaX |
||
116 | * @param float $deltaY |
||
117 | * @return Box |
||
118 | */ |
||
119 | 1 | public function move($deltaX, $deltaY) : Box |
|
123 | |||
124 | /** |
||
125 | * @param int $increment |
||
126 | * @return Box |
||
127 | */ |
||
128 | 1 | public function resize($increment) : Box |
|
137 | |||
138 | /** |
||
139 | * @return float |
||
140 | */ |
||
141 | 14 | public function getBottom() : float |
|
145 | |||
146 | /** |
||
147 | * @return float |
||
148 | */ |
||
149 | 19 | public function getHeight() : float |
|
153 | |||
154 | /** |
||
155 | * @return float |
||
156 | */ |
||
157 | 20 | public function getLeft() : float |
|
161 | |||
162 | /** |
||
163 | * @return float |
||
164 | */ |
||
165 | 17 | public function getRight() : float |
|
169 | |||
170 | /** |
||
171 | * @return float |
||
172 | */ |
||
173 | 14 | public function getTop() : float |
|
177 | |||
178 | /** |
||
179 | * @return float |
||
180 | */ |
||
181 | 19 | public function getWidth() : float |
|
185 | |||
186 | /** |
||
187 | * @return float |
||
188 | */ |
||
189 | 21 | public function getX() : float |
|
193 | |||
194 | /** |
||
195 | * @return float |
||
196 | */ |
||
197 | 21 | public function getY() : float |
|
201 | |||
202 | /** |
||
203 | * @return Vector |
||
204 | */ |
||
205 | 1 | public function getPosition() : Vector |
|
209 | |||
210 | /** |
||
211 | * @return Vector |
||
212 | */ |
||
213 | 1 | public function getDimensions() : Vector |
|
217 | |||
218 | /** |
||
219 | * @return Vector |
||
220 | */ |
||
221 | 18 | public function getCenter() : Vector |
|
228 | |||
229 | /** |
||
230 | * @return string |
||
231 | */ |
||
232 | 1 | public function __toString() : string |
|
236 | |||
237 | 1 | public function serialize(): string |
|
246 | } |
||
247 |