Added assets manager singleton.
This commit is contained in:
@@ -16,7 +16,8 @@ local Drawable = make_class(Asset)
|
||||
-- Class methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function Drawable:_init()
|
||||
function Drawable:_init(file_name)
|
||||
Asset._init(self, file_name)
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -17,15 +17,15 @@ local Sprite = make_class(Drawable)
|
||||
-- Class methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function Sprite:_init(sprite_name, x, y)
|
||||
self.sprite_name = string.format('assets/%s', sprite_name)
|
||||
function Sprite:_init(file_name, x, y)
|
||||
Drawable._init(self, file_name)
|
||||
self.x = (x ~= nil and x) or 0
|
||||
self.y = (y ~= nil and y) or 0
|
||||
end
|
||||
|
||||
|
||||
function Sprite:load()
|
||||
self.sprite = love.graphics.newImage(self.sprite_name)
|
||||
self.sprite = love.graphics.newImage(self.file_name)
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local make_class = require 'src.utils.classes'
|
||||
local assets = require 'src.utils.asstmngr'
|
||||
local Drawable = require 'src.graphics.drawable'
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class definitions
|
||||
------------------------------------------------------------------------------
|
||||
@@ -18,8 +20,8 @@ local GameState = make_class(Drawable)
|
||||
|
||||
function GameState:_init(name, index)
|
||||
Drawable._init(self)
|
||||
self.name = name
|
||||
self.index = index
|
||||
self.name = name
|
||||
self.index = index
|
||||
end
|
||||
|
||||
|
||||
@@ -28,6 +30,15 @@ function GameState:update(_)
|
||||
end
|
||||
|
||||
|
||||
function GameState:load()
|
||||
end
|
||||
|
||||
|
||||
function GameState:unload()
|
||||
assets:unload(self.name)
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local love = require 'love'
|
||||
local assets = require 'src.utils.asstmngr'
|
||||
local make_class = require 'src.utils.classes'
|
||||
local GameState = require 'src.gstates.gstate'
|
||||
local Fader = require 'src.graphics.fader'
|
||||
@@ -36,40 +37,37 @@ function MainMenu:_init(name, index)
|
||||
|
||||
-- Create sound effects.
|
||||
self.bgm = SoundEffect('bgm/eskisky.wav')
|
||||
end
|
||||
|
||||
|
||||
function MainMenu:load()
|
||||
-- Load sprites.
|
||||
self.background:load()
|
||||
self.cursor:load()
|
||||
|
||||
-- Load sound effects and start playing the background music
|
||||
self.bgm:load()
|
||||
self.bgm:play()
|
||||
-- Register all assets.
|
||||
assets:register(self.name, self.background)
|
||||
assets:register(self.name, self.cursor)
|
||||
assets:register(self.name, self.bgm)
|
||||
end
|
||||
|
||||
|
||||
function MainMenu:update(dt)
|
||||
-- Update the fader.
|
||||
self.fade:update(dt)
|
||||
if assets:done(self.name) then
|
||||
if not self.bgm:isPlaying() then self.bgm:play() end
|
||||
|
||||
-- Move on to the next game state if the user skipped the intro or all stages are complete.
|
||||
if not self.skip then return self.index else return -1 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 then return self.index else return -1 end
|
||||
else
|
||||
assets:update(self.name)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function MainMenu:draw()
|
||||
self.background:draw()
|
||||
self.cursor:draw()
|
||||
self.fade:draw()
|
||||
end
|
||||
|
||||
|
||||
function MainMenu:unload()
|
||||
self.background:unload()
|
||||
self.cursor:unload()
|
||||
self.bgm:unload()
|
||||
if assets:done(self.name) then
|
||||
self.background:draw()
|
||||
self.cursor:draw()
|
||||
self.fade:draw()
|
||||
else
|
||||
assets:draw()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ local Asset = require 'src.utils.asset'
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
-- SoundEffect is a simple wrapper around Löve's Source class to allow for
|
||||
-- easy loading/unloading and automatically hecking if a sound effect is valid
|
||||
-- easy loading/unloading and automatically checking if a sound effect is valid
|
||||
-- before using it.
|
||||
local SoundEffect = make_class(Asset)
|
||||
|
||||
@@ -21,7 +21,7 @@ local SoundEffect = make_class(Asset)
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function SoundEffect:_init(file_name)
|
||||
self.file_name = string.format('assets/%s', file_name)
|
||||
Asset._init(self, file_name)
|
||||
end
|
||||
|
||||
|
||||
|
||||
53
src/ui/font.lua
Normal file
53
src/ui/font.lua
Normal file
@@ -0,0 +1,53 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Imports
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local love = require 'love'
|
||||
local make_class = require 'src.utils.classes'
|
||||
local Asset = require 'src.utils.asset'
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class definitions
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
-- Font is a simple wrapper around Löve's own Font class to allow for
|
||||
-- easy loading/unloading and automatically checking if it's is valid
|
||||
-- before using it.
|
||||
local Font = make_class(Asset)
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function Font:_init(file_name, size)
|
||||
self.file_name = string.format('assets/%s', file_name)
|
||||
self.size = (size ~= nil and size) or 20
|
||||
end
|
||||
|
||||
|
||||
function Font:load()
|
||||
self.f = love.graphics.newFont(self.file_name, self.size)
|
||||
end
|
||||
|
||||
|
||||
function Font:unload()
|
||||
self.f = nil
|
||||
end
|
||||
|
||||
|
||||
function Font:set()
|
||||
if self.f ~= nil then love.graphics.setFont(self.f) end
|
||||
end
|
||||
|
||||
|
||||
function Font:unset()
|
||||
love.graphics.setFont()
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
return Font
|
||||
@@ -16,15 +16,18 @@ local Asset = make_class()
|
||||
-- Class methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function Asset:_init()
|
||||
function Asset:_init(file_name)
|
||||
self.file_name = string.format('assets/%s', file_name)
|
||||
end
|
||||
|
||||
|
||||
function Asset:load()
|
||||
error 'Attempted to load unimplemented asset.'
|
||||
end
|
||||
|
||||
|
||||
function Asset:unload()
|
||||
error 'Attempted to unload unimplemented asset.'
|
||||
end
|
||||
|
||||
|
||||
|
||||
130
src/utils/asstmngr.lua
Normal file
130
src/utils/asstmngr.lua
Normal file
@@ -0,0 +1,130 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Imports
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local love = require 'love'
|
||||
local make_class = require 'src.utils.classes'
|
||||
local Drawable = require 'src.graphics.drawable'
|
||||
local Sprite = require 'src.graphics.sprite'
|
||||
local Font = require 'src.ui.font'
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class definitions
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local AssetManager = make_class(Drawable)
|
||||
local Asset = require 'src.utils.asset'
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Helper functions
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local function _load(manager, owner)
|
||||
if manager.assets[owner] ~= nil then
|
||||
for k, _ in pairs(manager.assets[owner]) do
|
||||
if k.is_a ~= nil and k.is_a[Asset] then
|
||||
k:load()
|
||||
end
|
||||
coroutine.yield(false)
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function AssetManager:_init()
|
||||
Drawable._init(self)
|
||||
self.assets = {}
|
||||
self.bckg = Sprite('imgs/floppy.png')
|
||||
self.font = Font('fonts/Concrete.ttf', 40)
|
||||
self.co = nil
|
||||
|
||||
self.bckg:load()
|
||||
self.font:load()
|
||||
end
|
||||
|
||||
|
||||
function AssetManager:unload(owner)
|
||||
-- If the owner is known...
|
||||
if self.assets[owner] ~= nil then
|
||||
-- Iterate over it's asset set and unload everything.
|
||||
for k, _ in pairs(self.assets[owner]) do
|
||||
if k.is_a ~= nil and k.is_a[Asset] then
|
||||
k:unload()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function AssetManager:update(owner)
|
||||
if self.assets[owner] ~= nil then
|
||||
if self.co == nil then
|
||||
self.co = coroutine.create(_load)
|
||||
end
|
||||
|
||||
local errorfree, retval = coroutine.resume(self.co, self, owner)
|
||||
|
||||
if errorfree then
|
||||
self.assets[owner].done = retval
|
||||
else
|
||||
error(retval)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function AssetManager:draw()
|
||||
self.bckg:draw()
|
||||
|
||||
love.graphics.setColor(195, 147, 123)
|
||||
self.font:set()
|
||||
love.graphics.print('Loading...', 5, 150)
|
||||
self.font:unset()
|
||||
love.graphics.setColor()
|
||||
end
|
||||
|
||||
|
||||
function AssetManager:register(owner, asset)
|
||||
-- If the owner is new, then create an asset set for it.
|
||||
if self.assets[owner] == nil then
|
||||
self.assets[owner] = {
|
||||
done = false,
|
||||
}
|
||||
end
|
||||
|
||||
-- Store the asset in the set.
|
||||
self.assets[owner][asset] = true
|
||||
end
|
||||
|
||||
|
||||
function AssetManager:remove(owner, asset)
|
||||
-- If the owner is new, then create an asset set for it.
|
||||
if self.assets[owner] ~= nil then
|
||||
-- Unload the asset just in case.
|
||||
if self.assets[owner][asset] ~= nil then
|
||||
self.assets[owner][asset]:unload()
|
||||
end
|
||||
|
||||
-- Then remove it from the set.
|
||||
self.assets[owner][asset] = nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function AssetManager:done(owner)
|
||||
return self.assets[owner] ~= nil and self.assets[owner].done
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
return AssetManager()
|
||||
@@ -20,7 +20,9 @@ local function make_class(BaseClass)
|
||||
cls.__index, cls.is_a = cls, {[cls] = true}
|
||||
|
||||
if BaseClass ~= nil then
|
||||
cls.is_a[BaseClass] = true
|
||||
for k, _ in pairs(BaseClass.is_a) do
|
||||
cls.is_a[k] = true
|
||||
end
|
||||
end
|
||||
|
||||
-- the class's __call metamethod
|
||||
|
||||
Reference in New Issue
Block a user