72 lines
1.6 KiB
Lua
72 lines
1.6 KiB
Lua
-- Chuchu by Makaron
|
|
-- Crates are the main thing so here we go
|
|
|
|
local CONST_ITERATIONS_RESOLVE = 1
|
|
|
|
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.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
|
|
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)
|
|
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
|
|
end
|
|
|
|
function Crates:getAttached()
|
|
return self.attached
|
|
end
|
|
|
|
return setmetatable(Crates, {
|
|
__call = function(_, ...) return new(...) end
|
|
})
|