Files
LoveDOS-Dungeon-Crawler/main.lua

89 lines
2.3 KiB
Lua

------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------
local love = require 'love'
local game_states = require 'src.states'
local Fader = require 'src.fader'
------------------------------------------------------------------------------
-- Variables
------------------------------------------------------------------------------
Current_state = 1
Fade = Fader()
Change_state = false
------------------------------------------------------------------------------
-- Game methods
------------------------------------------------------------------------------
function love.load()
game_states[Current_state]:load()
Fade:fade_in()
end
function love.update(dt)
local new_state = game_states[Current_state]:update(dt)
-- If the game state changed then trigger a fade out.
if new_state ~= Current_state and Fade.done then
Fade:fade_out()
Change_state = true
end
Fade:update(dt)
-- Update the game state.
if game_states[new_state] == nil then
-- If the new state doesn't exist then wait for the fade out to end and quit.
if Fade.done then
game_states[Current_state]:unload(dt)
love.event.quit()
end
else
-- If the new state exists then unload it's data and set the new state.
if Change_state and Fade.done then
game_states[Current_state]:unload(dt)
Current_state = new_state
game_states[Current_state]:load()
Change_state = false
Fade:fade_in()
end
end
end
function love.draw()
love.graphics.clear()
game_states[Current_state]:draw()
Fade:draw()
end
function love.keypressed(key)
-- Send events to the active game state if there is no fade active.
if Fade.done then
game_states[Current_state]:keypressed(key)
end
end
function love.mousemoved(x, y)
-- Send events to the active game state if there is no fade active.
if Fade.done then
game_states[Current_state]:mousemoved(x, y)
end
end
function love.mousepressed(x, y, btn)
-- Send events to the active game state if there is no fade active.
if Fade.done then
game_states[Current_state]:mousemoved(x, y, btn)
end
end