-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBox.zig
56 lines (45 loc) · 1.54 KB
/
Box.zig
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
const Rect = @import("Renderer.zig").Rect;
const Box = @This();
x: i32,
y: i32,
w: i32,
h: i32,
pub fn init(x: i32, y: i32, w: i32, h: i32) Box {
return Box{ .x = x, .y = y, .w = w, .h = h };
}
pub fn toRect(self: Box) Rect {
return Rect.init(self.x, self.y, self.w, self.h);
}
pub fn overlaps(self: Box, other: Box) bool {
return self.x < other.x + other.w and self.x + self.w > other.x and self.y < other.y + other.h and self.y + self.h > other.y;
}
// like raycast in x dir by amount
pub fn castX(self: Box, amount: i32, other: Box) i32 {
// check y interval
if (self.y >= other.y + other.h or self.y + self.h <= other.y) return amount;
if (amount > 0 and self.x < other.x + other.w) {
if (self.x + self.w + amount > other.x) {
return other.x - (self.x + self.w);
}
} else if (amount < 0 and self.x + self.w > other.x) {
if (self.x + amount < other.x + other.w) {
return other.x + other.w - self.x;
}
}
return amount;
}
// like raycast in y dir by amount
pub fn castY(self: Box, amount: i32, other: Box) i32 {
// check x interval
if (self.x >= other.x + other.w or self.x + self.w <= other.x) return amount;
if (amount > 0 and self.y < other.y + other.h) {
if (self.y + self.h + amount > other.y) {
return other.y - (self.y + self.h);
}
} else if (amount < 0 and self.y + self.h > other.y) {
if (self.y + amount < other.y + other.h) {
return other.y + other.h - self.y;
}
}
return amount;
}