73 lines
1.6 KiB
Lua
73 lines
1.6 KiB
Lua
-- Chuchu by Makaron
|
|
-- Crates are the main thing so here we go
|
|
local Timer = require('deps.knife.timer')
|
|
|
|
local CONST_ITERATIONS_RESOLVE = .01
|
|
local CONST_TIME_TO_RELOAD = .4
|
|
|
|
local Crates = {
|
|
world = nil,
|
|
player = nil,
|
|
list = { },
|
|
attached = { }
|
|
}
|
|
|
|
local Crate = { }
|
|
|
|
local curContact = nil
|
|
|
|
function Crate.new(world, x, y)
|
|
local object = {
|
|
collider = world:addRectangle(x, y, 20, 20)
|
|
}
|
|
object.collider:setClass('Crate')
|
|
object.attached = false
|
|
object.contact = { }
|
|
object.joint = nil
|
|
|
|
object.collider:setPresolve(function (shape1, shape2, contact)
|
|
if shape2:getClass() == 'Player' and object.attached == false then
|
|
contact:setEnabled(false)
|
|
object.contact.x, object.contact.y = contact:getPositions()
|
|
object.attached = true
|
|
Timer.after(CONST_ITERATIONS_RESOLVE, function ()
|
|
object.joint = Crates.world:addJoint('revolute', Crates.player.collider, object.collider, object.contact.x, object.contact.y, true)
|
|
table.insert(Crates.attached, object)
|
|
end)
|
|
end
|
|
end)
|
|
|
|
return setmetatable(object, {
|
|
__index = Crate
|
|
})
|
|
end
|
|
|
|
function Crates:init(world, player)
|
|
self.world = world
|
|
self.player = player
|
|
end
|
|
|
|
function Crates:spawn(x, y)
|
|
table.insert(self.list, Crate.new(self.world, x, y))
|
|
end
|
|
|
|
function Crates:update(dt)
|
|
Timer.update(dt)
|
|
end
|
|
|
|
function Crates:getAttached()
|
|
return self.attached
|
|
end
|
|
|
|
function Crates:detach(key)
|
|
self.attached[key].joint._joint:destroy()
|
|
Timer.after(CONST_TIME_TO_RELOAD, function ()
|
|
self.attached[key].attached = false
|
|
table.remove(self.attached, key)
|
|
end)
|
|
end
|
|
|
|
return setmetatable(Crates, {
|
|
__call = function(_, ...) return new(...) end
|
|
})
|