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 love = require 'love'
local game_states = require 'src.states' local game_states = require 'src.states'
local Fader = require 'src.fader'
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
@@ -11,27 +12,17 @@ local game_states = require 'src.states'
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
Current_state = 1 Current_state = 1
Fade_in = 0.0 Fade = Fader()
Fade_out = 1.0
Change_state = false 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 -- Game methods
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
function love.load() function love.load()
game_states[Current_state]:load() game_states[Current_state]:load()
Fade:fade_in()
end end
@@ -39,66 +30,43 @@ function love.update(dt)
local new_state = game_states[Current_state]:update(dt) local new_state = game_states[Current_state]:update(dt)
-- If the game state changed then trigger a fade out. -- If the game state changed then trigger a fade out.
if new_state ~= Current_state and Fade_out >= 1.0 then if new_state ~= Current_state and Fade.done then
Fade_out = 0.0 Fade:fade_out()
Change_state = true Change_state = true
end end
-- If there is a fade in going then update it. Fade:update(dt)
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. -- Update the game state.
if game_states[new_state] == nil then 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 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) game_states[Current_state]:unload(dt)
love.event.quit() love.event.quit()
end end
else else
-- If the new state exists then unload it's data and set the new state. -- 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) game_states[Current_state]:unload(dt)
Current_state = new_state Current_state = new_state
game_states[Current_state]:load() game_states[Current_state]:load()
Change_state = false Change_state = false
Fade:fade_in()
end end
end end
end end
function love.draw() function love.draw()
-- Reset the drawing color and clear the screen.
love.graphics.clear() love.graphics.clear()
-- Render the current game state.
game_states[Current_state]:draw() game_states[Current_state]:draw()
Fade: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 end
function love.keypressed(key) function love.keypressed(key)
-- Send events to the active game state if there is no fade active. -- 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) game_states[Current_state]:keypressed(key)
end end
end end
@@ -106,7 +74,7 @@ end
function love.mousemoved(x, y) function love.mousemoved(x, y)
-- Send events to the active game state if there is no fade active. -- 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) game_states[Current_state]:mousemoved(x, y)
end end
end end
@@ -114,7 +82,7 @@ end
function love.mousepressed(x, y, btn) function love.mousepressed(x, y, btn)
-- Send events to the active game state if there is no fade active. -- 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) game_states[Current_state]:mousemoved(x, y, btn)
end end
end end

92
src/fader.lua Normal file
View File

@@ -0,0 +1,92 @@
------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------
local love = require 'love'
local make_class = require 'src.classes'
------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------
local Fader = make_class()
------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------
function Fader:_init()
self.t = 0.0
self.step = 0
self.done = true
self.reverse = false
end
function Fader:_reset()
self.t = 0
self.step = 0
self.done = false
end
function Fader:fade_in()
self:_reset()
self.reverse = true
end
function Fader:fade_out()
self:_reset()
self.reverse = false
end
function Fader:update(dt)
if not self.done then
self.t = self.t + dt
if self.t >= 0.009 then
self.step = self.step + 1
self.t = 0.0
end
if self.step > 175 then self.done = true end
end
end
function Fader:draw()
local c = 0
love.graphics.setColor(0, 0, 0)
for i = 0, 16 do
for j = 0, 10 do
if not self.reverse then
if c <= self.step then
love.graphics.rectangle('fill', 20 * i, 20 * j, 20, 20)
end
else
if c <= self.step then else
love.graphics.rectangle('fill', 20 * i, 20 * j, 20, 20)
end
end
c = c + 1
end
end
love.graphics.setColor()
-- love.graphics.print(string.format('t: %s', self.t), 5, 35)
-- love.graphics.print(string.format('step: %s', self.step), 5, 45)
-- love.graphics.print(string.format('W: %s', love.graphics.getWidth()), 5, 55)
-- love.graphics.print(string.format('H: %s', love.graphics.getHeight()), 5, 65)
end
------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------
return Fader