diff --git a/main.lua b/main.lua index ee63d47..f656644 100644 --- a/main.lua +++ b/main.lua @@ -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 diff --git a/src/fader.lua b/src/fader.lua new file mode 100644 index 0000000..aaf30af --- /dev/null +++ b/src/fader.lua @@ -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