basic serialization worx*
*
This commit is contained in:
parent
d76ace8f69
commit
973a8ab25b
41 changed files with 162 additions and 82 deletions
10
TODO
10
TODO
|
|
@ -10,4 +10,12 @@ YES -should puzzles be sort of secondary to exploration? li_e should most of
|
||||||
|
|
||||||
all of the npcs should be stuc_ in a cycle of whatever they're doing, focused on it, hav always been focused on it, maybe it has to do w/ the fact that they are always doing 'night activities'
|
all of the npcs should be stuc_ in a cycle of whatever they're doing, focused on it, hav always been focused on it, maybe it has to do w/ the fact that they are always doing 'night activities'
|
||||||
|
|
||||||
ma_e sprites static class vars instead of globals
|
ma_e sprites static class vars instead of globals (?)
|
||||||
|
|
||||||
|
so instead of having the exits be a part of the room save, they should be a separate save, e.g., exits.sav w/ a table li_e this:
|
||||||
|
{
|
||||||
|
room_name = {top = room_name2, bottom = room_name3}
|
||||||
|
}
|
||||||
|
why?? b/c this allows us to visually arrange levels and not have to open every adjacent file (as much as 4!!) and edit them
|
||||||
|
|
||||||
|
go for the feeling of the beginning of eternal sunshine w/ joel's narration, maybe a sense of desperation (does this require urgency?? should the game be time-limited li_e majora's mas_? lol)
|
||||||
|
|
@ -8,6 +8,7 @@ function Ass:initialize(x, y, color, folder, name)
|
||||||
x = x,
|
x = x,
|
||||||
y = y,
|
y = y,
|
||||||
color = color,
|
color = color,
|
||||||
|
name = name,
|
||||||
cellShape = spriteCells["tree"][color],
|
cellShape = spriteCells["tree"][color],
|
||||||
collision = "unmoveable", --player can't push other player
|
collision = "unmoveable", --player can't push other player
|
||||||
sprite = spr
|
sprite = spr
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ Color.static.list = {
|
||||||
}
|
}
|
||||||
|
|
||||||
function Color:initialize(inColor)
|
function Color:initialize(inColor)
|
||||||
|
self.name = inColor
|
||||||
local c = Color.list[inColor]
|
local c = Color.list[inColor]
|
||||||
self.r, self.g, self.b, self.a = c.r, c.g, c.b, c.a
|
self.r, self.g, self.b, self.a = c.r, c.g, c.b, c.a
|
||||||
end
|
end
|
||||||
|
|
@ -19,6 +20,10 @@ function Color:set()
|
||||||
return self.r, self.g, self.b, self.a
|
return self.r, self.g, self.b, self.a
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Color:getname()
|
||||||
|
return self.name
|
||||||
|
end
|
||||||
|
|
||||||
function randomColor()
|
function randomColor()
|
||||||
local colors = {"red", "green", "blue"}
|
local colors = {"red", "green", "blue"}
|
||||||
return colors[math.random(1, 3)]
|
return colors[math.random(1, 3)]
|
||||||
|
|
|
||||||
5
door.lua
5
door.lua
|
|
@ -2,7 +2,7 @@ require "entity"
|
||||||
|
|
||||||
Door = class("Door", Entity)
|
Door = class("Door", Entity)
|
||||||
|
|
||||||
function Door:initialize(x, y, colors)
|
function Door:initialize(x, y)
|
||||||
Entity.initialize(self, {
|
Entity.initialize(self, {
|
||||||
x = x,
|
x = x,
|
||||||
y = y,
|
y = y,
|
||||||
|
|
@ -13,7 +13,7 @@ function Door:initialize(x, y, colors)
|
||||||
self.locked = true
|
self.locked = true
|
||||||
|
|
||||||
self.colors = {}
|
self.colors = {}
|
||||||
for _, color in pairs(colors) do
|
for _, color in pairs({"red", "green", "blue"}) do
|
||||||
self.colors[color] = true --these are currently loc_ed
|
self.colors[color] = true --these are currently loc_ed
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -41,6 +41,7 @@ end
|
||||||
|
|
||||||
function Door:setColors(colors)
|
function Door:setColors(colors)
|
||||||
local offColors = {}
|
local offColors = {}
|
||||||
|
|
||||||
for color, off in pairs(colors) do
|
for color, off in pairs(colors) do
|
||||||
if off then
|
if off then
|
||||||
if self.colors[color] then self.colors[color] = false end
|
if self.colors[color] then self.colors[color] = false end
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ function Entity:initialize(t)
|
||||||
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)
|
||||||
|
self.name = t.name
|
||||||
self.color = Color:new(t.color)
|
self.color = Color:new(t.color)
|
||||||
self.collision = t.collision --"none", "moveable", "unmoveable"
|
self.collision = t.collision --"none", "moveable", "unmoveable"
|
||||||
self.sprite = t.sprite
|
self.sprite = t.sprite
|
||||||
|
|
@ -123,9 +124,8 @@ end
|
||||||
function Entity:getSerializationTable()
|
function Entity:getSerializationTable()
|
||||||
local pos = self:getGridPos()
|
local pos = self:getGridPos()
|
||||||
local table = {}
|
local table = {}
|
||||||
table.x, table.y, table.color = pos.x, pos.y, color
|
table.x, table.y, table.color = pos.x, pos.y, self.color:getname()
|
||||||
if self.name then table.name = self.name end
|
if self.name then table.name = self.name end
|
||||||
|
|
||||||
return table
|
return table
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
1
main.lua
1
main.lua
|
|
@ -92,6 +92,7 @@ end
|
||||||
function _initDefaults()
|
function _initDefaults()
|
||||||
--set filter
|
--set filter
|
||||||
love.graphics.setDefaultFilter("linear", "nearest")
|
love.graphics.setDefaultFilter("linear", "nearest")
|
||||||
|
love.filesystem.createDirectory("rooms")
|
||||||
|
|
||||||
--set global width / height variables
|
--set global width / height variables
|
||||||
width = love.graphics.getWidth()
|
width = love.graphics.getWidth()
|
||||||
|
|
|
||||||
118
room.lua
118
room.lua
|
|
@ -12,15 +12,25 @@ require "switch"
|
||||||
require "door"
|
require "door"
|
||||||
|
|
||||||
function Room:initialize(t)
|
function Room:initialize(t)
|
||||||
self.colorsPlayerHas = {"red", "green", "blue"}
|
|
||||||
self.exits = t.exits
|
|
||||||
self.name = t.name
|
|
||||||
self.player = {}
|
self.player = {}
|
||||||
self.switches = { red = {}, green = {}, blue = {} }
|
self.switches = { red = {}, green = {}, blue = {} }
|
||||||
self.switchMatrices = {}
|
|
||||||
self.doors = {}
|
self.doors = {}
|
||||||
self.collideables = { red = {}, green = {}, blue = {} }
|
self.collideables = { red = {}, green = {}, blue = {} }
|
||||||
self.collidableMatrices = {} --{red = {{...}{...}{...}}, ...
|
self.activeColors = {red = false, green = false, blue = false}
|
||||||
|
self.colorsPlayerHas = {}
|
||||||
|
|
||||||
|
if t.name then
|
||||||
|
print("loaded!!")
|
||||||
|
self:load(t.name)
|
||||||
|
for _, color in ipairs(t.player.activeColors) do
|
||||||
|
self:registerPlayer(Player:new(t.player.x, t.player.y, color), color)
|
||||||
|
self:setActiveColor(color, true)
|
||||||
|
end
|
||||||
|
self.exits = t.exits
|
||||||
|
self.name = t.name
|
||||||
|
else
|
||||||
|
self.colorsPlayerHas = {"red", "green", "blue"}
|
||||||
|
|
||||||
|
|
||||||
for _, color in ipairs(t.player.activeColors) do
|
for _, color in ipairs(t.player.activeColors) do
|
||||||
self.player[color] = Player:new(t.player.x, t.player.y, color)
|
self.player[color] = Player:new(t.player.x, t.player.y, color)
|
||||||
|
|
@ -40,14 +50,14 @@ function Room:initialize(t)
|
||||||
-- table.insert(self.doors, door)
|
-- table.insert(self.doors, door)
|
||||||
|
|
||||||
for color, active in pairs(self.activeColors) do
|
for color, active in pairs(self.activeColors) do
|
||||||
self.collideables[color]["w: " .. x .. ", " .. y] = Wall:new(x, y, color)
|
table.insert(self.collideables[color], Wall:new(x, y, color))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for i=0, 5 do
|
for i=0, 5 do
|
||||||
local x, y = randomPosition()
|
local x, y = randomPosition()
|
||||||
for color, active in pairs(self.activeColors) do
|
for color, active in pairs(self.activeColors) do
|
||||||
self.collideables[color]["w: " .. x .. ", " .. y] = Box:new(x, y, color)
|
table.insert(self.collideables[color], Box:new(x, y, color))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -63,19 +73,19 @@ function Room:initialize(t)
|
||||||
-- if active then self.collideables[color]["n: " .. x .. ", " .. y] = Ass:new(x, y, color, "animals/processed/", "me_rach") end
|
-- if active then self.collideables[color]["n: " .. x .. ", " .. y] = Ass:new(x, y, color, "animals/processed/", "me_rach") end
|
||||||
|
|
||||||
local x, y = 1, 1
|
local x, y = 1, 1
|
||||||
if active then self.collideables[color]["n: " .. x .. ", " .. y] = Ass:new(x, y, color, "nature/", "palm_scaled_reduced") end
|
if active then table.insert(self.collideables[color], Ass:new(x, y, color, "nature/", "palm_scaled_reduced")) end
|
||||||
self.collideables[color]["n: " .. x .. ", " .. y]:getOccupiedCells()
|
|
||||||
|
|
||||||
-- local x, y = 7, 7
|
-- local x, y = 7, 7
|
||||||
-- if active then self.collideables[color]["n: " .. x .. ", " .. y] = Ass:new(x, y, color, "food/processed/", "beer_mug") end
|
-- 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)
|
-- self.collideables[color]["fpp"] = Artifact:new(6, 6, color)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
for color, entities in pairs(self.collideables) do
|
|
||||||
self.collidableMatrices[color] = self:createEntityMatrix(entities)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
self.switchMatrices = {}
|
||||||
|
self.collidableMatrices = {} --{red = {{...}{...}{...}}, ...
|
||||||
|
self:createSwitchMatrix()
|
||||||
|
self:createCollideablesMatrix()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -126,6 +136,7 @@ function Room:createEntityMatrix(entities)
|
||||||
table.insert(matrix, row)
|
table.insert(matrix, row)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--assumes that entities table is set up li_e {index = ent1, anotherIndex = ent2, ...}
|
--assumes that entities table is set up li_e {index = ent1, anotherIndex = ent2, ...}
|
||||||
for _, entity in pairs(entities) do
|
for _, entity in pairs(entities) do
|
||||||
for _, occupiedCells in pairs(entity:getOccupiedCells()) do
|
for _, occupiedCells in pairs(entity:getOccupiedCells()) do
|
||||||
|
|
@ -134,6 +145,7 @@ function Room:createEntityMatrix(entities)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- printMatrix(matrix)
|
-- printMatrix(matrix)
|
||||||
|
|
||||||
return matrix
|
return matrix
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -362,7 +374,6 @@ function Room:getPossibleColors()
|
||||||
|
|
||||||
for _, color in ipairs(self.colorsPlayerHas) do
|
for _, color in ipairs(self.colorsPlayerHas) do
|
||||||
table.insert(colors, color)
|
table.insert(colors, color)
|
||||||
print(color)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return colors
|
return colors
|
||||||
|
|
@ -387,7 +398,6 @@ function Room:tick()
|
||||||
self:createSwitchMatrix()
|
self:createSwitchMatrix()
|
||||||
local setDoorColors = self:switchCheck()
|
local setDoorColors = self:switchCheck()
|
||||||
for _, door in pairs(self.doors) do
|
for _, door in pairs(self.doors) do
|
||||||
print(door)
|
|
||||||
door:setColors(setDoorColors)
|
door:setColors(setDoorColors)
|
||||||
end
|
end
|
||||||
self:nextRoomCheck()
|
self:nextRoomCheck()
|
||||||
|
|
@ -487,11 +497,66 @@ function Room:nextRoomCheck()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Room:createEntity(class, color, x, y)
|
||||||
|
if class == "Door" then
|
||||||
|
self:registerDoor(Door:new(x, y))
|
||||||
|
elseif class == "Wall" then
|
||||||
|
self:registerCollidable(Wall:new(x, y, color), {color})
|
||||||
|
elseif class == "Switch" then
|
||||||
|
self.registerSwitch(Switch:new(x, y), color)
|
||||||
|
elseif class == "player" then
|
||||||
|
self:registerPlayer(Player:new(x, y, color), color)
|
||||||
|
elseif class == "Box" then
|
||||||
|
self:registerCollidable(Box:new(x, y, color), {color})
|
||||||
|
elseif class == "npc" then
|
||||||
|
-- do something soon
|
||||||
|
elseif class == "Ass" then
|
||||||
|
--do this later when we have a more definite schema
|
||||||
|
-- self:registerAssorted(Ass:new(x, y, color, name))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Room:registerCollidable(entityPointer, colorArray)
|
||||||
|
if type(colorArray) ~= "table" then colorArray = {colorArray} end
|
||||||
|
for _, color in ipairs(colorArray) do
|
||||||
|
table.insert(self.collideables[color], entityPointer)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Room:registerDoor(entityPointer)
|
||||||
|
table.insert(self.doors, entityPointer)
|
||||||
|
self:registerCollidable(entityPointer, {"red", "green", "blue"})
|
||||||
|
end
|
||||||
|
|
||||||
|
function Room:registerSwitch(entityPointer, color)
|
||||||
|
table.insert(self.switches[color], entityPointer)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Room:registerPlayer(entityPointer, color)
|
||||||
|
table.insert(self.colorsPlayerHas, color)
|
||||||
|
self.player[color] = entityPointer
|
||||||
|
end
|
||||||
|
|
||||||
|
function Room:load(name)
|
||||||
|
local room = love.filesystem.read("rooms/" .. name .. ".sav")
|
||||||
|
self:deserialize(TSerial.unpack(room))
|
||||||
|
end
|
||||||
|
|
||||||
|
function Room:deserialize(saveTable)
|
||||||
|
for entityClass, entities in pairs(saveTable) do
|
||||||
|
for _, entity in pairs(entities) do
|
||||||
|
self:createEntity(entityClass, entity.color, entity.x, entity.y)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function Room:save()
|
function Room:save()
|
||||||
local name = self.name
|
local name = self.name or "test"
|
||||||
local level = TSerial.pack(self:serialize(), nil, true)
|
local room = TSerial.pack(self:serialize(), nil, true)
|
||||||
print(level)
|
|
||||||
--write level to file
|
local success = love.filesystem.write("rooms/" .. name .. ".sav", room)
|
||||||
|
if not success then error "great job u fool" end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Room:serialize()
|
function Room:serialize()
|
||||||
|
|
@ -502,7 +567,7 @@ function Room:serialize()
|
||||||
level.player[color] = self:getPlayerObject(color):getSerializationTable()
|
level.player[color] = self:getPlayerObject(color):getSerializationTable()
|
||||||
end
|
end
|
||||||
|
|
||||||
level.doors = {}
|
level.door = {}
|
||||||
for _, door in pairs(self:getDoors()) do
|
for _, door in pairs(self:getDoors()) do
|
||||||
table.insert(level.door, doors:getSerializationTable())
|
table.insert(level.door, doors:getSerializationTable())
|
||||||
end
|
end
|
||||||
|
|
@ -511,20 +576,17 @@ function Room:serialize()
|
||||||
for color, collideableArray in pairs(self:getCollideables()) do
|
for color, collideableArray in pairs(self:getCollideables()) do
|
||||||
for _, collideable in pairs(collideableArray) do
|
for _, collideable in pairs(collideableArray) do
|
||||||
if not collideable:isInstanceOf("Door") then --for now, we might change doors up some
|
if not collideable:isInstanceOf("Door") then --for now, we might change doors up some
|
||||||
local entityClass = string.lower(collideable:getClass())
|
local entityClass = collideable:getClass()
|
||||||
print(entityClass)
|
|
||||||
if not level[entityClass] then level[entityClass] = {} end
|
if not level[entityClass] then level[entityClass] = {} end
|
||||||
if not level[entityClass][color] then level[entityClass][color] = {} end
|
table.insert(level[entityClass], collideable:getSerializationTable())
|
||||||
table.insert(level[entityClass][color], collideable:getSerializationTable())
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
level.switches = {}
|
level.switch = {}
|
||||||
for color, switchArray in pairs(self:getSwitches()) do
|
for color, switchArray in pairs(self:getSwitches()) do
|
||||||
level.switches[color] = {}
|
|
||||||
for _, switch in ipairs(switchArray) do
|
for _, switch in ipairs(switchArray) do
|
||||||
table.insert(level.switches[color], switch:getSerializationTable())
|
table.insert(level.switch, switch:getSerializationTable())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -551,9 +613,11 @@ function nextRoom(pos)
|
||||||
currentRoom:save()
|
currentRoom:save()
|
||||||
|
|
||||||
currentRoom = Room:new{
|
currentRoom = Room:new{
|
||||||
|
name = "test",
|
||||||
player = {
|
player = {
|
||||||
x = newPos.x, y = newPos.y, activeColors = currentRoom:getActiveColors()
|
x = newPos.x, y = newPos.y, activeColors = currentRoom:getActiveColors()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
4
wall.lua
4
wall.lua
|
|
@ -9,9 +9,9 @@ function Wall:initialize(x, y, color)
|
||||||
sizeX = 1,
|
sizeX = 1,
|
||||||
sizeY = 1,
|
sizeY = 1,
|
||||||
color = color,
|
color = color,
|
||||||
cellShape = spriteCells.test_object,
|
cellShape = spriteCells.wall,
|
||||||
collision = "unmoveable",
|
collision = "unmoveable",
|
||||||
sprite = sprites.test_object
|
sprite = sprites.wall
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue