From 77b25197e770bdb8c1a6fc42f08dbf723bc9f4fa Mon Sep 17 00:00:00 2001 From: Vodkannelle Date: Thu, 13 May 2021 20:23:05 +0200 Subject: [PATCH] integrated rope mechanic and drew every part of the crane --- game.lua | 74 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 59 insertions(+), 15 deletions(-) diff --git a/game.lua b/game.lua index 94e1bb6..ae77246 100644 --- a/game.lua +++ b/game.lua @@ -19,12 +19,25 @@ local player = { maxSpeed = { x = 100, y = 100 + }, + magSpeed = 2 +} + +local movingStuff +local movingStuffHeight +local movingSutffWidth +local magnet = { + collider = nil, + size = { + width = nil, + height = nil } } -local magnet = { - collider = nil, - side = 'LEFT' +local stuffToMagnetJoint +local joint = { + min = 50, + max = 250 } local velocity = {0, 0} @@ -56,19 +69,25 @@ function Chu.new(x, y, debug) obj.pier = utils.spawnStaticRectangleBySize(world, x - PLAN_SIZE.marginX, y + PLAN_SIZE.h - pierHeight, pierWidth + PLAN_SIZE.marginX, pierHeight) local craneWidth, craneHeight = assets.sprites.crane:getWidth(), assets.sprites.crane:getHeight() + movingStuffWidth, movingStuffHeight = assets.sprites.movingstuff:getWidth(), assets.sprites.movingstuff:getHeight() obj.crane = { - top = utils.spawnStaticRectangleBySize(world, x + 36, y + 80, craneWidth, 6), - left = utils.spawnStaticRectangleBySize(world, x + 36, y + 86, 6, craneHeight - 12), - right = utils.spawnStaticRectangleBySize(world, x + 30 + craneWidth, y + 86, 6, craneHeight - 12), + top = utils.spawnStaticRectangleBySize(world, x + 36, y + 80 + movingStuffHeight - 13, craneWidth, 6), + left = utils.spawnStaticRectangleBySize(world, x + 36, y + 80 + movingStuffHeight - 12 + 6, 6, craneHeight - 32), + right = utils.spawnStaticRectangleBySize(world, x + 30 + craneWidth, y + 80 + movingStuffHeight - 12 + 6, 6, craneHeight - 32), bottom = utils.spawnStaticRectangleBySize(world, x + 36, y + 74 + craneHeight, craneWidth, 6) } - local magnetWidth, magnetHeight = assets.sprites.movingstuff:getWidth(), assets.sprites.movingstuff:getHeight() - local center = utils.getCenterByXY(x + 43, y + 87, magnetWidth, magnetHeight) - magnet.collider = world:addRectangle(center.x, center.y, magnetWidth, magnetHeight):setClass('Player') + local center = utils.getCenterByXY(x + 36 + (craneWidth/2) - (movingStuffWidth/2), y + craneHeight + 80 - movingStuffHeight - 6, movingStuffWidth, movingStuffHeight) + movingStuff = world:addRectangle(center.x, center.y, movingStuffWidth, movingStuffHeight):setClass('Player') + + magnet.size.width, magnet.size.height = assets.sprites.magnet:getWidth(), assets.sprites.magnet:getHeight() + local centerMag = utils.getCenterByXY(x + 36 + (craneWidth/2) - (magnet.size.width/2), y + craneHeight + 80 + 50, magnet.size.width, magnet.size.height) + magnet.collider = world:addRectangle(centerMag.x, centerMag.y, magnet.size.width, magnet.size.height):setClass('Player') --magnet.collider:setRestitution(0) + stuffToMagnetJoint = world:addJoint('distance', movingStuff, magnet.collider, center.x, center.y, centerMag.x, centerMag.y) + input = Boipus() -- Controls -- Keyboard and mouse @@ -99,10 +118,17 @@ function Chu:update(dt) for key in pairs(attached) do crates:detach(key) end - elseif input:down('moveUp') or input:down('moveLeft') or input:down('moveRight') or input:down('moveDown') then - if input:down('moveUp') then velocity[2] = -player.maxSpeed.y - elseif input:down('moveDown') then velocity[2] = player.maxSpeed.y - else velocity[2] = 0 end + elseif input:down('moveUp') or input:down('moveDown') or input:down('moveLeft') or input:down('moveRight') then + local length = stuffToMagnetJoint:getLength() + if input:down('moveUp') then + length = length - player.magSpeed + if length < joint.min then length = joint.min end + stuffToMagnetJoint:setLength(length) + elseif input:down('moveDown') then + length = length + player.magSpeed + if length > joint.max then length = joint.max end + stuffToMagnetJoint:setLength(length) + end if input:down('moveLeft') then velocity[1] = -player.maxSpeed.x elseif input:down('moveRight') then velocity[1] = player.maxSpeed.x else velocity[1] = 0 end @@ -110,7 +136,7 @@ function Chu:update(dt) velocity = {0, 0} end - magnet.collider:setLinearVelocity(velocity[1], velocity[2]) + movingStuff:setLinearVelocity(velocity[1], velocity[2]) end function Chu:draw() @@ -121,16 +147,34 @@ function Chu:draw() love.graphics.draw(assets.sprites.cranevert, self.x + 60, self.y + PLAN_SIZE.h - 456) love.graphics.draw(assets.sprites.cranevert, self.x + PLAN_SIZE.w - 112, self.y + PLAN_SIZE.h - 456) + + local stuffPosX, stuffPosY = movingStuff:getPosition() + local otherPosX, otherPosY = magnet.collider:getPosition() + + love.graphics.setColor(0, 0, 0, 1) + love.graphics.line(stuffPosX, stuffPosY, otherPosX, otherPosY) + love.graphics.setColor(255, 255, 255, 1) + + local stuffPos = utils.getXYByCenter(stuffPosX, stuffPosY, movingStuffWidth, movingStuffHeight) + love.graphics.draw(assets.sprites.movingstuff, stuffPos.x, stuffPos.y) + love.graphics.draw(assets.sprites.crane, self.x + 36, self.y + 80) + love.graphics.draw(assets.sprites.wheel, stuffPos.x + 9, stuffPos.y + movingStuffHeight - 9) + love.graphics.draw(assets.sprites.wheel, stuffPos.x + movingStuffWidth - 23, stuffPos.y + movingStuffHeight - 9) love.graphics.draw(assets.sprites.level.pier, self.x, self.y + PLAN_SIZE.h - 128) - world:draw() + otherPosX, otherPosY = magnet.collider:getPosition() + love.graphics.draw(assets.sprites.magnet, otherPosX, otherPosY, magnet.collider:getAngle(), 1, 1, magnet.size.width/2, magnet.size.height/2) + + --world:draw() if self.debug then love.graphics.setColor(255, 0, 0, .5) love.graphics.rectangle('line', self.x, self.y, PLAN_SIZE.w, PLAN_SIZE.h) + + love.graphics.print(stuffToMagnetJoint:getLength(), 10, 10) end end