2020-11-13 19:11:09 +01:00
|
|
|
-- modules/assets/import :: a simple assets importer, to import easily everything into
|
|
|
|
-- the asset manager.
|
2019-03-16 12:27:38 +01:00
|
|
|
|
|
|
|
--[[
|
|
|
|
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()
|
|
|
|
|
2020-11-13 17:51:12 +01:00
|
|
|
local cwd = (...):gsub('%.init$', '') .. ".types."
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2019-03-18 16:52:27 +01:00
|
|
|
local Texture = require(cwd .. "texture")
|
|
|
|
|
2019-03-16 13:04:26 +01:00
|
|
|
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-03-16 12:27:38 +01:00
|
|
|
|
2020-11-13 17:43:28 +01:00
|
|
|
local SFX = require(cwd .. "sfx")
|
|
|
|
|
2019-04-09 18:47:52 +02:00
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- Initilizing and configuring option
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:new(isGlobal)
|
|
|
|
self.isGlobal = isGlobal
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:clear()
|
2020-11-13 19:11:09 +01:00
|
|
|
if (self.isGlobal) then
|
|
|
|
core.assets:clear()
|
|
|
|
else
|
|
|
|
core.assets:clearLocal()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- WRAPPER FUNCTIONS
|
|
|
|
-- Basic wrappers to ensure compatibility
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:get(key)
|
|
|
|
return core.assets:get(key)
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:getFont(key)
|
|
|
|
return self:getWithType(key, "font")
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:getWithType(key, type)
|
|
|
|
return core.assets:get(key)
|
|
|
|
end
|
2019-04-28 13:02:19 +02:00
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:draw(name, x, y, r, sx, sy, ox, oy, kx, ky)
|
|
|
|
core.assets:draw(name, x, y, r, sx, sy, ox, oy, kx, ky)
|
2019-04-28 13:02:19 +02:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:newSFX(name, filepath)
|
|
|
|
self:addSFX(name, filepath)
|
2019-04-28 13:02:19 +02:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:addImage(name, filename)
|
|
|
|
self:addTexture(name, filename)
|
2019-04-28 13:02:19 +02:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:playSFX(name)
|
|
|
|
core.assets:playSFX(name)
|
2019-04-28 13:02:19 +02:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:drawImage(name, x, y, r, sx, sy, ox, oy, kx, ky)
|
|
|
|
self:draw(name, x, y, r, sx, sy, ox, oy, kx, ky)
|
2019-04-28 13:02:19 +02:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
-- Music
|
|
|
|
|
|
|
|
function Assets:setMusic(filename, loop)
|
|
|
|
core.debug:logDebug("assets", "Assets:setMusic is deprecated")
|
|
|
|
core.music:addMusic(filename, true)
|
2019-04-28 13:02:19 +02:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:silence()
|
|
|
|
core.debug:logDebug("assets", "Assets:silence is deprecated")
|
|
|
|
core.music:silence()
|
2019-04-28 13:02:19 +02:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:resetMusic()
|
|
|
|
core.debug:logDebug("assets", "Assets:resetMusic is deprecated")
|
|
|
|
core.music:purge()
|
2019-04-28 13:02:19 +02:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:playMusic()
|
|
|
|
core.debug:logDebug("assets", "Assets:playMusic is deprecated")
|
|
|
|
core.music:playMusic()
|
2019-04-28 13:02:19 +02:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
-- Activity
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:setActivity(activity)
|
|
|
|
core.assets:setActivity(activity)
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:switchActivity()
|
|
|
|
core.assets:switchActivity()
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:getActivity()
|
|
|
|
core.assets:getActivity()
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
-- IMPORT FUNCTIONS
|
|
|
|
-- Easilly import assets
|
2019-04-09 18:47:52 +02:00
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
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:logWarn("assets/importer", "unkown asset type " .. asset_type)
|
|
|
|
end
|
|
|
|
end
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:importAutotiles(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addAutotile(asset[1], asset[2])
|
|
|
|
end
|
2019-04-09 18:47:52 +02:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:importBackgrounds(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addBackground(asset[1], asset[2])
|
|
|
|
end
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:importFonts(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addFont(asset[1], asset[2], asset[3])
|
|
|
|
end
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:importImageFonts(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addImageFont(asset[1], asset[2], asset[3])
|
|
|
|
end
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:importSprites(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addSprite(asset[1], asset[2])
|
|
|
|
end
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:importTextures(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addImage(asset[1], asset[2])
|
|
|
|
end
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:importTilesets(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addTileset(asset[1], asset[2])
|
|
|
|
end
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:importSFX(assets)
|
|
|
|
for i, asset in ipairs(assets) do
|
|
|
|
self:addSFX(asset[1], asset[2])
|
|
|
|
end
|
2019-04-09 18:47:52 +02:00
|
|
|
end
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
-- ADD FUNCTIONS
|
|
|
|
-- Different wrapper to create easily asset objects
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:add(name, assetObject)
|
|
|
|
core.assets:add(name, assetObject, self.isGlobal)
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:addBackground(name, filepath)
|
|
|
|
self:add(name, Background(filepath))
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:addSprite(name, filepath)
|
|
|
|
self:add(name, Sprite(filepath))
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:addFont(key, filename, size)
|
2020-11-13 19:11:09 +01:00
|
|
|
self:add(key, Font(filename, size))
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:addImageFont(key, filename, extraspacing)
|
2020-11-13 19:11:09 +01:00
|
|
|
self:add(key, ImageFont(filename, extraspacing))
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:addTileset(name, filepath)
|
|
|
|
self:add(name, Tileset(filepath))
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:addAutotile(name, tilesize)
|
|
|
|
self:add(name, Autotile(name, tilesize))
|
2019-04-09 18:47:52 +02:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:addSFX(name, filepath)
|
|
|
|
self:add(name, SFX(filepath))
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:addTexture(name, filename)
|
|
|
|
self:add(name, Texture(filename))
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
-- DEPRECATED FUNCTIONS
|
|
|
|
-- Don't do anything and will be removed soon
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:clearFonts()
|
|
|
|
core.debug:logDebug("assets", "Assets:clearFonts is deprecated")
|
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:clearTileset()
|
|
|
|
core.debug:logDebug("assets", "Assets:clearTileset is deprecated")
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Assets:clearAutotile()
|
2020-11-13 19:11:09 +01:00
|
|
|
core.debug:logDebug("assets", "Assets:clearAutotile is deprecated")
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:clearSFX()
|
|
|
|
core.debug:logDebug("assets", "Assets:clearSFX is deprecated")
|
|
|
|
end
|
2019-05-30 13:54:39 +02:00
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:clearBackgrounds()
|
|
|
|
core.debug:logDebug("assets", "Assets:clearBackgrounds is deprecated")
|
2019-05-30 13:54:39 +02:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:clearSprites()
|
|
|
|
core.debug:logDebug("assets", "Assets:clearSprites is deprecated")
|
2019-05-30 13:54:39 +02:00
|
|
|
end
|
|
|
|
|
2020-11-13 19:11:09 +01:00
|
|
|
function Assets:clearImages()
|
|
|
|
core.debug:logDebug("assets", "Assets:clearImages is deprecated")
|
2019-05-30 13:54:39 +02:00
|
|
|
end
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
return Assets
|