From 2b323fff8ff64440828e25d244ba483aee837e12 Mon Sep 17 00:00:00 2001 From: lustlion Date: Wed, 19 Jan 2022 16:13:13 +0100 Subject: [PATCH] Sliced game loop into game, gameworld and editor. added editor keybind for palette. --- data/scripts.lua | 2 + data/scripts/editor.lua | 81 +++++++++++++++++++++ data/scripts/game.lua | 142 ++----------------------------------- data/scripts/gameworld.lua | 42 +++++++++++ data/scripts/keybind.lua | 3 + main.lua | 8 +-- 6 files changed, 136 insertions(+), 142 deletions(-) create mode 100644 data/scripts/editor.lua create mode 100644 data/scripts/gameworld.lua diff --git a/data/scripts.lua b/data/scripts.lua index 61fc55d..60c1e14 100644 --- a/data/scripts.lua +++ b/data/scripts.lua @@ -20,3 +20,5 @@ require "data/scripts/keybind" require "data/scripts/pause" -- game loop require "data/scripts/game" +require "data/scripts/gameworld" +require "data/scripts/editor" diff --git a/data/scripts/editor.lua b/data/scripts/editor.lua new file mode 100644 index 0000000..b2b3915 --- /dev/null +++ b/data/scripts/editor.lua @@ -0,0 +1,81 @@ +function EditorStep() + palette = palette or false + AnimateTiles() + if Keybind:HasPressed(Keybind.editor.palette) then + if palette then + palette = false + else + palette = true + end + end +end + +function EditorDraw() + GameworldDraw() + if palette then + EditorDrawPalette() + end +end + +function EditorDrawPalette() + local width = LevelData.tileset:getPixelWidth()/tileProperties.width + local height = LevelData.tileset:getPixelHeight()/tileProperties.height + + local position_x = 1 + local position_y = 1 + for i = 1, #TileIndex-width-1 do + + love.graphics.draw( + LevelData.tileset, + TileIndex[i], + position_x * tileProperties.width*2, + (--[[p_scroll +]]position_y) * tileProperties.height*2, + 0, + 2, + 2 + ) + position_x = position_x + 1 + + if position_x > width then + position_x = position_x - width + position_y = position_y + 1 + end + end + + love.graphics.rectangle( + "line", + tileProperties.width*2, + (--[[p_scroll +]] 1) * tileProperties.height*2, + 2*LevelData.tileset:getPixelWidth(), + 2*LevelData.tileset:getPixelHeight() + ) +end +--[[ +function DrawSelectingPaletteTile() + if selecting_tile ~= nil then + local width = TileProperties.tileset:getPixelWidth()/TileProperties.width + local height = TileProperties.tileset:getPixelHeight()/TileProperties.height + + local positionx = 1 + local positiony = 1 + + for i = 1, #Tile-12 do + if i == selecting_tile then + love.graphics.rectangle( + "line", + positionx * TileProperties.width*2, + (p_scroll + positiony) * TileProperties.height*2, + TileProperties.width*2-1, + TileProperties.height*2-1 + ) + end + + positionx = positionx + 1 + + if positionx > width then + positionx = positionx - width + positiony = positiony + 1 + end + end + end +end]] diff --git a/data/scripts/game.lua b/data/scripts/game.lua index e811436..45002aa 100644 --- a/data/scripts/game.lua +++ b/data/scripts/game.lua @@ -1,15 +1,4 @@ -function stepEditor() - palette = palette or false - AnimateTiles() - if love.keyboard.isDown("tab") and not pressed then - if palette then palette = false else palette = true end - local pressed = true - else - pressed = false - end -end - -function stepGame() +function GameStep() -- GAME STEP if not do_pause then SetCollisionFlags(main_Player) @@ -23,27 +12,10 @@ function stepGame() end end -function drawEditor() - if game.width ~= love.graphics.getWidth() or game.height ~= love.graphics.getHeight() then - Camera.height = game.height - Camera.width = game.width - Canvas.Darkness:release() - Canvas.Darkness = CreateDarkness() - love.graphics.setCanvas(Canvas.Darkness) - SetDarkness() - love.graphics.setCanvas() - end +function GameDraw() - gameworldDraw() - if palette then - paletteDraw() - end -end - -function drawGame() - - gameworldDraw() - gameworldLighting() + GameworldDraw() + GameworldLighting() -- HUD textScale = 0.5 @@ -58,109 +30,3 @@ function drawGame() -- reset color if do_pause then PauseUI() end end - -function gameworldDraw() - -- resize proof - if game_resize then - Camera.height = game.height - Camera.width = game.width - end - - local pcr, pcg, pcb, pca = love.graphics.getColor() - - love.graphics.scale(game.scale,game.scale) - love.graphics.setColor(1,1,1,1) - LevelDisplayBackground() - - for _, enty in pairs(LoadedEntities) do - enty:HandleAnimation() - end - LevelDisplayForeground() - - love.graphics.setColor(pcr, pcg, pcb, pca) -end - -function gameworldLighting() - if game_resize then - Canvas.Darkness:release() - Canvas.Darkness = CreateDarkness() - love.graphics.setCanvas(Canvas.Darkness) - SetDarkness() - love.graphics.setCanvas() - end - - - -- work on lighting canvas - love.graphics.setCanvas(Canvas.Darkness) - SetDarkness() - DoLights() - DoBorder() - -- apply to game canvas - love.graphics.setColor(1,1,1,1) - love.graphics.setCanvas() - love.graphics.scale(1,1) - DrawDarkness() -end - -function paletteDraw() - local width = LevelData.tileset:getPixelWidth()/tileProperties.width - local height = LevelData.tileset:getPixelHeight()/tileProperties.height - - local position_x = 1 - local position_y = 1 - for i = 1, #TileIndex-width-1 do - - love.graphics.draw( - LevelData.tileset, - TileIndex[i], - position_x * tileProperties.width*2, - (--[[p_scroll +]]position_y) * tileProperties.height*2, - 0, - 2, - 2 - ) - position_x = position_x + 1 - - if position_x > width then - position_x = position_x - width - position_y = position_y + 1 - end - end - - love.graphics.rectangle( - "line", - tileProperties.width*2, - (--[[p_scroll +]] 1) * tileProperties.height*2, - 2*LevelData.tileset:getPixelWidth(), - 2*LevelData.tileset:getPixelHeight() - ) -end ---[[ -function DrawSelectingPaletteTile() - if selecting_tile ~= nil then - local width = TileProperties.tileset:getPixelWidth()/TileProperties.width - local height = TileProperties.tileset:getPixelHeight()/TileProperties.height - - local positionx = 1 - local positiony = 1 - - for i = 1, #Tile-12 do - if i == selecting_tile then - love.graphics.rectangle( - "line", - positionx * TileProperties.width*2, - (p_scroll + positiony) * TileProperties.height*2, - TileProperties.width*2-1, - TileProperties.height*2-1 - ) - end - - positionx = positionx + 1 - - if positionx > width then - positionx = positionx - width - positiony = positiony + 1 - end - end - end -end]] diff --git a/data/scripts/gameworld.lua b/data/scripts/gameworld.lua new file mode 100644 index 0000000..a45a677 --- /dev/null +++ b/data/scripts/gameworld.lua @@ -0,0 +1,42 @@ +function GameworldDraw() + -- resize proof + if game_resize then + Camera.height = game.height + Camera.width = game.width + end + + local pcr, pcg, pcb, pca = love.graphics.getColor() + + love.graphics.scale(game.scale,game.scale) + love.graphics.setColor(1,1,1,1) + LevelDisplayBackground() + + for _, enty in pairs(LoadedEntities) do + enty:HandleAnimation() + end + LevelDisplayForeground() + + love.graphics.setColor(pcr, pcg, pcb, pca) +end + +function GameworldLighting() + if game_resize then + Canvas.Darkness:release() + Canvas.Darkness = CreateDarkness() + love.graphics.setCanvas(Canvas.Darkness) + SetDarkness() + love.graphics.setCanvas() + end + + + -- work on lighting canvas + love.graphics.setCanvas(Canvas.Darkness) + SetDarkness() + DoLights() + DoBorder() + -- apply to game canvas + love.graphics.setColor(1,1,1,1) + love.graphics.setCanvas() + love.graphics.scale(1,1) + DrawDarkness() +end diff --git a/data/scripts/keybind.lua b/data/scripts/keybind.lua index 35a5e05..638064e 100644 --- a/data/scripts/keybind.lua +++ b/data/scripts/keybind.lua @@ -2,6 +2,7 @@ Keybind = {} Keybind.move = {} Keybind.menu = {} Keybind.debug = {} +Keybind.editor = {} function Keybind:CheckDown(action) for _, keyname in pairs(action.keys) do @@ -63,6 +64,8 @@ function Keybind:Default() Keybind.debug.reposition = { keys = {"f2"}} Keybind.debug.reload = { keys = {"f3"}} Keybind.debug.editor = { keys = {"f4"}} + -- Editor + Keybind.editor.palette = { keys = {"tab"}} end -- Set default values at start diff --git a/main.lua b/main.lua index 55d7814..37fa3c2 100644 --- a/main.lua +++ b/main.lua @@ -106,9 +106,9 @@ function love.update(dt) --editor if editor_mode then - stepEditor() + EditorStep() else - stepGame() + GameStep() end end @@ -134,9 +134,9 @@ function love.draw() end if editor_mode then - drawEditor() + EditorDraw() else - drawGame() + GameDraw() end love.graphics.print(arrow,10,40) end