138 lines
4.3 KiB
Lua
138 lines
4.3 KiB
Lua
-- Chuchu by Makaron
|
|
-- The main game screen of Chuchu
|
|
Boipus = require('deps.boipushy')
|
|
Physics = require('deps.physics')
|
|
|
|
-- my own libs
|
|
crates = require('crate')
|
|
utils = require('utils')
|
|
|
|
GRAVITY = 512
|
|
PLAN_SIZE = {
|
|
marginX = 120,
|
|
marginY = 120,
|
|
w = 512,
|
|
h = 512
|
|
}
|
|
|
|
local player = {
|
|
maxSpeed = {
|
|
x = 100,
|
|
y = 100
|
|
}
|
|
}
|
|
|
|
local magnet = {
|
|
collider = nil,
|
|
side = 'LEFT'
|
|
}
|
|
|
|
local velocity = {0, 0}
|
|
local current = { }
|
|
|
|
local Chu = {}
|
|
|
|
function Chu.new(x, y, debug)
|
|
obj = {}
|
|
obj.x = x
|
|
obj.y = y
|
|
if debug == nil then debug = false end
|
|
obj.debug = debug
|
|
|
|
world = Physics(0, GRAVITY)
|
|
world:addClass('Player')
|
|
world:addClass('GamePlan')
|
|
world:addClass('Crate')
|
|
world:addClass('Pier')
|
|
world:addClass('Crane')
|
|
obj.gamePlan = world:addChain(true, {
|
|
x - PLAN_SIZE.marginX, y - PLAN_SIZE.marginY,
|
|
x + PLAN_SIZE.w + PLAN_SIZE.marginX, y - PLAN_SIZE.marginY,
|
|
x + PLAN_SIZE.w + PLAN_SIZE.marginX, y + PLAN_SIZE.h + PLAN_SIZE.marginY,
|
|
x - PLAN_SIZE.marginX, y + PLAN_SIZE.h + PLAN_SIZE.marginY
|
|
}):setClass('GamePlan')
|
|
|
|
local pierWidth, pierHeight = assets.sprites.level.pier:getWidth(), assets.sprites.level.pier:getHeight()
|
|
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()
|
|
|
|
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),
|
|
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')
|
|
--magnet.collider:setRestitution(0)
|
|
|
|
input = Boipus()
|
|
-- Controls
|
|
-- Keyboard and mouse
|
|
input:bind(love.keyboard.getKeyFromScancode('w'), 'moveUp')
|
|
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')
|
|
input:bind(love.keyboard.getKeyFromScancode('lshift'), 'detach')
|
|
|
|
crates:init(world, magnet)
|
|
crates:spawn(x + 120, y + 400)
|
|
crates:spawn(x + 150, y + 400)
|
|
|
|
return setmetatable(obj, {
|
|
__index = Chu
|
|
})
|
|
end
|
|
|
|
function Chu:update(dt)
|
|
world:update(dt)
|
|
crates:update(dt)
|
|
|
|
if input:pressed('detach') then
|
|
velocity = {0, 0}
|
|
|
|
local attached = crates:getAttached()
|
|
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
|
|
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
|
|
|
|
magnet.collider:setLinearVelocity(velocity[1], velocity[2])
|
|
end
|
|
|
|
function Chu:draw()
|
|
love.graphics.setColor(255, 255, 255, 1)
|
|
love.graphics.draw(assets.sprites.level.background, self.x, self.y)
|
|
love.graphics.draw(assets.sprites.level.clouds, self.x, self.y + 32)
|
|
love.graphics.draw(assets.sprites.level.clouds, self.x, self.y, 0, -1, 1, assets.sprites.level.clouds:getWidth())
|
|
|
|
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)
|
|
love.graphics.draw(assets.sprites.crane, self.x + 36, self.y + 80)
|
|
|
|
|
|
love.graphics.draw(assets.sprites.level.pier, self.x, self.y + PLAN_SIZE.h - 128)
|
|
|
|
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)
|
|
end
|
|
end
|
|
|
|
return Chu
|