fixed overlap chec

This commit is contained in:
0x81c 2015-03-16 08:47:08 -04:00
parent df7dd1b1f3
commit cd2e0d862a
4 changed files with 44 additions and 11 deletions

View file

@ -6,7 +6,6 @@ function Box:initialize(x, y, color)
Entity.initialize(self, {
x = x,
y = y,
name = "box",
sizeX = 1,
sizeY = 1,
color = color,

View file

@ -10,7 +10,7 @@ function Entity:initialize(t)
self.collision = t.collision --"none", "moveable", "unmoveable"
self.sprite = t.sprite
self.shaAmount = t.sha or 1
self.onBump = t.onBump or function() return true end
self.onBump = t.onBump or function(self) return true end
self.drawOffset = {x = 0, y = 0}
if self.size.w < 1 or self.size.h < 1 then
@ -19,6 +19,13 @@ function Entity:initialize(t)
end
end
Entity.static.positionsAreEqual = function(cell1, cell2)
if cell1.x == cell2.x and cell1.y == cell2.y then
return true
end
return false
end
function Entity:getCollision()
return self.collision
end
@ -126,5 +133,5 @@ function Entity:sha()
end
function Entity:bump()
return self.onBump()
return self:onBump()
end

View file

@ -5,7 +5,10 @@ function npc:initialize(x, y, color, name)
x = x,
y = y,
color = color,
collision = "none", --player can't push other player
sprite = sprites.npc[name][color]
collision = "unmoveable",
sprite = sprites.npc[name][color],
onBump = function(self)
self.tal_ing = true
end,
})
end

View file

@ -143,10 +143,13 @@ end
function Room:movePlayer(key)
local moved = false
local previousPositions = {} --these are so we can perform the overlap chec_
local newPositions = {}
for _, color in pairs(self:getActiveColors()) do
local entitiesToPush, entitiesToPull = {}, {}
local pos = self.player[color]:getGridPos()
previousPositions[color] = pos
local x, y = pos.x, pos.y
local dir = {x = 0, y = 0}
local axis
@ -288,10 +291,11 @@ function Room:movePlayer(key)
self.player[color]:move(axis, dir[axis])
moved = true
end
newPositions[color] = self.player[color]:getGridPos()
end
if moved then
self:playerJoinCheck(previousPositions, newPositions)
self:tick()
end
end
@ -330,6 +334,13 @@ function Room:getActiveColors()
return colors
end
function Room:colorIsActive(color)
if self.activeColors[color] then
return true
end
return false
end
function Room:getInactiveColors()
local colors = {}
@ -375,12 +386,21 @@ function Room:tick()
print(door)
door:setColors(setDoorColors)
end
self:playerJoinCheck()
self:nextRoomCheck()
end
function Room:playerJoinCheck()
local join = {}
function Room:playerJoinCheck(previousPositions, newPositions)
local activate = {}
local deactivate = {}
local activeColors = self:getActiveColors()
for _, color in ipairs(activeColors) do
if #activeColors > 1 then
if Entity.positionsAreEqual(previousPositions[color], newPositions[color]) then
table.insert(deactivate, color)
end
end
end
for _, inactiveColor in ipairs(self:getInactiveColors()) do
local overlapping = 0
@ -393,13 +413,17 @@ function Room:playerJoinCheck()
end
end
if overlapping == #activeColors then
table.insert(join, inactiveColor)
table.insert(activate, inactiveColor)
end
end
for _, color in ipairs(join) do
for _, color in ipairs(activate) do
self:setActiveColor(color, true)
end
for _, color in ipairs(deactivate) do
self:setActiveColor(color, false)
end
end
function Room:getPlayerPos(color)