added a timer for cleaner execution, goodbye resolver!
This commit is contained in:
31
crate.lua
31
crate.lua
@@ -1,7 +1,9 @@
|
||||
-- Chuchu by Makaron
|
||||
-- Crates are the main thing so here we go
|
||||
local Timer = require('deps.knife.timer')
|
||||
|
||||
local CONST_ITERATIONS_RESOLVE = 1
|
||||
local CONST_ITERATIONS_RESOLVE = .01
|
||||
local CONST_TIME_TO_RELOAD = .4
|
||||
|
||||
local Crates = {
|
||||
world = nil,
|
||||
@@ -22,15 +24,16 @@ function Crate.new(world, x, y)
|
||||
object.attached = false
|
||||
object.contact = { }
|
||||
object.joint = nil
|
||||
object.resolver = nil
|
||||
|
||||
object.collider:setPresolve(function (shape1, shape2, contact)
|
||||
print(shape2:getClass())
|
||||
if shape2:getClass() == 'Player' and object.attached == false then
|
||||
contact:setEnabled(false)
|
||||
object.contact.x, object.contact.y = contact:getPositions()
|
||||
object.resolver = CONST_ITERATIONS_RESOLVE
|
||||
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)
|
||||
|
||||
@@ -49,23 +52,21 @@ function Crates:spawn(x, y)
|
||||
end
|
||||
|
||||
function Crates:update(dt)
|
||||
for key in pairs(self.list) do
|
||||
if self.list[key].resolver ~= nil then
|
||||
self.list[key].resolver = self.list[key].resolver - 1
|
||||
if self.list[key].resolver <= 0 then
|
||||
curContact = self.list[key].contact
|
||||
self.world:addJoint('revolute', self.player.collider, self.list[key].collider, curContact.x, curContact.y, true)
|
||||
self.list[key].resolver = nil
|
||||
table.insert(self.attached, self.list[key])
|
||||
end
|
||||
end
|
||||
end
|
||||
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
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user