chroma_solstice/room.lua

204 lines
4.5 KiB
Lua
Raw Normal View History

2015-03-11 20:11:29 -04:00
Room = class("Room")
2015-03-11 21:41:02 -04:00
2015-03-11 21:08:15 -04:00
require "player"
2015-03-12 03:04:09 -04:00
require "box"
require "wall"
2015-03-12 05:13:04 -04:00
require "artifact"
2015-03-11 20:11:29 -04:00
2015-03-11 21:08:15 -04:00
function Room:initialize(t)
self.exits = t.exits
self.name = t.name
self.player = {}
2015-03-12 03:04:09 -04:00
self.collideables = {
red = {},
green = {},
blue = {}
}
self.activeColors = {
red = false, green = false, blue = false
}
2015-03-11 21:08:15 -04:00
2015-03-11 21:41:02 -04:00
for color, coords in pairs(t.player) do
2015-03-11 21:08:15 -04:00
self.player[color] = Player:new(coords.x, coords.y, color)
2015-03-12 03:04:09 -04:00
self.activeColors[color] = true
end
2015-03-12 05:13:04 -04:00
for i=0, 10 do
2015-03-12 03:04:09 -04:00
local c = randomColor()
local x, y = math.random(1, gridWidth), math.random(1, gridHeight)
self.collideables[c]["b: " .. x .. ", " .. y] = Box:new(x, y, c)
2015-03-11 21:08:15 -04:00
end
2015-03-11 20:11:29 -04:00
2015-03-11 21:41:02 -04:00
2015-03-12 05:13:04 -04:00
for i=0, 10 do
2015-03-12 03:04:09 -04:00
local c = randomColor()
local x, y = math.random(1, gridWidth), math.random(1, gridHeight)
self.collideables[c]["w: " .. x .. ", " .. y] = Wall:new(x, y, c)
end
2015-03-12 05:13:04 -04:00
self.collideables["green"]["fpp"] = Artifact:new(6, 6, "green")
2015-03-11 20:11:29 -04:00
end
2015-03-11 21:41:02 -04:00
function Room:update(dt)
for color, player in pairs(self.player) do
player:update(dt)
end
2015-03-11 20:11:29 -04:00
end
2015-03-11 21:41:02 -04:00
function Room:draw()
for color, player in pairs(self.player) do
player:draw()
end
2015-03-12 03:04:09 -04:00
for color, entityArray in pairs(self.collideables) do
for _, entity in pairs(entityArray) do
entity:draw()
end
end
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)
for color, active in pairs(self.activeColors) do
if color == colorToActivate then
self:setActiveColor(color, true)
else
self:setActiveColor(color, false)
end
end
end
function Room:setActiveColor(color, bool)
self.activeColors[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
2015-03-12 05:13:04 -04:00
if entity:isAlignedWithPlayerMovement("y", pos, direction) then
2015-03-12 03:04:09 -04:00
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
2015-03-12 05:13:04 -04:00
if entity:isAlignedWithPlayerMovement("x", pos, direction) then
2015-03-12 03:04:09 -04:00
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)
2015-03-12 05:21:24 -04:00
shallowTablePrint(t)
2015-03-12 03:04:09 -04:00
return t
end
function Room:checkAndPull(entities, pos, axis, direction)
for _, entity in pairs(entities) do
2015-03-12 05:13:04 -04:00
if axis == "x" and entity:atGridPos(pos.x - direction, pos.y) then
2015-03-12 03:04:09 -04:00
if entity:canMove() then
entity:move(axis, direction)
return
end
2015-03-12 05:13:04 -04:00
elseif axis == "y" and entity:atGridPos(pos.x, pos.y - direction) then
2015-03-12 03:04:09 -04:00
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 = {}
for _, entity in ipairs(entities) do
2015-03-12 05:13:04 -04:00
pos[axis] = pos[axis] + direction
if entity:atGridPos(pos.x, pos.y) then
2015-03-12 05:21:24 -04:00
print("HOW????" .. entity)
2015-03-12 03:04:09 -04:00
if entity:canMove() then
table.insert(entitiesToMove, entity)
else
2015-03-12 05:21:24 -04:00
print(entity)
2015-03-12 03:04:09 -04:00
return false
end
else
2015-03-12 05:13:04 -04:00
-- break
2015-03-12 03:04:09 -04:00
end
counter = counter + direction
end
for _, entity in ipairs(entitiesToMove) do
entity:move(axis, direction)
end
return true
2015-03-11 21:08:15 -04:00
end
function Room:tick()
2015-03-12 03:04:09 -04:00
for color, entityArray in pairs(self.collideables) do
for _, entity in pairs(entityArray) do
entity:tick()
end
end
2015-03-11 21:08:15 -04:00
self:nextRoomCheck()
end
function Room:nextRoomCheck()
end
function nextRoom()
2015-03-12 03:04:09 -04:00
2015-03-11 20:11:29 -04:00
end