epervier-old/gamecore/modules/assets/init.lua
2019-03-18 16:52:27 +01:00

202 lines
4.6 KiB
Lua

-- modules/assets :: a simple assets manager, aim to put every assets in a simple
-- serie of table in order to find them easily.
--[[
Copyright © 2019 Kazhnuz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local Assets = Object:extend()
local cwd = (...):gsub('%.init$', '') .. "."
local Texture = require(cwd .. "texture")
local Sprite = require(cwd .. "sprites")
local Font = require(cwd .. "fonts")
local ImageFont = require(cwd .. "imagefonts")
local Tileset = require(cwd .. "tileset")
local Autotile = require(cwd .. "autotile")
local Background = require(cwd .. "background")
function Assets:new()
self.sprites = {}
self.sfx = {}
self.fonts = {}
self.music = nil
self:clearBackgrounds()
self:clearFonts()
self:clearAutotile()
self:clearTileset()
self.images = {}
self.isActive = true
end
function Assets:init()
self.sprites = {}
self.sfx = {}
self.fonts = {}
self.music = nil
self.backgrounds= {}
self:clearFonts()
self.images = {}
end
function Assets:clear()
-- TODO: destroy individually each texture/image when assets are cleared
self.sprites = {}
self.sfx = {}
self.fonts = {}
self.music = nil
self.backgrounds= {}
self:clearFonts()
self.images = {}
end
function Assets:update(dt)
if (self.isActive) then
self:animationsUpdate(dt)
end
end
-- SFX et Musique
function Assets:addSFX(name, filepath)
self:newSFX(name, filepath)
end
function Assets:newSFX(name, filepath)
self.sfx[name] = love.audio.newSource( filepath, "static" )
end
function Assets:clearSFX()
love.audio.stop( )
self.sfx = {}
end
function Assets:setMusic(filename)
if filename ~= nil then
love.audio.stop( )
self.music = love.audio.newSource(filename, "stream" )
self.music:setVolume(core.options.data.audio.music / 100)
end
end
function Assets:playSFX(filename)
if not (self.sfx[filename] == nil) then
self.sfx[filename]:stop()
self.sfx[filename]:setVolume(core.options.data.audio.sfx / 100)
love.audio.play( self.sfx[filename] )
end
end
function Assets:playMusic()
if not (self.music == nil) then
love.audio.play(self.music)
end
end
function Assets:silence()
love.audio.stop()
end
-- Background --
function Assets:addImage(name, filename)
self.images[name] = Texture(filename)
end
function Assets:drawImage(name, x, y, r, sx, sy, ox, oy, kx, ky)
self.images[name]:draw(x, y, r, sx, sy, ox, oy, kx, ky)
end
-- Images --
function Assets:clearBackgrounds()
self.backgrounds = {}
end
function Assets:addBackground(name, filepath)
self.backgrounds[name] = Background(filepath)
end
-- SPRITES --
function Assets:addSprite(name, filepath)
self.sprites[name] = Sprite(filepath)
end
function Assets:clearSprites()
self.sprites = {}
end
function Assets:animationsUpdate(dt)
for i,v in pairs(self.sprites) do
v:update(dt)
end
end
-- FONTS --
function Assets:clearFonts()
self.fonts = {}
end
function Assets:addFont(key, filename, size)
local font = Font(filename, size)
self.fonts[key] = font
end
function Assets:addImageFont(key, filename, extraspacing)
local font = ImageFont(filename, extraspacing)
self.fonts[key] = font
end
function Assets:getFont(filename)
return self.fonts[filename]
end
-- Tileset
function Assets:addTileset(name, filepath)
self.tileset[name] = Tileset(filepath)
end
function Assets:clearTileset()
self.tileset = {}
end
-- Autotile
function Assets:addAutotile(name, tilesize)
self.autotile[name] = Autotile(name, tilesize)
end
function Assets:clearAutotile()
self.autotile = {}
end
return Assets