level serializes now
This commit is contained in:
parent
7fa31726f4
commit
d76ace8f69
2 changed files with 81 additions and 12 deletions
35
entity.lua
35
entity.lua
|
|
@ -2,7 +2,6 @@ Entity = class("Entity")
|
||||||
|
|
||||||
function Entity:initialize(t)
|
function Entity:initialize(t)
|
||||||
self.x, self.y = t.x, t.y
|
self.x, self.y = t.x, t.y
|
||||||
self.name = t.name
|
|
||||||
self.spriteSize = {w = t.sprite:getWidth(), h = t.sprite:getHeight()}
|
self.spriteSize = {w = t.sprite:getWidth(), h = t.sprite:getHeight()}
|
||||||
self.size = {w = self.spriteSize.w / 16, h = self.spriteSize.h / 16}
|
self.size = {w = self.spriteSize.w / 16, h = self.spriteSize.h / 16}
|
||||||
self.cellShape = t.cellShape or self:cellShapeFromBox(self.size)
|
self.cellShape = t.cellShape or self:cellShapeFromBox(self.size)
|
||||||
|
|
@ -11,6 +10,7 @@ function Entity:initialize(t)
|
||||||
self.sprite = t.sprite
|
self.sprite = t.sprite
|
||||||
self.shaAmount = t.sha or 1
|
self.shaAmount = t.sha or 1
|
||||||
self.onBump = t.onBump or function(self) return true end
|
self.onBump = t.onBump or function(self) return true end
|
||||||
|
self.tick = t.tick or function(self) return true end
|
||||||
|
|
||||||
self.drawOffset = {x = 0, y = 0}
|
self.drawOffset = {x = 0, y = 0}
|
||||||
if self.size.w < 1 or self.size.h < 1 then
|
if self.size.w < 1 or self.size.h < 1 then
|
||||||
|
|
@ -19,13 +19,6 @@ function Entity:initialize(t)
|
||||||
end
|
end
|
||||||
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()
|
function Entity:getCollision()
|
||||||
return self.collision
|
return self.collision
|
||||||
end
|
end
|
||||||
|
|
@ -95,8 +88,12 @@ function Entity:getGridSize()
|
||||||
return {x = self.size.w, y = self.size.h}
|
return {x = self.size.w, y = self.size.h}
|
||||||
end
|
end
|
||||||
|
|
||||||
function Entity:tick()
|
function Entity:getname()
|
||||||
|
return self.name
|
||||||
|
end
|
||||||
|
|
||||||
|
function Entity:tick()
|
||||||
|
return self.tick()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Entity:draw()
|
function Entity:draw()
|
||||||
|
|
@ -119,6 +116,18 @@ function Entity:canPassOver()
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Entity:getClass()
|
||||||
|
return self.class.name
|
||||||
|
end
|
||||||
|
|
||||||
|
function Entity:getSerializationTable()
|
||||||
|
local pos = self:getGridPos()
|
||||||
|
local table = {}
|
||||||
|
table.x, table.y, table.color = pos.x, pos.y, color
|
||||||
|
if self.name then table.name = self.name end
|
||||||
|
|
||||||
|
return table
|
||||||
|
end
|
||||||
|
|
||||||
function Entity:move(axis, direction)
|
function Entity:move(axis, direction)
|
||||||
self[axis] = self[axis] + direction
|
self[axis] = self[axis] + direction
|
||||||
|
|
@ -135,3 +144,11 @@ end
|
||||||
function Entity:bump()
|
function Entity:bump()
|
||||||
return self:onBump()
|
return self:onBump()
|
||||||
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
|
||||||
|
|
|
||||||
56
room.lua
56
room.lua
|
|
@ -18,6 +18,7 @@ function Room:initialize(t)
|
||||||
self.player = {}
|
self.player = {}
|
||||||
self.switches = { red = {}, green = {}, blue = {} }
|
self.switches = { red = {}, green = {}, blue = {} }
|
||||||
self.switchMatrices = {}
|
self.switchMatrices = {}
|
||||||
|
self.doors = {}
|
||||||
self.collideables = { red = {}, green = {}, blue = {} }
|
self.collideables = { red = {}, green = {}, blue = {} }
|
||||||
self.collidableMatrices = {} --{red = {{...}{...}{...}}, ...
|
self.collidableMatrices = {} --{red = {{...}{...}{...}}, ...
|
||||||
|
|
||||||
|
|
@ -25,7 +26,6 @@ function Room:initialize(t)
|
||||||
self.player[color] = Player:new(t.player.x, t.player.y, color)
|
self.player[color] = Player:new(t.player.x, t.player.y, color)
|
||||||
end
|
end
|
||||||
|
|
||||||
self.doors = {}
|
|
||||||
self.activeColors = {}
|
self.activeColors = {}
|
||||||
for _, color in ipairs(t.player.activeColors) do self.activeColors[color] = true end
|
for _, color in ipairs(t.player.activeColors) do self.activeColors[color] = true end
|
||||||
|
|
||||||
|
|
@ -434,6 +434,22 @@ function Room:getPlayerPos(color)
|
||||||
return self.player[color]:getGridPos()
|
return self.player[color]:getGridPos()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Room:getPlayerObject(color)
|
||||||
|
return self.player[color]
|
||||||
|
end
|
||||||
|
|
||||||
|
function Room:getDoors()
|
||||||
|
return self.doors
|
||||||
|
end
|
||||||
|
|
||||||
|
function Room:getCollideables()
|
||||||
|
return self.collideables
|
||||||
|
end
|
||||||
|
|
||||||
|
function Room:getSwitches()
|
||||||
|
return self.switches
|
||||||
|
end
|
||||||
|
|
||||||
function Room:switchCheck()
|
function Room:switchCheck()
|
||||||
local doorsToUnlock = {}
|
local doorsToUnlock = {}
|
||||||
for color, switchArray in pairs(self.switches) do
|
for color, switchArray in pairs(self.switches) do
|
||||||
|
|
@ -472,11 +488,47 @@ function Room:nextRoomCheck()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Room:save()
|
function Room:save()
|
||||||
self:serialize()
|
local name = self.name
|
||||||
|
local level = TSerial.pack(self:serialize(), nil, true)
|
||||||
|
print(level)
|
||||||
|
--write level to file
|
||||||
end
|
end
|
||||||
|
|
||||||
function Room:serialize()
|
function Room:serialize()
|
||||||
|
local level = {}
|
||||||
|
|
||||||
|
level.player = {}
|
||||||
|
for _, color in ipairs(self:getInactiveColors()) do
|
||||||
|
level.player[color] = self:getPlayerObject(color):getSerializationTable()
|
||||||
|
end
|
||||||
|
|
||||||
|
level.doors = {}
|
||||||
|
for _, door in pairs(self:getDoors()) do
|
||||||
|
table.insert(level.door, doors:getSerializationTable())
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
for color, collideableArray in pairs(self:getCollideables()) do
|
||||||
|
for _, collideable in pairs(collideableArray) do
|
||||||
|
if not collideable:isInstanceOf("Door") then --for now, we might change doors up some
|
||||||
|
local entityClass = string.lower(collideable:getClass())
|
||||||
|
print(entityClass)
|
||||||
|
if not level[entityClass] then level[entityClass] = {} end
|
||||||
|
if not level[entityClass][color] then level[entityClass][color] = {} end
|
||||||
|
table.insert(level[entityClass][color], collideable:getSerializationTable())
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
level.switches = {}
|
||||||
|
for color, switchArray in pairs(self:getSwitches()) do
|
||||||
|
level.switches[color] = {}
|
||||||
|
for _, switch in ipairs(switchArray) do
|
||||||
|
table.insert(level.switches[color], switch:getSerializationTable())
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return level
|
||||||
end
|
end
|
||||||
|
|
||||||
function nextRoom(pos)
|
function nextRoom(pos)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue