diff --git a/main.lua b/main.lua index fbdb9db..8ac8e0d 100644 --- a/main.lua +++ b/main.lua @@ -41,6 +41,18 @@ local current = { } local tempAttached = nil +function mirrorCollider(collider, side) + current.x, current.y = collider:getPosition() + if side == 'LEFT' then + current.x = current.x - CONST_SIDES.LEFT[1] + current.x = CONST_SIDES.RIGHT[1] + CONST_SIDES.WIDTH - current.x + else + current.x = current.x - CONST_SIDES.RIGHT[1] + current.x = CONST_SIDES.LEFT[1] + CONST_SIDES.WIDTH - current.x + end + collider:setPosition(current.x, current.y) +end + function love.load() love.window.setMode(512, 512) @@ -88,40 +100,16 @@ function love.update(dt) -- first, let's take care of the attached crates for key in pairs(crates:getAttached()) do - current.x, current.y = crates.attached[key].collider:getPosition() - - if magnet.side == 'LEFT' then - current.x = current.x - CONST_SIDES.LEFT[1] - - -- then, we need to mirror it onto the other side - current.x = CONST_SIDES.RIGHT[1] + CONST_SIDES.WIDTH - current.x - else - current.x = current.x - CONST_SIDES.RIGHT[1] - - current.x = CONST_SIDES.LEFT[1] + CONST_SIDES.WIDTH - current.x - end - - crates.attached[key].collider:setPosition(current.x, current.y) + mirrorCollider(crates.attached[key].collider, magnet.side) end - current.x, current.y = magnet.collider:getPosition() + mirrorCollider(magnet.collider, magnet.side) - -- i want a relative x position, for easier use, so first - -- let's substract the x position depending on the side if magnet.side == 'LEFT' then - current.x = current.x - CONST_SIDES.LEFT[1] - - -- then, we need to mirror it onto the other side - current.x = CONST_SIDES.RIGHT[1] + CONST_SIDES.WIDTH - current.x magnet.side = 'RIGHT' else - current.x = current.x - CONST_SIDES.RIGHT[1] - - current.x = CONST_SIDES.LEFT[1] + CONST_SIDES.WIDTH - current.x magnet.side = 'LEFT' end - - magnet.collider:setPosition(current.x, current.y) else if 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