diff --git a/crate.lua b/crate.lua index eeb04cb..cecc4ff 100644 --- a/crate.lua +++ b/crate.lua @@ -2,6 +2,8 @@ -- Crates are the main thing so here we go local Timer = require('deps.knife.timer') +local utils = require('utils') + ITERATIONS_RESOLVE = .01 TIME_TO_RELOAD = .4 @@ -16,10 +18,27 @@ local Crate = { } local curContact = nil -function Crate.new(world, x, y) - local object = { - collider = world:addRectangle(x, y, 20, 20) +function Crate.new(world, x, y, size) + local object = { } + object.size = { + w = 0, + h = 0 } + + if size == 'small' then + object.size.w, object.size.h = assets.sprites.crates.small:getWidth(), assets.sprites.crates.small:getHeight() + object.img = assets.sprites.crates.small + elseif size == 'medium' then + object.size.w, object.size.h = assets.sprites.crates.medium:getWidth(), assets.sprites.crates.medium:getHeight() + object.img = assets.sprites.crates.medium + elseif size == 'large' then + object.size.w, object.size.h = assets.sprites.crates.large:getWidth(), assets.sprites.crates.large:getHeight() + object.img = assets.sprites.crates.large + end + + local center = utils.getCenterByXY(x, y, object.size.w, object.size.h) + + object.collider = world:addRectangle(x, y, object.size.w, object.size.h) object.collider:setClass('Crate') object.attached = false object.contact = { } @@ -47,14 +66,23 @@ function Crates:init(world, player) self.player = player end -function Crates:spawn(x, y) - table.insert(self.list, Crate.new(self.world, x, y)) +function Crates:spawn(x, y, size) + table.insert(self.list, Crate.new(self.world, x, y, size)) end function Crates:update(dt) Timer.update(dt) end +function Crates:draw() + local pos = {x = 0, y = 0, r = 0} + for key in pairs(self.list) do + pos.x, pos.y = self.list[key].collider:getPosition() + pos.r = self.list[key].collider:getAngle() + love.graphics.draw(self.list[key].img, pos.x, pos.y, pos.r, 1, 1, self.list[key].size.w/2, self.list[key].size.h/2) + end +end + function Crates:getAttached() return self.attached end diff --git a/game.lua b/game.lua index ae77246..d405c1a 100644 --- a/game.lua +++ b/game.lua @@ -99,8 +99,8 @@ function Chu.new(x, y, debug) input:bind(love.keyboard.getKeyFromScancode('lshift'), 'detach') crates:init(world, magnet) - crates:spawn(x + 120, y + 400) - crates:spawn(x + 150, y + 400) + crates:spawn(x + 120, y + 400, 'small') + crates:spawn(x + 150, y + 400, 'medium') return setmetatable(obj, { __index = Chu @@ -168,7 +168,9 @@ function Chu: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() + crates:draw() + + world:draw() if self.debug then love.graphics.setColor(255, 0, 0, .5)