did some physics mirroring etc
This commit is contained in:
97
main.lua
97
main.lua
@@ -1,23 +1,54 @@
|
||||
-- Chuchu by Makaron
|
||||
Boipus = require('deps.boipushy')
|
||||
BF = require('deps.breezefield')
|
||||
Physics = require('deps.physics')
|
||||
assets = require('deps.cargo').init('data')
|
||||
|
||||
local CONST_GRAVITY = 512
|
||||
local CONST_SIDES = {
|
||||
LEFT = {
|
||||
10, 10, 200, 10, 200, 500, 10, 500
|
||||
},
|
||||
RIGHT = {
|
||||
310, 10, 502, 10, 502, 502, 310, 502
|
||||
},
|
||||
WIDTH = 190
|
||||
}
|
||||
|
||||
local player = {
|
||||
maxSpeed = {
|
||||
x = 2,
|
||||
y = 2
|
||||
x = 100,
|
||||
y = 100
|
||||
}
|
||||
}
|
||||
|
||||
local magnet = {
|
||||
x = 50,
|
||||
y = 50
|
||||
y = 50,
|
||||
w = 50,
|
||||
h = 50,
|
||||
collider = nil,
|
||||
side = 'LEFT'
|
||||
}
|
||||
|
||||
local side = {
|
||||
left = nil,
|
||||
right = nil
|
||||
}
|
||||
|
||||
local velocity = {0, 0}
|
||||
local current = { }
|
||||
|
||||
function love.load()
|
||||
love.window.setMode(512, 512)
|
||||
|
||||
world = Physics(0, 0)
|
||||
world:addClass('Player')
|
||||
world:addClass('Side')
|
||||
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:setRestitution(0)
|
||||
|
||||
input = Boipus()
|
||||
-- Controls
|
||||
-- Keyboard and mouse
|
||||
@@ -25,6 +56,7 @@ function love.load()
|
||||
input:bind(love.keyboard.getKeyFromScancode('a'), 'moveLeft')
|
||||
input:bind(love.keyboard.getKeyFromScancode('d'), 'moveRight')
|
||||
input:bind(love.keyboard.getKeyFromScancode('s'), 'moveDown')
|
||||
input:bind(love.keyboard.getKeyFromScancode('space'), 'switch')
|
||||
|
||||
-- Gamepad
|
||||
--[[input:bind('dpup', 'moveUp')
|
||||
@@ -34,18 +66,61 @@ function love.load()
|
||||
end
|
||||
|
||||
function love.update(dt)
|
||||
-- controls(dt)
|
||||
if input:down('moveUp') then magnet.y = magnet.y - player.maxSpeed.y end
|
||||
if input:down('moveLeft') then magnet.x = magnet.x - player.maxSpeed.x end
|
||||
if input:down('moveRight') then magnet.x = magnet.x + player.maxSpeed.x end
|
||||
if input:down('moveDown') then magnet.y = magnet.y + player.maxSpeed.y end
|
||||
world: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
|
||||
if input:down('moveRight') then magnet.collider:applyLinearImpulse(player.maxSpeed.x, 0) end
|
||||
if input:down('moveDown') then magnet.collider:applyLinearImpulse(0, player.maxSpeed.y) end ]]--
|
||||
|
||||
-- this is temp, ofc, it's ugly af
|
||||
if input:pressed('switch') then
|
||||
velocity = {0, 0}
|
||||
current.x, current.y = magnet.collider:getPosition()
|
||||
|
||||
-- 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
|
||||
elseif input:down('moveDown') then velocity[2] = player.maxSpeed.y
|
||||
else velocity[2] = 0 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
|
||||
else
|
||||
velocity = {0, 0}
|
||||
end
|
||||
end
|
||||
|
||||
magnet.collider:setLinearVelocity(velocity[1], velocity[2])
|
||||
end
|
||||
|
||||
function love.draw()
|
||||
love.graphics.clear(255, 255, 255)
|
||||
--[[love.graphics.clear(255, 255, 255)
|
||||
|
||||
love.graphics.setColor(0, 0, 0, 1)
|
||||
love.graphics.rectangle('line', magnet.x, magnet.y, 32, 32)
|
||||
love.graphics.polygon('line', CONST_SIDES.LEFT)
|
||||
love.graphics.rectangle('line', magnet.x, magnet.y, 32, 32) ]]--
|
||||
|
||||
world:draw()
|
||||
|
||||
current.x, current.y = magnet.collider:getPosition()
|
||||
love.graphics.print('Pos X: ' .. current.x .. ', Pos Y: ' .. current.y)
|
||||
|
||||
-- love.graphics.print('Key code: ' .. key)
|
||||
--love.graphics.draw(assets.sprites.crane)
|
||||
|
||||
Reference in New Issue
Block a user