Files
LoveDOS-Dungeon-Crawler/main.lua

121 lines
3.5 KiB
Lua

------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------
local love = require 'love'
local game_states = require 'src.states'
------------------------------------------------------------------------------
-- Variables
------------------------------------------------------------------------------
Current_state = 1
Fade_in = 0.0
Fade_out = 1.0
Change_state = false
------------------------------------------------------------------------------
-- Game methods
------------------------------------------------------------------------------
local function lerp(a, b, t)
-- Linear interpolation between two values a and b, by a t between 0.0 and 1.0
return (a * t) + (b * (1.0 - t))
end
------------------------------------------------------------------------------
-- Game methods
------------------------------------------------------------------------------
function love.load()
game_states[Current_state]:load()
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_out >= 1.0 then
Fade_out = 0.0
Change_state = true
end
-- If there is a fade in going then update it.
if Fade_in < 1.0 then
Fade_in = Fade_in + (dt / 2)
end
-- If there is a fade out going then update it.
if Fade_out < 1.0 then
Fade_out = Fade_out + (dt / 2)
end
-- 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_out >= 1.0 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_out >= 1.0 then
game_states[Current_state]:unload(dt)
Current_state = new_state
game_states[Current_state]:load()
Change_state = false
end
end
end
function love.draw()
-- Reset the drawing color and clear the screen.
love.graphics.clear()
-- Render the current game state.
game_states[Current_state]:draw()
-- If there is a fade in going then render it.
if Fade_in < 1.0 then
love.graphics.setColor(0, 0, 0)
love.graphics.circle('fill', -320, love.graphics.getHeight() / 2, lerp(0, 400 * 2, Fade_in))
love.graphics.setColor()
end
-- If there is a fade out going then render it.
if Fade_out < 1.0 then
love.graphics.setColor(0, 0, 0)
love.graphics.circle('fill', -320, love.graphics.getHeight() / 2, lerp(400 * 2, 0, Fade_out))
love.graphics.setColor()
end
end
function love.keypressed(key)
-- Send events to the active game state if there is no fade active.
if Fade_in >= 1.0 and Fade_out >= 1.0 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_in >= 1.0 and Fade_out >= 1.0 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_in >= 1.0 and Fade_out >= 1.0 then
game_states[Current_state]:mousemoved(x, y, btn)
end
end