Sliced game loop into game, gameworld and editor. added editor keybind for palette.

This commit is contained in:
lustlion 2022-01-19 16:13:13 +01:00
parent 0706d76b57
commit 2b323fff8f
6 changed files with 136 additions and 142 deletions

View File

@ -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"

81
data/scripts/editor.lua Normal file
View File

@ -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]]

View File

@ -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]]

View File

@ -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

View File

@ -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

View File

@ -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