Files
LoveDOS-Dungeon-Crawler/main.lua

89 lines
2.3 KiB
Lua
Raw Normal View History

------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------
2025-09-27 18:38:20 -04:00
local love = require 'love'
local game_states = require 'src.states'
2025-09-28 12:54:17 -04:00
local Fader = require 'src.fader'
------------------------------------------------------------------------------
-- Variables
------------------------------------------------------------------------------
Current_state = 1
2025-09-28 12:54:17 -04:00
Fade = Fader()
Change_state = false
------------------------------------------------------------------------------
-- Game methods
------------------------------------------------------------------------------
2025-09-27 18:38:20 -04:00
function love.load()
game_states[Current_state]:load()
2025-09-28 12:54:17 -04:00
Fade:fade_in()
end
function love.update(dt)
local new_state = game_states[Current_state]:update(dt)
2025-09-27 18:38:20 -04:00
-- If the game state changed then trigger a fade out.
2025-09-28 12:54:17 -04:00
if new_state ~= Current_state and Fade.done then
Fade:fade_out()
Change_state = true
end
2025-09-27 18:38:20 -04:00
2025-09-28 12:54:17 -04:00
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.
2025-09-28 12:54:17 -04:00
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.
2025-09-28 12:54:17 -04:00
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
2025-09-28 12:54:17 -04:00
Fade:fade_in()
end
end
2025-09-27 18:38:20 -04:00
end
function love.draw()
love.graphics.clear()
game_states[Current_state]:draw()
2025-09-28 12:54:17 -04:00
Fade:draw()
2025-09-27 18:38:20 -04:00
end
function love.keypressed(key)
-- Send events to the active game state if there is no fade active.
2025-09-28 12:54:17 -04:00
if Fade.done then
game_states[Current_state]:keypressed(key)
end
2025-09-27 18:38:20 -04:00
end
function love.mousemoved(x, y)
-- Send events to the active game state if there is no fade active.
2025-09-28 12:54:17 -04:00
if Fade.done then
game_states[Current_state]:mousemoved(x, y)
end
2025-09-27 18:38:20 -04:00
end
function love.mousepressed(x, y, btn)
-- Send events to the active game state if there is no fade active.
2025-09-28 12:54:17 -04:00
if Fade.done then
game_states[Current_state]:mousemoved(x, y, btn)
end
2025-09-27 18:38:20 -04:00
end