Added Fader class.

This commit is contained in:
2025-09-28 12:54:17 -04:00
parent 56366e4db9
commit 0023c0a0a2
2 changed files with 105 additions and 45 deletions

View File

@@ -4,6 +4,7 @@
local love = require 'love'
local game_states = require 'src.states'
local Fader = require 'src.fader'
------------------------------------------------------------------------------
@@ -11,27 +12,17 @@ local game_states = require 'src.states'
------------------------------------------------------------------------------
Current_state = 1
Fade_in = 0.0
Fade_out = 1.0
Fade = Fader()
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()
Fade:fade_in()
end
@@ -39,66 +30,43 @@ 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
if new_state ~= Current_state and Fade.done then
Fade:fade_out()
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
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_out >= 1.0 then
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_out >= 1.0 then
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()
-- 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
Fade:draw()
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
if Fade.done then
game_states[Current_state]:keypressed(key)
end
end
@@ -106,7 +74,7 @@ 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
if Fade.done then
game_states[Current_state]:mousemoved(x, y)
end
end
@@ -114,7 +82,7 @@ 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
if Fade.done then
game_states[Current_state]:mousemoved(x, y, btn)
end
end