-
Notifications
You must be signed in to change notification settings - Fork 0
/
rect.lua
33 lines (30 loc) · 879 Bytes
/
rect.lua
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
rect = {}
setmetatable(rect,rect)
function rect:__index(key)
return rawget(rect,key)
end
function rect:collide(other)
return self:__basecollide(other) or other:__basecollide(self)
end
function rect:__basecollide(other)
if (other.x >= self.x and other.x <= self.endx) or (other.endx >= self.x and other.endx <= self.endx) or (other.x <= self.x and other.endx >= self.x) then
if (other.y >= self.y and other.y <= self.endy) or (other.endy >= self.y and other.endy <= self.endy) or (other.y <= self.y and other.endy >= self.y) then
return true
end
end
return false
end
function rect:__call(x,y,width,height)
local nt = {x=x,y=y,width=width,height=height,endx=x+width,endy=y+height}
setmetatable(nt,self)
return nt
end
function rect:setX(nx)
self.x = nx
self.endx = nx+self.width
end
function rect:setY(ny)
self.y = ny
self.endy = ny+self.height
end
return rect