This commit is contained in:
Your Name 2026-08-07 03:22:28 -04:00
parent f8e3d78332
commit b9b0726506
6 changed files with 140 additions and 81 deletions

View file

@ -36,11 +36,6 @@ function Room:initialize(t)
end
self:setActiveColor(color, true)
end
elseif not next(self.player) then
-- fresh load of a room with no saved player: default red spawn so it's
-- playable (the player starts with only red)
self:registerPlayer(Player:new(1, 1, "red"), "red")
self:setActiveColor("red", true)
end
self.name = t.name
@ -702,13 +697,37 @@ end
-- This is intentionally separate from Room:save. It is the editor's explicit
-- "write this level into the project" action; gameplay autosaves must never
-- call it.
function Room:saveToSource(name)
function Room:saveToSource(name, savePlayerPositions, playerSourceName)
name = name or self.name or "test"
local room = TSerial.pack(self:serialize(), nil, true)
local saveTable = self:serialize(savePlayerPositions ~= false)
if savePlayerPositions == false then
-- A normal editor save keeps the authored spawn positions from the
-- existing source level. During a rename that source is still oldName.
local sourceName = playerSourceName or name
local existing = readFromSource("rooms/" .. sourceName .. ".sav")
local keptPlayer = false
if existing then
local previous = TSerial.unpack(existing)
for _, object in ipairs(previous.objects or {}) do
if object.class == "player" then
table.insert(saveTable.objects, object)
keptPlayer = true
end
end
end
-- A newly named blank level has no prior source spawn to retain.
if not keptPlayer then
for color, player in pairs(self.player) do
local pos = player:getGridPos()
table.insert(saveTable.objects, { class = "player", color = color, x = pos.x, y = pos.y })
end
end
end
local room = TSerial.pack(saveTable, nil, true)
return writeToSource("rooms/" .. name .. ".sav", room)
end
function Room:serialize()
function Room:serialize(includePlayers)
local objects = {}
local function emit(class, color, entity)
@ -716,8 +735,10 @@ function Room:serialize()
table.insert(objects, { class = class, color = color, x = pos.x, y = pos.y })
end
for color, player in pairs(self.player) do
emit("player", color, player)
if includePlayers ~= false then
for color, player in pairs(self.player) do
emit("player", color, player)
end
end
for color, collideableArray in pairs(self.collideables) do
@ -769,6 +790,7 @@ function nextRoom(pos)
x = newPos.x, y = newPos.y, activeColors = currentRoom:getActiveColors()
}
}
if gameWorld then gameWorld:setLastRoom(currentRoom.name) end
if currentRoom.name then setEditorRoomName(currentRoom.name) end
end