Mothback/data/scripts/collision.lua

77 lines
1.6 KiB
Lua

Collision = {}
--[[
Collision
[bool] isDisabled
> if true used for collision
[bool] isActive
> if true, this collision is active (on collision, duh)
[bool] isColliding
> if true, this collision is colliding
[2d pos] from - x, y
> top right corner of collision box
[2d pos] to - x, y
> bottom left corner of collision box
[int] width
> width of collision box
[int] height
> height of collision box
--]]
-- can also be called with only ox and oy, where they become the width and height instead
function Collision:New(ox,oy,tx,ty)
local o = {isColliding = false, isDisabled = false, isActive = false}
if tx ~= nil and ty ~= nil then
o.from = {x = ox, y = oy}
o.to = {x = tx, y = ty}
o.width = o.to.x - o.from.x
o.height = o.to.y - o.from.y
else
o.width = ox
o.height = oy
o.from = {x = 0, y = 0}
o.to = {x = 0, y = 0}
end
setmetatable(o, self)
self.__index = self
return o
end
function Collision:CenterAt(x, y)
self.from.x = x-self.width/2
self.from.y = y-self.height/2
self.to.x = x+self.width/2
self.to.y = y+self.height/2
end
function Collision:PlaceAt(x, y)
self.from.x = x or self.from.x
self.from.y = y or self.from.y
self.to.x = self.from.x + self.width
self.to.y = self.from.x + self.height
end
function Collision:Draw(color)
if self.isColliding == true then
love.graphics.setColor(0,1,0,0.5)
elseif self.isActive == 1 then
love.graphics.setColor(0,1,1,0.5)
elseif color == 1 then
love.graphics.setColor(1,0,0,0.5)
elseif color == 2 then
love.graphics.setColor(0,1,1,0.5)
end
love.graphics.rectangle("fill",self.from.x-Camera.pos.x, self.from.y-Camera.pos.y, self.width, self.height)
end