Can expand and reduce canvass with lctrl and lshift. (Bug: all tiles get erased :c)

This commit is contained in:
lustlion
2022-01-31 01:22:15 +01:00
parent 727592ac55
commit e1bfd4f7f5
3 changed files with 106 additions and 18 deletions

View File

@@ -19,6 +19,86 @@ function LevelLoadTiles()
TileCreateObjects()
end
function LevelExpandCanvas(horizontal,vertical)
local h = LevelGetTileWidth()
local v = LevelGetTileHeight()
-- get new canvas size
local newCanvasH = h + math.abs(horizontal)
local newCanvasV = v + math.abs(vertical)
-- lets make a new temporal canvas
local ExpandedLevel = {}
for i = 1, newCanvasV do
ExpandedLevel[i] = {}
for j = 1, newCanvasH do
ExpandedLevel[i][j] = InstanceTile(0)
end
end
-- lets guess how the new canvas and positions are offset
local expand_h = 0
if horizontal < 0 then
expand_h = -horizontal
end
local expand_v = 0
if vertical < 0 then
expand_v = -vertical
end
-- get data from old canvas to new canvas
for i = 1, #LevelTiles do
for j = 1, #LevelTiles[i] do
ExpandedLevel[i+expand_v][j+expand_h] = InstanceTile(LevelTiles[i][j])
end
end
-- use new canvas
LevelTiles = ExpandedLevel
end
function LevelReduceCanvas(horizontal,vertical)
local h = LevelGetTileWidth()
local v = LevelGetTileHeight()
-- get new canvas size
local newCanvasH = h - math.abs(horizontal)
local newCanvasV = v - math.abs(vertical)
-- lets make a new temporal canvas
local ExpandedLevel = {}
for i = 1, newCanvasV do
ExpandedLevel[i] = {}
for j = 1, newCanvasH do
ExpandedLevel[i][j] = InstanceTile(0)
end
end
-- lets guess how the new canvas and positions are offset
local expand_h = 0
if horizontal < 0 then
expand_h = -horizontal
end
local expand_v = 0
if vertical < 0 then
expand_v = -vertical
end
-- get data from old canvas to new canvas
for i = 1, #ExpandedLevel do
for j = 1, #ExpandedLevel[i] do
ExpandedLevel[i][j] = InstanceTile(LevelTiles[i+expand_v][j+expand_h])
end
end
-- use new canvas
LevelTiles = ExpandedLevel
end
function LevelGetTileData()
for k, v in pairs(tileset) do
if v == LevelData.tileset then
@@ -79,7 +159,7 @@ function LevelIndexTiles()
-- instance level tiles according to the Properties
for i = 1, #LevelTiles do
for j = 1, #LevelTiles[i] do
InstanceTile(i,j,LevelTiles[i][j])
SetTile(i,j,LevelTiles[i][j])
end
end
end
@@ -117,9 +197,8 @@ function TileDataInitialize()
end
end
function InstanceTile(i,j,id)
LevelTiles[i][j] = {}
local tile = LevelTiles[i][j]
function InstanceTile(id)
local tile = {}
tile.id = id
local Properties = TileData[tile.id]
@@ -137,6 +216,12 @@ function InstanceTile(i,j,id)
tile.display = Properties.force
end
end
return tile
end
function SetTile(i,j,id)
LevelTiles[i][j] = InstanceTile(id)
end
function TileGetType(tile)