player done, old way

This commit is contained in:
0x81c 2015-03-15 15:34:17 -04:00
parent 687939b269
commit 1156564fa2
4 changed files with 184 additions and 103 deletions

241
room.lua
View file

@ -14,23 +14,26 @@ function Room:initialize(t)
self.colorsPlayerHas = {"red", "green", "blue"}
self.exits = t.exits
self.name = t.name
self.player = Player:new(t.player.x, t.player.y, t.player.activeColors)
self.player = {}
self.switches = { red = {}, green = {}, blue = {} }
self.switchMatrices = {}
self.collideables = { red = {}, green = {}, blue = {} }
self.collidableMatrices = {} --{red = {{...}{...}{...}}, ...
for _, color in ipairs(t.player.activeColors) do
self.player[color] = Player:new(t.player.x, t.player.y, color)
end
self.doors = {}
self.activeColors = {
red = true, green = true, blue = true
}
self.activeColors = {}
for _, color in ipairs(t.player.activeColors) do self.activeColors[color] = true end
-- for color, coords in pairs(t.player) do
-- self.player[color] = Player:new(coords.x, coords.y, color)
-- self.activeColors[color] = true
-- end
for i=1, 5 do
for i=1, 30 do
local x, y = randomPosition()
local door = Door:new(x, y, self.colorsPlayerHas)
table.insert(self.doors, door)
@ -65,9 +68,9 @@ function Room:initialize(t)
local x, y = 7, 7
if active then self.collideables[color]["n: " .. x .. ", " .. y] = Ass:new(x, y, color, "food/processed/", "beer_mug") end
self.collideables[color]["fpp"] = Artifact:new(6, 6, color)
end
-- self.collideables["green"]["fpp"] = Artifact:new(6, 6, "green")
for color, entities in pairs(self.collideables) do
self.collidableMatrices[color] = self:createEntityMatrix(entities)
@ -76,12 +79,16 @@ end
function Room:update(dt)
self.player:update(dt)
for color, player in pairs(self.player) do
player:update(dt)
end
end
function Room:draw()
self.player:draw()
for color, player in pairs(self.player) do
player:draw()
end
for color, entityArray in pairs(self.collideables) do
for _, entity in pairs(entityArray) do
entity:draw()
@ -134,50 +141,40 @@ function Room:getCellInMatrix(matrix, cell)
end
function Room:movePlayer(key)
local activeColors = self.player:getActiveColors()
local pos = self.player:getGridPos()
local x, y = pos.x, pos.y
local dir = {x = 0, y = 0}
local axis
if key == "up" then
dir.y, axis = -1, "y"
elseif key == "down" then
dir.y, axis = 1, "y"
elseif key == "left" then
dir.x, axis = -1, "x"
elseif key == "right" then
dir.x, axis = 1, "x"
end
local moved = false
local move = true
local entitiesToPush = {
red = {},
green = {},
blue = {}
}
local entitiesToPull = {
red = {},
green = {},
blue = {}
}
for _, color in pairs(self:getActiveColors()) do
local entitiesToPush, entitiesToPull = {}, {}
local pos = self.player[color]:getGridPos()
local x, y = pos.x, pos.y
local dir = {x = 0, y = 0}
local axis
-- PUSHHHHHHHHHHHHHHHHHHHHHH---------------------
if key == "up" then
dir.y, axis = -1, "y"
elseif key == "down" then
dir.y, axis = 1, "y"
elseif key == "left" then
dir.x, axis = -1, "x"
elseif key == "right" then
dir.x, axis = 1, "x"
end
local initialTargetCell = {
x = pos.x + dir.x, y = pos.y + dir.y
} -- this is for reference for each color as we change the targetCells table
local move = true
-- PUSHHHHHHHHHHHHHHHHHHHHHH---------------------
if not self:isInBounds(initialTargetCell) then
move = true
elseif self:getCellInMatrix(self.collidableMatrices["red"], initialTargetCell) and self:getCellInMatrix(self.collidableMatrices["red"], initialTargetCell):canPassOver() then
move = true
else
local targetCells
local initialTargetCell = {
x = pos.x + dir.x, y = pos.y + dir.y
} -- this is for reference for each color as we change the targetCells table
if not self:isInBounds(initialTargetCell) then
move = true
elseif self:getCellInMatrix(self.collidableMatrices["red"], initialTargetCell) and self:getCellInMatrix(self.collidableMatrices["red"], initialTargetCell):canPassOver() then
move = true
else
local targetCells
--so the way this worx is....
for _, color in pairs(activeColors) do
--for each color...
targetCells = {initialTargetCell} -- ...we start with our attempted movement...
local doneSearching
while not doneSearching do
@ -188,10 +185,10 @@ function Room:movePlayer(key)
local entity = self:getCellInMatrix(self.collidableMatrices[color], cell)
if entity then
--something is here!!
if not entitiesToPush[color][entity] then --if the entity hasnt been found already
if not entitiesToPush[entity] then --if the entity hasnt been found already
if entity:canMove() then
local projectedCells = entity:getOccupiedCells(dir) -- we project the movement here
entitiesToPush[color][entity] = entity
entitiesToPush[entity] = entity
for _, projectedCell in ipairs(projectedCells) do
table.insert(nextTargetCells, projectedCell)
end
@ -220,20 +217,16 @@ function Room:movePlayer(key)
targetCells = nextTargetCells
end
--let's get out of this loop b/c if we can't move one color we can't move at all
if move == false then break end
end
end
---PULL CHEC
---PULL CHEC
local initialTargetCell = {x = pos.x - dir.x, y = pos.y - dir.y}
local initialTargetCell = {x = pos.x - dir.x, y = pos.y - dir.y}
local targetCells
local targetCells
local pull = {}
--so the way this worx is....
for _, color in pairs(activeColors) do
local pull = {}
--for each color...
targetCells = {initialTargetCell} -- ...we start with our attempted movement...
local doneSearching
@ -245,10 +238,10 @@ function Room:movePlayer(key)
local entity = self:getCellInMatrix(self.collidableMatrices[color], cell)
if entity then
--something is here!!
if not entitiesToPull[color][entity] then --if the entity hasnt been found already
if not entitiesToPull[entity] then --if the entity hasnt been found already
if entity:canMove() then
local projectedCells = entity:getOccupiedCells(dir) -- we project the movement here
entitiesToPull[color][entity] = entity
entitiesToPull[entity] = entity
for _, projectedCell in ipairs(projectedCells) do
table.insert(nextTargetCells, projectedCell)
end
@ -262,7 +255,6 @@ function Room:movePlayer(key)
table.insert(emptyCells, cell)
end
else
pull[color] = true
--we are out of bounds now
doneSearching = true
end
@ -270,58 +262,96 @@ function Room:movePlayer(key)
--if every projected cell is empty then we can do the move
if #emptyCells == #targetCells then
pull[color] = true
doneSearching = true
end
targetCells = nextTargetCells
end
--let's get out of this loop b/c if we can't move one color we can't move at all
if move == false then break end
end
if move then
for color, entities in pairs(entitiesToPush) do
for _, entity in pairs(entities) do
if move then
for _, entity in pairs(entitiesToPush) do
entity:move(axis, dir[axis])
end
end
for color, entities in pairs(entitiesToPull) do
if pull[color] then
for _, entity in pairs(entities) do
entity:move(axis, dir[axis])
end
for _, entity in pairs(entitiesToPull) do
entity:move(axis, dir[axis])
end
self.player[color]:move(axis, dir[axis])
moved = true
end
self.player:move(axis, dir[axis])
end
if moved then
self:tick()
end
end
function Room:switchPlayerColor(color)
local colors
if color == "white" then colors = {"red", "green", "blue"} else colors = {color} end
local canSwitch
local canSwitch = true
local playerPos = self.player:getGridPos()
for _, color in ipairs(colors) do
if self:getCellInMatrix(self.collidableMatrices[color], playerPos) then
canSwitch = false
if self:onlyColorIsActive(color) then
canSwitch = false
else
for _, c in pairs(self:getPossibleColors()) do
if c == color then
self:setActiveColor(c, true)
else
self:setActiveColor(c, false)
end
end
end
if canSwitch then
self.player:setActiveColors(colors)
end
self.player:playSwitchSound(canSwitch)
self.player[color]:playSwitchSound(canSwitch)
end
function Room:setActiveColor(color, bool)
self.activeColors[color] = bool
end
function Room:getActiveColors()
local colors = {}
for color, active in pairs(self.activeColors) do
if active then
table.insert(colors, color)
end
end
return colors
end
function Room:getInactiveColors()
local colors = {}
for color, active in pairs(self.activeColors) do
if not active then
table.insert(colors, color)
end
end
return colors
end
function Room:getPossibleColors()
local colors = {}
for _, color in ipairs(self.colorsPlayerHas) do
table.insert(colors, color)
print(color)
end
return colors
end
function Room:onlyColorIsActive(color)
local activeColors = self:getActiveColors()
if #activeColors == 1 and activeColors[1] == color then
return true
else
return false
end
end
function Room:tick()
for color, entityArray in pairs(self.collideables) do
for _, entity in pairs(entityArray) do
@ -335,9 +365,37 @@ function Room:tick()
print(door)
door:setColors(setDoorColors)
end
self:playerJoinCheck()
self:nextRoomCheck()
end
function Room:playerJoinCheck()
local join = {}
for _, inactiveColor in ipairs(self:getInactiveColors()) do
local overlapping = 0
local activeColors = self:getActiveColors()
local inactivePos = self:getPlayerPos(inactiveColor)
for _, activeColor in ipairs(activeColors) do
local activePos = self:getPlayerPos(activeColor)
if activePos.x == inactivePos.x and activePos.y == inactivePos.y then
overlapping = overlapping + 1
end
end
if overlapping == #activeColors then
table.insert(join, inactiveColor)
end
end
for _, color in ipairs(join) do
self:setActiveColor(color, true)
end
end
function Room:getPlayerPos(color)
return self.player[color]:getGridPos()
end
function Room:switchCheck()
local doorsToUnlock = {}
for color, switchArray in pairs(self.switches) do
@ -367,8 +425,11 @@ function Room:isInBounds(cell)
end
function Room:nextRoomCheck()
if not self:isInBounds(self.player:getGridPos()) then
nextRoom(self.player:getGridPos())
for _, color in ipairs(self:getActiveColors()) do
if not self:isInBounds(self.player[color]:getGridPos()) then
nextRoom(self.player[color]:getGridPos())
return
end
end
end
@ -391,7 +452,7 @@ function nextRoom(pos)
currentRoom = Room:new{
player = {
x = newPos.x, y = newPos.y, activeColors = currentRoom.player:getActiveColors()
x = newPos.x, y = newPos.y, activeColors = currentRoom:getActiveColors()
}
}
end