Started implementing game states and intro.
This commit is contained in:
122
main.lua
122
main.lua
@@ -1,46 +1,120 @@
|
||||
---@diagnostic disable: undefined-global
|
||||
Mousex = 0
|
||||
Mousey = 0
|
||||
Cursor = nil
|
||||
Bgm = nil
|
||||
Click = nil
|
||||
Bckg = nil
|
||||
------------------------------------------------------------------------------
|
||||
-- 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()
|
||||
Cursor = love.graphics.newImage('imgs/pointer.png')
|
||||
Bckg = love.graphics.newImage('imgs/bckg.png')
|
||||
game_states[Current_state]:load()
|
||||
end
|
||||
|
||||
|
||||
Bgm = love.audio.newSource('bgm/dogs.wav')
|
||||
Bgm:setLooping(true)
|
||||
Bgm:play()
|
||||
function love.update(dt)
|
||||
local new_state = game_states[Current_state]:update(dt)
|
||||
|
||||
Click = love.audio.newSource('snd/click.wav')
|
||||
-- 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()
|
||||
love.graphics.draw(Bckg, 0, 0)
|
||||
love.graphics.draw(Cursor, Mousex , Mousey)
|
||||
-- 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)
|
||||
if key == "escape" then
|
||||
love.event.quit()
|
||||
end
|
||||
-- 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)
|
||||
Mousex = x
|
||||
Mousey = 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, button)
|
||||
if button == 1 then
|
||||
Click:play()
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user