2025-09-28 02:14:41 -04:00
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
-- Imports
|
|
|
|
|
------------------------------------------------------------------------------
|
2025-09-27 18:38:20 -04:00
|
|
|
|
2025-09-28 02:14:41 -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'
|
2025-09-28 02:14:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
-- Variables
|
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
Current_state = 1
|
2025-09-28 12:54:17 -04:00
|
|
|
Fade = Fader()
|
2025-09-28 02:14:41 -04:00
|
|
|
Change_state = false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
-- Game methods
|
|
|
|
|
------------------------------------------------------------------------------
|
2025-09-27 18:38:20 -04:00
|
|
|
|
|
|
|
|
function love.load()
|
2025-09-28 02:14:41 -04:00
|
|
|
game_states[Current_state]:load()
|
2025-09-28 12:54:17 -04:00
|
|
|
Fade:fade_in()
|
2025-09-28 02:14:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function love.update(dt)
|
|
|
|
|
local new_state = game_states[Current_state]:update(dt)
|
2025-09-27 18:38:20 -04:00
|
|
|
|
2025-09-28 02:14:41 -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()
|
2025-09-28 02:14:41 -04:00
|
|
|
Change_state = true
|
|
|
|
|
end
|
2025-09-27 18:38:20 -04:00
|
|
|
|
2025-09-28 12:54:17 -04:00
|
|
|
Fade:update(dt)
|
2025-09-28 02:14:41 -04:00
|
|
|
|
|
|
|
|
-- 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
|
2025-09-28 02:14:41 -04:00
|
|
|
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
|
2025-09-28 02:14:41 -04:00
|
|
|
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()
|
2025-09-28 02:14:41 -04:00
|
|
|
end
|
|
|
|
|
end
|
2025-09-27 18:38:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function love.draw()
|
2025-09-28 02:14:41 -04:00
|
|
|
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)
|
2025-09-28 02:14:41 -04:00
|
|
|
-- 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
|
2025-09-28 02:14:41 -04:00
|
|
|
game_states[Current_state]:keypressed(key)
|
|
|
|
|
end
|
2025-09-27 18:38:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function love.mousemoved(x, y)
|
2025-09-28 02:14:41 -04:00
|
|
|
-- 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
|
2025-09-28 02:14:41 -04:00
|
|
|
game_states[Current_state]:mousemoved(x, y)
|
|
|
|
|
end
|
2025-09-27 18:38:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
2025-09-28 02:14:41 -04:00
|
|
|
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
|
2025-09-28 02:14:41 -04:00
|
|
|
game_states[Current_state]:mousemoved(x, y, btn)
|
|
|
|
|
end
|
2025-09-27 18:38:20 -04:00
|
|
|
end
|