game_toolkit_platformer/sim/rect.rs
1//! [`Rect`]: an integer axis-aligned box, in units or in a terrain's pixels.
2
3/// An axis-aligned box: top-left `x, y`, size `w` x `h`. Units inside the movement, a
4/// terrain's pixels at the facade with a `Builder::scale` (see `docs/scale.md`).
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub struct Rect {
7 /// Left edge.
8 pub x: i32,
9 /// Top edge.
10 pub y: i32,
11 /// Width.
12 pub w: i32,
13 /// Height.
14 pub h: i32,
15}
16
17impl Rect {
18 /// The box moved by `dx, dy`.
19 pub fn shifted(self, dx: i32, dy: i32) -> Self {
20 Self {
21 x: self.x + dx,
22 y: self.y + dy,
23 ..self
24 }
25 }
26
27 /// The box `k` times as large, from the origin: units to pixels at `k` pixels per
28 /// unit.
29 pub fn scaled(self, k: i32) -> Self {
30 Self {
31 x: self.x * k,
32 y: self.y * k,
33 w: self.w * k,
34 h: self.h * k,
35 }
36 }
37
38 /// One past the right edge.
39 pub fn right(self) -> i32 {
40 self.x + self.w
41 }
42
43 /// One past the bottom edge.
44 pub fn bottom(self) -> i32 {
45 self.y + self.h
46 }
47
48 /// Whether the two boxes overlap (share any unit square).
49 pub fn intersects(self, o: Rect) -> bool {
50 self.x < o.right() && o.x < self.right() && self.y < o.bottom() && o.y < self.bottom()
51 }
52}