From e1bfd4f7f5518910d23bfec280d7efd1b84e3363 Mon Sep 17 00:00:00 2001 From: lustlion Date: Mon, 31 Jan 2022 01:22:15 +0100 Subject: [PATCH] Can expand and reduce canvass with lctrl and lshift. (Bug: all tiles get erased :c) --- data/scripts/editor.lua | 29 +++++++------ data/scripts/keybind.lua | 2 + data/scripts/level.lua | 93 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 106 insertions(+), 18 deletions(-) diff --git a/data/scripts/editor.lua b/data/scripts/editor.lua index c9c5c54..07f997c 100644 --- a/data/scripts/editor.lua +++ b/data/scripts/editor.lua @@ -115,34 +115,35 @@ function EditorDoEdit() and LevelTiles[vertical][horizontal] ~= nil and love.keyboard.isDown("lshift") ~= true and love.keyboard.isDown("lctrl") ~= true - and selecting_tile ~= nil then - if Keybind:CheckDown(Keybind.generic.lclick) then - InstanceTile(vertical,horizontal,selecting_tile) + if Keybind:CheckDown(Keybind.generic.lclick) + and selecting_tile ~= nil + then + SetTile(vertical,horizontal,selecting_tile) elseif Keybind:CheckDown(Keybind.generic.rclick) then - InstanceTile(vertical,horizontal,0) + SetTile(vertical,horizontal,0) end LevelReloadTiles() - --[[ - elseif love.keyboard.isDown("lshift") then - --ExpandCanvas(math.sign(expand_h),math.sign(expand_v)) + + elseif Keybind:HasPressed(Keybind.generic.lshift) then + LevelExpandCanvas(math.sign(expand_h),math.sign(expand_v)) if expand_h < 0 then - Camera.pos.x = Camera.pos.x - expand_h*tileProperties.scale*tileProperties.width + Camera.pos.x = Camera.pos.x - math.sign(expand_h)*tileProperties.scale*tileProperties.width end if expand_v < 0 then - Camera.pos.y = Camera.pos.y - expand_v*tileProperties.scale*tileProperties.height + Camera.pos.y = Camera.pos.y - math.sign(expand_v)*tileProperties.scale*tileProperties.height end - elseif love.keyboard.isDown("lctrl") then - --ReduceCanvas(math.sign(expand_h),math.sign(expand_v)) + elseif Keybind:HasPressed(Keybind.generic.lctrl) then + LevelReduceCanvas(math.sign(expand_h),math.sign(expand_v)) if expand_h < 0 then - Camera.pos.x = Camera.pos.x - expand_h*tileProperties.scale*tileProperties.width + Camera.pos.x = Camera.pos.x - math.sign(expand_h)*tileProperties.scale*tileProperties.width end if expand_v < 0 then - Camera.pos.y = Camera.pos.y - expand_v*tileProperties.scale*tileProperties.height - end]] + Camera.pos.y = Camera.pos.y - math.sign(expand_v)*tileProperties.scale*tileProperties.height + end end end end diff --git a/data/scripts/keybind.lua b/data/scripts/keybind.lua index e069262..b47a07c 100644 --- a/data/scripts/keybind.lua +++ b/data/scripts/keybind.lua @@ -71,6 +71,8 @@ function Keybind:Default() -- Generic Keybind.generic.lclick = { keys = {1}} Keybind.generic.rclick = { keys = {2}} + Keybind.generic.lshift = { keys = {"lshift"}} + Keybind.generic.lctrl = { keys = {"lctrl"}} end -- Set default values at start diff --git a/data/scripts/level.lua b/data/scripts/level.lua index b193a95..c3f75fc 100644 --- a/data/scripts/level.lua +++ b/data/scripts/level.lua @@ -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)