feat: add v1->v2 schema auto-upgrade for map tables
Converts legacy single-tilemap maps (tiles[], tile_rotations[]) into the schema-v2 format (atlases[], layers.surface with packed GID cells). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
35
init.lua
35
init.lua
@@ -43,6 +43,40 @@ local function decode_gid(gid)
|
|||||||
return atlas_index, tile_id, rotation
|
return atlas_index, tile_id, rotation
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- =====================================================================
|
||||||
|
-- Schema v1→v2 Auto-Upgrade (transparent beim Load)
|
||||||
|
-- v1: { id, tilemap, size, tiles[], tile_rotations? }
|
||||||
|
-- v2: { schema_version, id, size, atlases[], layers: { surface: { tiles[] } }, roof? }
|
||||||
|
-- =====================================================================
|
||||||
|
|
||||||
|
local function upgrade_v1_to_v2(v1)
|
||||||
|
local atlas_id = v1.tilemap or "default_tilemap"
|
||||||
|
local v1_tiles = v1.tiles or {}
|
||||||
|
local v1_rots = v1.tile_rotations or {}
|
||||||
|
|
||||||
|
local gids = {}
|
||||||
|
for i = 1, #v1_tiles do
|
||||||
|
local tile_id = v1_tiles[i]
|
||||||
|
local rot_deg = v1_rots[i] or 0
|
||||||
|
local rot_quad = math.floor(rot_deg / 90) % 4
|
||||||
|
if tile_id == 0 then
|
||||||
|
gids[i] = 0
|
||||||
|
else
|
||||||
|
gids[i] = encode_gid(0, tile_id, rot_quad)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
schema_version = 2,
|
||||||
|
id = v1.id,
|
||||||
|
size = v1.size,
|
||||||
|
atlases = { atlas_id },
|
||||||
|
layers = {
|
||||||
|
surface = { tiles = gids }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
-- =====================================================================
|
-- =====================================================================
|
||||||
-- Internal helpers
|
-- Internal helpers
|
||||||
-- =====================================================================
|
-- =====================================================================
|
||||||
@@ -354,5 +388,6 @@ end
|
|||||||
|
|
||||||
M.encode_gid = encode_gid
|
M.encode_gid = encode_gid
|
||||||
M.decode_gid = decode_gid
|
M.decode_gid = decode_gid
|
||||||
|
M.upgrade_v1_to_v2 = upgrade_v1_to_v2
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user