Started sketchign the game state.
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 50 KiB |
+43
-77
@@ -2,11 +2,14 @@
|
||||
-- Imports
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local love = require 'love'
|
||||
local assets = require 'src.utils.asstmngr'
|
||||
local make_class = require 'src.utils.classes'
|
||||
local GameState = require 'src.gstates.gstate'
|
||||
local Sprite = require 'src.graphics.sprite'
|
||||
local Fader = require 'src.graphics.fader'
|
||||
local SoundEffect = require 'src.sound.sfx'
|
||||
local Cursor = require 'src.ui.cursor'
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class definitions
|
||||
@@ -21,107 +24,70 @@ local Game = make_class(GameState)
|
||||
|
||||
function Game:_init(name, index)
|
||||
GameState._init(self, name, index)
|
||||
self.skip = false
|
||||
self.fade = Fader()
|
||||
self.stage = 0
|
||||
self.timer = 0
|
||||
|
||||
self.splash = Sprite('imgs/splash.png')
|
||||
self.author = Sprite('imgs/wally.png')
|
||||
self.title = Sprite('imgs/title.png')
|
||||
self.bckg = Sprite('imgs/kitchen.png')
|
||||
self.quit_snd = SoundEffect('snd/byebye.wav')
|
||||
|
||||
self.splash_snd = SoundEffect('snd/jachiev.wav')
|
||||
self.author_snd = SoundEffect('snd/alang.wav')
|
||||
self.title_snd = SoundEffect('snd/ablhole.wav')
|
||||
end
|
||||
-- Create a mouse cursor object at the current mouse position.
|
||||
local mx, my = love.mouse.getPosition()
|
||||
self.cursor = Cursor(mx, my)
|
||||
|
||||
assets:register(self.name, self.cursor)
|
||||
assets:register(self.name, self.bckg)
|
||||
assets:register(self.name, self.quit_snd)
|
||||
|
||||
function Game:load()
|
||||
-- Load all assets.
|
||||
self.splash:load()
|
||||
self.author:load()
|
||||
self.title:load()
|
||||
self.splash_snd:load()
|
||||
self.author_snd:load()
|
||||
self.title_snd:load()
|
||||
|
||||
-- Play the initial sound effect.
|
||||
self.splash_snd:play()
|
||||
self.quit = false
|
||||
end
|
||||
|
||||
|
||||
function Game:update(dt)
|
||||
local total_time
|
||||
if self.all_loaded then
|
||||
-- Update the fader.
|
||||
self.fade:update(dt)
|
||||
|
||||
-- Set time to wait for each intro stage.
|
||||
if self.stage == 0 then
|
||||
total_time = 6.5
|
||||
elseif self.stage == 1 then
|
||||
total_time = 5.0
|
||||
-- Move on to the next game state if the user skipped the intro or all stages are complete.
|
||||
if self.quit then
|
||||
return -1
|
||||
else
|
||||
return self.index
|
||||
end
|
||||
else
|
||||
total_time = 8.0
|
||||
local done = true
|
||||
assets:update(self.name)
|
||||
self.all_loaded = assets:done(self.name) and done
|
||||
return self.index
|
||||
end
|
||||
|
||||
-- Start counting the stage time when the fade in is done.
|
||||
if self.fade.done then
|
||||
self.timer = self.timer + dt
|
||||
end
|
||||
|
||||
-- Check if it's time to change stage.
|
||||
if self.timer >= total_time then
|
||||
self.fade:fade_out(
|
||||
function ()
|
||||
-- Change the stage and play the corresponding sound.
|
||||
self.stage = self.stage + 1
|
||||
|
||||
if self.stage == 1 then
|
||||
self.author_snd:play()
|
||||
elseif self.stage == 2 then
|
||||
self.title_snd:play()
|
||||
end
|
||||
|
||||
-- Request a fade in.
|
||||
self.fade:fade_in()
|
||||
end
|
||||
)
|
||||
|
||||
-- Reset the stage timer.
|
||||
self.timer = 0.0
|
||||
end
|
||||
|
||||
-- Update the fader.
|
||||
self.fade:update(dt)
|
||||
|
||||
-- Move on to the next game state if the user skipped the intro or all stages are complete.
|
||||
if not self.skip and self.stage < 3 then return self.index else return 2 end
|
||||
end
|
||||
|
||||
|
||||
function Game:draw()
|
||||
-- If there is data to draw, then draw the current stage's backdrop.
|
||||
if self.stage == 0 then self.splash:draw()
|
||||
elseif self.stage == 1 then self.author:draw()
|
||||
elseif self.stage == 2 then self.title:draw() end
|
||||
|
||||
-- Draw the fader if needed.
|
||||
self.fade:draw()
|
||||
if self.all_loaded then
|
||||
self.bckg:draw()
|
||||
self.cursor:draw()
|
||||
self.fade:draw()
|
||||
else
|
||||
assets:draw()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function Game:unload()
|
||||
-- Null all assets so they can be claimed by the GC.
|
||||
self.splash:unload()
|
||||
self.author:unload()
|
||||
self.title:unload()
|
||||
self.splash_snd:unload()
|
||||
self.author_snd:unload()
|
||||
self.title_snd:unload()
|
||||
assets:unload(self.name)
|
||||
end
|
||||
|
||||
|
||||
function Game:keypressed(_)
|
||||
function Game:mousemoved(x, y, dx, dy)
|
||||
self.cursor:mousemoved(x, y, dx, dy)
|
||||
end
|
||||
|
||||
|
||||
function Game:keypressed(key)
|
||||
-- Skip the intro on any key press.
|
||||
self.skip = true
|
||||
if key == "escape" then
|
||||
self.quit = true
|
||||
self.quit_snd:play()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -166,6 +166,11 @@ function Menu:mousereleased(x, y, btn)
|
||||
end
|
||||
|
||||
|
||||
function Menu:unload()
|
||||
assets:unload(self.name)
|
||||
end
|
||||
|
||||
|
||||
function Menu:IsCursorInGameBtn(x, y)
|
||||
return x >= 224 and x <= 224 + 89 and y >= 131 and y <= 131 + 30
|
||||
end
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local Menu = require 'src.gstates.menu'
|
||||
local Game = require 'src.gstates.game'
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
@@ -12,6 +13,7 @@ local Menu = require 'src.gstates.menu'
|
||||
-- Table of all valid game states.
|
||||
local states = {
|
||||
Menu('menu', 1),
|
||||
Game('game', 2),
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user