diff --git a/entity.lua b/entity.lua index 52f1d99..92d8c48 100644 --- a/entity.lua +++ b/entity.lua @@ -24,6 +24,10 @@ function Entity:getDrawBox() return {x = drawPos.x, y = drawPos.y, w = self.spriteSize.w * drawScale, h = self.spriteSize.h * drawScale} end +function Entity:getGridBox() + return {x = self.x, y = self.y, w = self.size.w, h = self.size.h} +end + function Entity:getGridPos() return {x = self.x, y = self.y} end @@ -50,15 +54,6 @@ function Entity:draw() love.graphics.draw(self.sprite, p.x, p.y, 0, drawScale, drawScale) end -function Entity:isAlignedWithPlayerMovement(axis, pos, direction) - -- print(axis, pos[axis], self:getGridPos()[axis], self:getGridPos()[axis] + self.size[axis]) - if self:getGridPos()[axis] <= pos[axis] and self:getGridPos()[axis] + self:getGridSize()[axis] > pos[axis] then - print("true!!") - return true - end - return false -end - function Entity:canMove() if self.collision == "unmoveable" then return false diff --git a/room.lua b/room.lua index 562e7da..f14a963 100644 --- a/room.lua +++ b/room.lua @@ -77,33 +77,6 @@ end function Room:movePlayer(key) - local moveAxis - local direction - - if key == "up" then - moveAxis, direction = "y", -1 - elseif key == "down" then - moveAxis, direction = "y", 1 - elseif key == "left" then - moveAxis, direction = "x", -1 - elseif key == "right" then - moveAxis, direction = "x", 1 - else - error("wtf did u do") - end - - for color, active in pairs(self.activeColors) do - if active then - local playerPos = self.player[color]:getGridPos() - local entities = self:getAdjacentEntities(self.collideables[color], playerPos, moveAxis, direction) - local canMove = self:tryPlayerMove(entities, playerPos, moveAxis, direction) - if canMove then - self:checkAndPull(self.collideables[color], playerPos, moveAxis, direction) - self.player[color]:move(moveAxis, direction) - self:tick() - end - end - end end function Room:switchPlayer(colorToActivate) @@ -121,88 +94,15 @@ function Room:setActiveColor(color, bool) end function Room:getAdjacentEntities(entities, pos, axis, direction) - local t = {} - --first we gather all entities in direction of movement - for _, entity in pairs(entities) do - if axis == "x" then - if entity:isAlignedWithPlayerMovement("y", pos, direction) then - if direction > 0 and entity:getGridPos()[axis] > pos[axis] then - table.insert(t, entity) - elseif direction < 0 and entity:getGridPos()[axis] < pos[axis] then - table.insert(t, entity) - end - end - elseif axis == "y" then - if entity:isAlignedWithPlayerMovement("x", pos, direction) then - if direction > 0 and entity:getGridPos()[axis] > pos[axis] then - table.insert(t, entity) - elseif direction < 0 and entity:getGridPos()[axis] < pos[axis] then - table.insert(t, entity) - end - end - end - end - - --then we put them in order - table.sort(t, function(a, b) - if direction > 0 then - return a:getGridPos()[axis] < b:getGridPos()[axis] - else - return a:getGridPos()[axis] > b:getGridPos()[axis] - end - end) - - shallowTablePrint(t) - - return t end function Room:checkAndPull(entities, pos, axis, direction) - for _, entity in pairs(entities) do - if axis == "x" and entity:atGridPos(pos.x - direction, pos.y) then - if entity:canMove() then - entity:move(axis, direction) - return - end - elseif axis == "y" and entity:atGridPos(pos.x, pos.y - direction) then - if entity:canMove() then - entity:move(axis, direction) - return - end - end - end end function Room:tryPlayerMove(entities, pos, axis, direction) - local counter = direction - local entitiesToMove = {} - local origPosAxis = pos[axis] - for _, entity in ipairs(entities) do - pos[axis] = pos[axis] + direction - print(pos[axis]) - print(origPosAxis + counter) - if entity:atGridPos(pos.x, pos.y) then - if entity:canMove() then - table.insert(entitiesToMove, entity) - else - print(entity) - return false - end - else - break - end - counter = counter + direction - end - - for _, entity in ipairs(entitiesToMove) do - entity:move(axis, direction) - end - print("wowe true") - - return true end function Room:tick()