From 686f085f5b7d26bac8b045443b7a21aace1d289e Mon Sep 17 00:00:00 2001 From: Vodkannelle Date: Sat, 8 May 2021 17:25:31 +0200 Subject: [PATCH] added the crates and the crate object plus making them joint --- crate.lua | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.lua | 13 ++++++++--- 2 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 crate.lua diff --git a/crate.lua b/crate.lua new file mode 100644 index 0000000..a004b4b --- /dev/null +++ b/crate.lua @@ -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 +}) diff --git a/main.lua b/main.lua index 6e991aa..6bdc6a0 100644 --- a/main.lua +++ b/main.lua @@ -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