diff --git a/game.lua b/game.lua new file mode 100644 index 0000000..ac897c4 --- /dev/null +++ b/game.lua @@ -0,0 +1,127 @@ +-- Chuchu by Makaron +-- The main game screen of Chuchu +Boipus = require('deps.boipushy') +Physics = require('deps.physics') +assets = require('deps.cargo').init('data') + +-- my own libs +crates = require('crate') + +GRAVITY = 512 +PLAN_SIZE = { + marginX = 120, + marginY = 120, + w = 512, + h = 512 +} + +local player = { + maxSpeed = { + x = 100, + y = 100 + } +} + +local magnet = { + w = 50, + h = 50, + 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') + 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') + + magnet.collider = world:addRectangle(x + 50, y + 50, magnet.w, magnet.h):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) + + -- this is temp, ofc, it's ugly af + --[[if input:pressed('switch') then + velocity = {0, 0} + + -- first, let's take care of the attached crates + for key in pairs(crates:getAttached()) do + mirrorCollider(crates.attached[key].collider, magnet.side) + end + + mirrorCollider(magnet.collider, magnet.side) + + if magnet.side == 'LEFT' then + magnet.side = 'RIGHT' + else + magnet.side = 'LEFT' + end]]-- + 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() + 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 diff --git a/main.lua b/main.lua index 892e0b5..faa7a41 100644 --- a/main.lua +++ b/main.lua @@ -1,137 +1,23 @@ -- Chuchu by Makaron -Boipus = require('deps.boipushy') -Physics = require('deps.physics') -assets = require('deps.cargo').init('data') - --- my own libs -crates = require('crate') - DEBUG_MODE = true +-- my own libs +Chu = require 'game' -GRAVITY = 512 -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 = 100, - y = 100 - } -} - -local magnet = { - w = 50, - h = 50, - collider = nil, - side = 'LEFT' -} - -local side = { - left = nil, - right = nil -} - -local velocity = {0, 0} -local current = { } +local game = nil function love.load() if DEBUG_MODE then love.window.setMode(1024, 900) else love.window.setMode(512, 512) end - world = Physics(0, 0) - world:addClass('Player') - world:addClass('Side') - world:addClass('Crate') - side.left = world:addChain(true, SIDES.LEFT):setClass('Side') - side.right = world:addChain(true, SIDES.RIGHT):setClass('Side') - magnet.collider = world:addRectangle(50, 50, magnet.w, magnet.h):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(120, 400) - crates:spawn(150, 400) - - -- Gamepad - --[[input:bind('dpup', 'moveUp') - input:bind('dpleft', 'moveLeft') - input:bind('dpright', 'moveRight') - input:bind('dpdown', 'moveDown')]]-- + game = Chu.new(200, 200, DEBUG_MODE) end function love.update(dt) - world:update(dt) - crates:update(dt) - - -- this is temp, ofc, it's ugly af - if input:pressed('switch') then - velocity = {0, 0} - - -- first, let's take care of the attached crates - for key in pairs(crates:getAttached()) do - mirrorCollider(crates.attached[key].collider, magnet.side) - end - - mirrorCollider(magnet.collider, magnet.side) - - if magnet.side == 'LEFT' then - magnet.side = 'RIGHT' - else - magnet.side = 'LEFT' - end - elseif input:pressed('detach') then - velocity = {0, 0} - - local attached = crates:getAttached() - for key in pairs(attached) do - crates:detach(key) - end - 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]) + game:update(dt) end function love.draw() - world:draw() + game:draw() if DEBUG_MODE then love.graphics.print('DEBUG MODE IS ENABLED', 850, 10) end end - -function mirrorCollider(collider, side) - current.x, current.y = collider:getPosition() - if side == 'LEFT' then - current.x = current.x - SIDES.LEFT[1] - current.x = SIDES.RIGHT[1] + SIDES.WIDTH - current.x - else - current.x = current.x - SIDES.RIGHT[1] - current.x = SIDES.LEFT[1] + SIDES.WIDTH - current.x - end - collider:setPosition(current.x, current.y) -end