Mothback/code/collision.lua

80 lines
1.8 KiB
Lua
Raw Permalink Normal View History

2021-10-16 23:06:11 +00:00
Collision = {}
LoadedObjects.Collisions = {}
LoadedObjects.Platforms = {}
LoadedObjects.Ladders = {}
LoadedObjects.Hazards = {}
2021-10-16 23:06:11 +00:00
--[[
Collision
2022-02-26 02:56:53 +00:00
[bool flag] isDisabled
2021-10-16 23:06:11 +00:00
> if true used for collision
2022-02-26 02:56:53 +00:00
[bool flag] isColliding
2021-10-16 23:06:11 +00:00
> if true, this collision is colliding
2022-02-26 02:56:53 +00:00
[vec2 position] from - x, y
> top right corner of collision box
2021-10-16 23:06:11 +00:00
2022-02-26 02:56:53 +00:00
[vec2 position] to - x, y
2021-10-16 23:06:11 +00:00
> bottom left corner of collision box
2022-02-26 02:56:53 +00:00
[int property] width
2021-10-16 23:06:11 +00:00
> width of collision box
2022-02-26 02:56:53 +00:00
[int property] height
2021-10-16 23:06:11 +00:00
> 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}
2021-10-16 23:06:11 +00:00
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 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)
love.graphics.setColor(0,1,90,0.5)
love.graphics.rectangle("line",self.from.x-Camera.pos.x, self.from.y-Camera.pos.y, self.width, self.height)
2021-10-16 23:06:11 +00:00
end