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
|
-- For luaJit compatibility.
if table.unpack == nil then
table.unpack = unpack
end
local Vector2 = require( "vector2" )
Rectangle = {}
Rectangle.meta = {
__index = Rectangle,
__tostring = function( r )
return "{"..tostring( r.x )..", "..tostring( r.y )..", "..tostring( r.width )..", "..tostring( r.height ).."}"
end,
__add = function( r1, r2 )
return Rectangle:new( r1.x + r2.x, r1.y + r2.y, r1.width + r2.width, r1.height + r2.height )
end,
__sub = function( r1, r2 )
return Rectangle:new( r1.x - r2.x, r1.y - r2.y, r1.width - r2.width, r1.height - r2.height )
end,
__mul = function( r1, r2 )
return Rectangle:new( r1.x * r2.x, r1.y * r2.y, r1.width * r2.width, r1.height * r2.height )
end,
__div = function( r1, r2 )
return Rectangle:new( r1.x / r2.x, r1.y / r2.y, r1.width / r2.width, r1.height / r2.height )
end,
__mod = function( r, v )
return Rectangle:new( math.fmod( r.x, v ), math.fmod( r.y, v ), math.fmod( r.width, v ), math.fmod( r.height, v ) )
end,
__pow = function( r, v )
return Rectangle:new( r.x ^ v, r.y ^ v, r.width ^ v, r.height ^ v )
end,
__unm = function( r )
return Rectangle:new( -r.x, -r.y, -r.width, -r.height )
end,
__idiv = function( r, v )
return Rectangle:new( r.x // v, r.y // v, r.width // v, r.height // v )
end,
__len = function()
return 4
end,
__eq = function( r1, r2 )
return RL.Vector2Equals( { r1.x, r1.y }, { r2.x, r2.y } ) and RL.Vector2Equals( { r1.width, r1.height }, { r2.width, r2.height } )
end,
}
function Rectangle:new( x, y, width, height )
if type( x ) == "table" then
x, y, width, height = table.unpack( x )
elseif type( x ) == "nil" then
x, y, width, height = 0, 0, 0, 0
end
local object = setmetatable( {}, Rectangle.meta )
object.x = x
object.y = y
object.width = width
object.height = height
return object
end
function Rectangle:set( x, y, width, height )
if type( x ) == "table" then
x, y, width, height = table.unpack( x )
elseif type( x ) == "nil" then
x, y, width, height = 0, 0, 0, 0
end
self.x = x
self.y = y
self.width = width
self.height = height
end
function Rectangle:arr()
return { self.x, self.y, self.width, self.height }
end
function Rectangle:unpack()
return self.x, self.y, self.width, self.height
end
function Rectangle:clone()
return Rectangle:new( self.x, self.y, self.width, self.height )
end
function Rectangle:scale( scalar )
return Rectangle:new( self.x, self.y, self.width * scalar, self.height * scalar )
end
function Rectangle:min( rec )
return Rectangle:new( self.x, self.y, math.min( self.width, rec.width ), math.min( self.height, rec.height ) )
end
function Rectangle:max( rec )
return Rectangle:new( self.x, self.y, math.max( self.width, rec.width ), math.max( self.height, rec.height ) )
end
function Rectangle:floor()
return Rectangle:new( math.floor( self.x ), math.floor( self.y ), math.floor( self.width ), math.floor( self.height ) )
end
function Rectangle:ceil()
return Rectangle:new( math.ceil( self.x ), math.ceil( self.y ), math.ceil( self.width ), math.ceil( self.height ) )
end
function Rectangle:area()
return self.width * self.height
end
function Rectangle:fit( rec )
local pos = Vector2:new( math.min( self.x, rec.x ), math.min( self.y, rec.y ) )
return Rectangle:new(
pos.x,
pos.y,
math.max( self.x + self.width - pos.x, rec.x + rec.width - pos.x ),
math.max( self.y + self.height - pos.y, rec.y + rec.height - pos.y )
)
end
function Rectangle:checkCollisionRec( rec )
return RL.CheckCollisionRecs( self, rec )
end
function Rectangle:checkCollisionCircle( center, radius )
return RL.CheckCollisionCircleRec( center, radius, self )
end
function Rectangle:checkCollisionPoint( point )
return RL.CheckCollisionPointRec( point, self )
end
function Rectangle:getCollisionRec( rec )
return Rectangle:new( RL.GetCollisionRec( self, rec ) )
end
return Rectangle
|