2019-01-28 14:00:00 +01:00
|
|
|
-- 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.
|
|
|
|
]]
|
|
|
|
|
2019-01-28 09:58:12 +01:00
|
|
|
local Assets = Object:extend()
|
|
|
|
|
2021-05-05 08:39:52 +02:00
|
|
|
local cwd = (...):gsub('%.init$', '') .. ".types."
|
2019-03-10 12:55:16 +01:00
|
|
|
|
2019-03-23 12:02:01 +01:00
|
|
|
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")
|
2019-01-28 09:58:12 +01:00
|
|
|
|
2021-04-21 16:30:52 +02:00
|
|
|
local SFX = require(cwd .. "sfx")
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- Initilizing and configuring option
|
2019-01-28 09:58:12 +01:00
|
|
|
|
|
|
|
function Assets:new()
|
2019-07-26 16:46:43 +02:00
|
|
|
self:clear()
|
2019-03-10 12:55:16 +01:00
|
|
|
|
|
|
|
self.isActive = true
|
2019-01-28 09:58:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:clear()
|
2019-01-28 14:00:00 +01:00
|
|
|
-- TODO: destroy individually each texture/image when assets are cleared
|
2019-07-26 16:46:43 +02:00
|
|
|
self:clearSprites()
|
|
|
|
self:clearSFX()
|
|
|
|
self:clearFonts()
|
|
|
|
self:resetMusic()
|
|
|
|
self:clearBackgrounds()
|
2019-01-28 09:58:12 +01:00
|
|
|
self:clearFonts()
|
2019-07-26 16:46:43 +02:00
|
|
|
self:clearTileset()
|
2019-01-28 09:58:12 +01:00
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
self:clearImages()
|
2019-01-28 09:58:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:update(dt)
|
2019-03-10 12:55:16 +01:00
|
|
|
if (self.isActive) then
|
|
|
|
self:animationsUpdate(dt)
|
|
|
|
end
|
2019-01-28 09:58:12 +01:00
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
-- IMPORT FUNCTIONS
|
|
|
|
-- Easilly import assets
|
|
|
|
|
|
|
|
function Assets:batchImport(datafile)
|
|
|
|
local datas = require(datafile)
|
|
|
|
|
|
|
|
for asset_type, assets in pairs(datas) do
|
|
|
|
if (asset_type == "autotiles") then
|
|
|
|
self:importAutotiles(assets)
|
|
|
|
elseif (asset_type == "backgrounds") then
|
|
|
|
self:importBackgrounds(assets)
|
|
|
|
elseif (asset_type == "fonts") then
|
|
|
|
self:importFonts(assets)
|
|
|
|
elseif (asset_type == "imagefonts") then
|
|
|
|
self:importImageFonts(assets)
|
|
|
|
elseif (asset_type == "images") then
|
|
|
|
self:importTextures(assets)
|
|
|
|
elseif (asset_type == "sprites") then
|
|
|
|
self:importSprites(assets)
|
|
|
|
elseif (asset_type == "textures") then
|
|
|
|
self:importTextures(assets)
|
|
|
|
elseif (asset_type == "tilesets") then
|
|
|
|
self:importTilesets(assets)
|
|
|
|
elseif (asset_type == "sfx") then
|
|
|
|
self:importSFX(assets)
|
|
|
|
else
|
|
|
|
core.debug:warning("assets/importer", "unkown asset type " .. asset_type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:importAutotiles(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addAutotile(asset[1], asset[2])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:importBackgrounds(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addBackground(asset[1], asset[2])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:importFonts(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addFont(asset[1], asset[2], asset[3])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:importImageFonts(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addImageFont(asset[1], asset[2], asset[3])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:importSprites(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addSprite(asset[1], asset[2])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:importTextures(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addImage(asset[1], asset[2])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:importTilesets(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addTileset(asset[1], asset[2])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:importSFX(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addSFX(asset[1], asset[2])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- SFX & MUSICS
|
|
|
|
-- Handle sound effects and musics
|
2019-01-28 09:58:12 +01:00
|
|
|
|
|
|
|
function Assets:addSFX(name, filepath)
|
|
|
|
self:newSFX(name, filepath)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:newSFX(name, filepath)
|
2021-04-21 16:30:52 +02:00
|
|
|
self.sfx[name] = SFX( filepath )
|
2019-01-28 09:58:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:clearSFX()
|
|
|
|
love.audio.stop( )
|
|
|
|
self.sfx = {}
|
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
function Assets:playSFX(filename)
|
|
|
|
if not (self.sfx[filename] == nil) then
|
2021-04-21 16:30:52 +02:00
|
|
|
self.sfx[filename]:play()
|
2019-07-26 16:46:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-28 09:58:12 +01:00
|
|
|
function Assets:setMusic(filename)
|
|
|
|
if filename ~= nil then
|
2021-04-21 17:12:20 +02:00
|
|
|
if (self.music ~= nil) then
|
|
|
|
self.music:stop()
|
|
|
|
end
|
2019-01-28 09:58:12 +01:00
|
|
|
self.music = love.audio.newSource(filename, "stream" )
|
2019-03-10 12:55:16 +01:00
|
|
|
self.music:setVolume(core.options.data.audio.music / 100)
|
2019-01-28 09:58:12 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
function Assets:silence()
|
|
|
|
love.audio.stop()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:resetMusic()
|
|
|
|
self.music = nil
|
2019-01-28 09:58:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:playMusic()
|
|
|
|
if not (self.music == nil) then
|
|
|
|
love.audio.play(self.music)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
-- IMAGES FUNCTIONS
|
|
|
|
-- Create directly texture items
|
2019-01-28 09:58:12 +01:00
|
|
|
|
|
|
|
function Assets:addImage(name, filename)
|
2019-03-23 12:02:01 +01:00
|
|
|
self.images[name] = Texture(filename)
|
2019-01-28 09:58:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:drawImage(name, x, y, r, sx, sy, ox, oy, kx, ky)
|
2019-03-23 12:02:01 +01:00
|
|
|
self.images[name]:draw(x, y, r, sx, sy, ox, oy, kx, ky)
|
2019-01-28 09:58:12 +01:00
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
function Assets:clearImages()
|
|
|
|
self.images = {}
|
2019-01-28 09:58:12 +01:00
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
-- BACKGROUNDS FUNCTIONS
|
|
|
|
-- Automatic tiling texture
|
|
|
|
|
2019-01-28 09:58:12 +01:00
|
|
|
function Assets:addBackground(name, filepath)
|
2019-07-26 16:46:43 +02:00
|
|
|
-- TODO: rework entirely background to work at any size
|
2019-01-28 09:58:12 +01:00
|
|
|
self.backgrounds[name] = Background(filepath)
|
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
function Assets:clearBackgrounds()
|
|
|
|
self.backgrounds = {}
|
|
|
|
end
|
2019-01-28 09:58:12 +01:00
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
-- SPRITES FUNCTIONS
|
|
|
|
-- Animated tileset
|
2019-01-28 09:58:12 +01:00
|
|
|
|
|
|
|
function Assets:addSprite(name, filepath)
|
|
|
|
self.sprites[name] = Sprite(filepath)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:animationsUpdate(dt)
|
|
|
|
for i,v in pairs(self.sprites) do
|
|
|
|
v:update(dt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
function Assets:clearSprites()
|
|
|
|
self.sprites = {}
|
2019-01-28 09:58:12 +01:00
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
-- FONTS FUNCTIONS
|
|
|
|
-- Handles fonts and imagesfonts
|
|
|
|
|
2019-01-28 09:58:12 +01:00
|
|
|
function Assets:addFont(key, filename, size)
|
|
|
|
local font = Font(filename, size)
|
|
|
|
self.fonts[key] = font
|
|
|
|
end
|
|
|
|
|
2019-03-10 12:55:16 +01:00
|
|
|
function Assets:addImageFont(key, filename, extraspacing)
|
|
|
|
local font = ImageFont(filename, extraspacing)
|
|
|
|
self.fonts[key] = font
|
|
|
|
end
|
|
|
|
|
2019-01-28 09:58:12 +01:00
|
|
|
function Assets:getFont(filename)
|
|
|
|
return self.fonts[filename]
|
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
function Assets:clearFonts()
|
|
|
|
self.fonts = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TILESET FUNCTIONS
|
|
|
|
-- Automatically create quads for a texture
|
2019-01-28 09:58:12 +01:00
|
|
|
|
|
|
|
function Assets:addTileset(name, filepath)
|
|
|
|
self.tileset[name] = Tileset(filepath)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:clearTileset()
|
|
|
|
self.tileset = {}
|
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
-- AUTOTILE FUNCTIONS
|
|
|
|
-- Automatically draw tiles
|
2019-01-28 09:58:12 +01:00
|
|
|
|
|
|
|
function Assets:addAutotile(name, tilesize)
|
|
|
|
self.autotile[name] = Autotile(name, tilesize)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:clearAutotile()
|
|
|
|
self.autotile = {}
|
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
-- ACTIVITY FUNCTIONS
|
|
|
|
-- Handle activity
|
|
|
|
|
|
|
|
function Assets:setActivity(activity)
|
|
|
|
self.isActive = activity
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:switchActivity()
|
|
|
|
self.isActive = (self.isActive == false)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:getActivity()
|
|
|
|
return self.isActive
|
|
|
|
end
|
|
|
|
|
2019-01-28 09:58:12 +01:00
|
|
|
return Assets
|