toreit out git add -A
This commit is contained in:
parent
2f40eaf000
commit
3ae6c416e5
2 changed files with 4 additions and 109 deletions
13
entity.lua
13
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}
|
return {x = drawPos.x, y = drawPos.y, w = self.spriteSize.w * drawScale, h = self.spriteSize.h * drawScale}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Entity:getGridBox()
|
||||||
|
return {x = self.x, y = self.y, w = self.size.w, h = self.size.h}
|
||||||
|
end
|
||||||
|
|
||||||
function Entity:getGridPos()
|
function Entity:getGridPos()
|
||||||
return {x = self.x, y = self.y}
|
return {x = self.x, y = self.y}
|
||||||
end
|
end
|
||||||
|
|
@ -50,15 +54,6 @@ function Entity:draw()
|
||||||
love.graphics.draw(self.sprite, p.x, p.y, 0, drawScale, drawScale)
|
love.graphics.draw(self.sprite, p.x, p.y, 0, drawScale, drawScale)
|
||||||
end
|
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()
|
function Entity:canMove()
|
||||||
if self.collision == "unmoveable" then
|
if self.collision == "unmoveable" then
|
||||||
return false
|
return false
|
||||||
|
|
|
||||||
100
room.lua
100
room.lua
|
|
@ -77,33 +77,6 @@ end
|
||||||
|
|
||||||
function Room:movePlayer(key)
|
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
|
end
|
||||||
|
|
||||||
function Room:switchPlayer(colorToActivate)
|
function Room:switchPlayer(colorToActivate)
|
||||||
|
|
@ -121,88 +94,15 @@ function Room:setActiveColor(color, bool)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Room:getAdjacentEntities(entities, pos, axis, direction)
|
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
|
end
|
||||||
|
|
||||||
function Room:checkAndPull(entities, pos, axis, direction)
|
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
|
end
|
||||||
|
|
||||||
function Room:tryPlayerMove(entities, pos, axis, direction)
|
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
|
end
|
||||||
|
|
||||||
function Room:tick()
|
function Room:tick()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue