diff --git a/crate.lua b/crate.lua index a004b4b..7c9ecfd 100644 --- a/crate.lua +++ b/crate.lua @@ -6,7 +6,8 @@ local CONST_ITERATIONS_RESOLVE = 1 local Crates = { world = nil, player = nil, - list = { } + list = { }, + attached = { } } local Crate = { } @@ -55,11 +56,16 @@ function Crates:update(dt) curContact = self.list[key].contact self.world:addJoint('revolute', self.player.collider, self.list[key].collider, curContact.x, curContact.y, true) self.list[key].resolver = nil + table.insert(self.attached, self.list[key]) end end end end +function Crates:getAttached() + return self.attached +end + return setmetatable(Crates, { __call = function(_, ...) return new(...) end }) diff --git a/main.lua b/main.lua index 6bdc6a0..fbdb9db 100644 --- a/main.lua +++ b/main.lua @@ -39,6 +39,8 @@ local side = { local velocity = {0, 0} local current = { } +local tempAttached = nil + function love.load() love.window.setMode(512, 512) @@ -83,6 +85,25 @@ function love.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 + 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) + end + current.x, current.y = magnet.collider:getPosition() -- i want a relative x position, for easier use, so first