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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
-- For luaJit compatibility.
if table.unpack == nil then
table.unpack = unpack
end
local Vector2 = require( "vector2" )
local Rectangle = {}
local metatable = {
__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,
__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,
__concat = function( a, b )
return tostring( a )..tostring( b )
end,
}
function Rectangle:new( x, y, width, height )
local object = setmetatable( {}, metatable )
object.x = x or 0
object.y = y or object.x
object.width = width or 0
object.height = height or object.width
return object
end
function Rectangle:newT( t )
local object = setmetatable( {}, metatable )
object.x, object.y, object.width, object.height = table.unpack( t )
return object
end
function Rectangle:newR( r )
local object = setmetatable( {}, metatable )
object.x = r.x
object.y = r.y
object.width = r.width
object.height = r.height
return object
end
function Rectangle:set( x, y, width, height )
self.x = x or 0
self.y = y or self.x
self.width = width or 0
self.height = height or self.width
end
function Rectangle:setT( t )
self.x, self.y, self.width, self.height = table.unpack( t )
end
function Rectangle:setR( r )
self.x = r.x
self.y = r.y
self.width = r.width
self.height = r.height
end
function Rectangle:serialize()
return "Rectangle:new("..self.x..","..self.y..","..self.width..","..self.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
-- Returns rectangle that fits both rectangles inside it
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
-- If rectangle is fully inside another rectangle
function Rectangle:isInside( rec )
return rec.x <= self.x and self.x + self.width <= rec.x + rec.width
and rec.y <= self.y and self.y + self.height <= rec.y + rec.height
end
-- Returns clamped rectangle that is inside another rectangle.
function Rectangle:clampInside( rec )
return Rectangle:new(
math.max( rec.x, math.min( self.x, rec.x + rec.width - self.width ) ),
math.max( rec.y, math.min( self.y, rec.y + rec.height - self.height ) ),
self.width,
self.height
)
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
-- Temp pre generated objects to avoid "slow" table generation.
local TEMP_COUNT = 100
local tempPool = {}
local curTemp = 1
for _ = 1, TEMP_COUNT do
table.insert( tempPool, Rectangle:new( 0, 0, 0, 0 ) )
end
function Rectangle:temp( x, y, width, height )
local object = tempPool[ curTemp ]
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object.x = x or 0
object.y = y or object.x
object.width = width or 0
object.height = height or object.width
return object
end
function Rectangle:tempT( t )
local object = tempPool[ curTemp ]
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object.x, object.y, object.width, object.height = table.unpack( t )
return object
end
function Rectangle:tempR( r )
local object = tempPool[ curTemp ]
curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
object.x = r.x
object.y = r.y
object.width = r.width
object.height = r.height
return object
end
function Rectangle:getTempId()
return curTemp
end
return Rectangle
|