added the crates and the crate object plus making them joint

This commit is contained in:
2021-05-08 17:25:31 +02:00
parent fba48548b7
commit 686f085f5b
2 changed files with 75 additions and 3 deletions

65
crate.lua Normal file
View File

@@ -0,0 +1,65 @@
-- Chuchu by Makaron
-- Crates are the main thing so here we go
local CONST_ITERATIONS_RESOLVE = 1
local Crates = {
world = nil,
player = nil,
list = { }
}
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
end
end
end
end
return setmetatable(Crates, {
__call = function(_, ...) return new(...) end
})

View File

@@ -3,6 +3,9 @@ Boipus = require('deps.boipushy')
Physics = require('deps.physics')
assets = require('deps.cargo').init('data')
-- my own libs
crates = require('crate')
local CONST_GRAVITY = 512
local CONST_SIDES = {
LEFT = {
@@ -22,8 +25,6 @@ local player = {
}
local magnet = {
x = 50,
y = 50,
w = 50,
h = 50,
collider = nil,
@@ -44,9 +45,10 @@ function love.load()
world = Physics(0, 0)
world:addClass('Player')
world:addClass('Side')
world:addClass('Crate')
side.left = world:addChain(true, CONST_SIDES.LEFT):setClass('Side')
side.right = world:addChain(true, CONST_SIDES.RIGHT):setClass('Side')
magnet.collider = world:addRectangle(magnet.x, magnet.y, magnet.w, magnet.h):setClass('Player')
magnet.collider = world:addRectangle(50, 50, magnet.w, magnet.h):setClass('Player')
magnet.collider:setRestitution(0)
input = Boipus()
@@ -58,6 +60,10 @@ function love.load()
input:bind(love.keyboard.getKeyFromScancode('s'), 'moveDown')
input:bind(love.keyboard.getKeyFromScancode('space'), 'switch')
crates:init(world, magnet)
crates:spawn(120, 400)
crates:spawn(150, 400)
-- Gamepad
--[[input:bind('dpup', 'moveUp')
input:bind('dpleft', 'moveLeft')
@@ -67,6 +73,7 @@ end
function love.update(dt)
world:update(dt)
crates:update(dt)
--[[ if input:down('moveUp') then magnet.collider:applyLinearImpulse(0, -player.maxSpeed.y) end
if input:down('moveLeft') then magnet.collider:applyLinearImpulse(-player.maxSpeed.x, 0) end