modules/assets: improve comments and organisation

This commit is contained in:
Kazhnuz 2019-04-09 18:47:52 +02:00
parent f2680c37f3
commit 915b4a2ab8
9 changed files with 158 additions and 79 deletions

View file

@ -24,6 +24,9 @@
local Animator = Object:extend() local Animator = Object:extend()
-- INIT FUNCTIONS
-- Initilizing and configuring option
function Animator:new(sprite) function Animator:new(sprite)
self.sprite = sprite self.sprite = sprite
self.frame = 1 self.frame = 1
@ -40,6 +43,9 @@ function Animator:setCustomSpeed(customSpeed)
self.customSpeed = customSpeed or 0 self.customSpeed = customSpeed or 0
end end
-- UPDATE FUNCTIONS
-- Update the animation of the animator
function Animator:update(dt) function Animator:update(dt)
if (self.currentAnimation == "") then if (self.currentAnimation == "") then
print("warning: no current animation data") print("warning: no current animation data")
@ -61,25 +67,8 @@ function Animator:update(dt)
end end
end end
function Animator:getAnimationDuration(animation) -- ANIMATION HANDLING FUNCTIONS
return (self.animationData.endAt - self.animationData.startAt) / self.animationData.speed -- Change the animation of the animator
end
function Animator:getFrame()
return self.frame
end
function Animator:animationExist(name)
return (self.sprite.data.animations[self.currentAnimation] ~= nil)
end
function Animator:draw(x, y, r, sx, sy, ox, oy, kx, ky)
self.sprite:drawFrame(self.frame, x, y, r, sx, sy, ox, oy, kx, ky)
end
function Animator:drawMask(x, y, r, sx, sy, ox, oy, kx, ky)
self.sprite:drawFrameMask(self.frame, x, y, r, sx, sy, ox, oy, kx, ky)
end
function Animator:changeAnimation(name, restart) function Animator:changeAnimation(name, restart)
-- Force restart if animation name is different -- Force restart if animation name is different
@ -102,4 +91,30 @@ function Animator:changeToDefaultAnimation(restart)
self:changeAnimation(self.sprite.data.metadata.defaultAnim, restart) self:changeAnimation(self.sprite.data.metadata.defaultAnim, restart)
end end
-- INFO FUNCTIONS
-- get information with these functions
function Animator:getAnimationDuration(animation)
return (self.animationData.endAt - self.animationData.startAt) / self.animationData.speed
end
function Animator:getFrame()
return self.frame
end
function Animator:animationExist(name)
return (self.sprite.data.animations[self.currentAnimation] ~= nil)
end
-- DRAW FUNCTIONS
-- Draw animations using these functions
function Animator:draw(x, y, r, sx, sy, ox, oy, kx, ky)
self.sprite:drawFrame(self.frame, x, y, r, sx, sy, ox, oy, kx, ky)
end
function Animator:drawMask(x, y, r, sx, sy, ox, oy, kx, ky)
self.sprite:drawFrameMask(self.frame, x, y, r, sx, sy, ox, oy, kx, ky)
end
return Animator return Animator

View file

@ -27,6 +27,9 @@ local cwd = (...):gsub('%.autotile$', '') .. "."
local Tileset = require(cwd .. "tileset") local Tileset = require(cwd .. "tileset")
local Autotile = Object:extend() local Autotile = Object:extend()
-- INIT FUNCTIONS
-- Initilizing and configuring option
function Autotile:new(filepath) function Autotile:new(filepath)
self.tileset = Tileset(filepath) self.tileset = Tileset(filepath)
@ -36,6 +39,9 @@ function Autotile:new(filepath)
self.tilesize = self.metadata.width self.tilesize = self.metadata.width
end end
-- DRAW FUNCTIONS
-- Draw tileset using these functions
function Autotile:drawtile(i, j, x, y, r, sx, sy, ox, oy, kx, ky) function Autotile:drawtile(i, j, x, y, r, sx, sy, ox, oy, kx, ky)
local i = i or 1 local i = i or 1
local j = j or 1 local j = j or 1

View file

@ -24,6 +24,9 @@
local Background = Object:extend() local Background = Object:extend()
-- INIT FUNCTIONS
-- Initilizing and configuring option
function Background:new(filepath) function Background:new(filepath)
self.image = love.graphics.newImage(filepath) self.image = love.graphics.newImage(filepath)
self.batch = love.graphics.newSpriteBatch(self.image , 1000 ) self.batch = love.graphics.newSpriteBatch(self.image , 1000 )

View file

@ -25,6 +25,7 @@
local Font = Object:extend() local Font = Object:extend()
-- INIT FUNCTIONS
-- Initilizing and configuring option -- Initilizing and configuring option
function Font:new(filename, size) function Font:new(filename, size)
@ -70,7 +71,8 @@ function Font:setLineHeight(height)
self.font:setLineHeight(height) self.font:setLineHeight(height)
end end
-- get information functions -- INFO FUNCTIONS
-- get information with these functions
function Font:getHeight() function Font:getHeight()
local font = self.font local font = self.font
@ -91,7 +93,8 @@ function Font:getColor()
return self.color return self.color
end end
-- print functions -- DRAW FUNCTIONS
-- print text using theses functions
function Font:draw(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky) function Font:draw(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
-- draw text with color and effect applied -- draw text with color and effect applied
@ -124,6 +127,7 @@ function Font:printf(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
end end
-- FILTER SYSTEM -- FILTER SYSTEM
-- With these filter, you can apply custom effects to the fonts
function Font:applyFilter(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky) function Font:applyFilter(text, x, y, limit, align, r, sx, sy, ox, oy, kx, ky)
if self.filter == "shadow" then if self.filter == "shadow" then

View file

@ -1,7 +1,35 @@
-- assets/fonts :: the imagefonts object, which are basically a bitmap version
-- of the font object.
--[[
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 cwd = (...):gsub('%.imagefonts$', '') .. "." local cwd = (...):gsub('%.imagefonts$', '') .. "."
local Font = require(cwd.. "fonts") local Font = require(cwd.. "fonts")
local ImageFont = Font:extend() local ImageFont = Font:extend()
-- INIT FUNCTIONS
-- Initilizing and configuring option
function ImageFont:new(filename, extraspacing) function ImageFont:new(filename, extraspacing)
local data = require(filename) local data = require(filename)
local extraspacing = extraspacing or data.extraspacing or 1 local extraspacing = extraspacing or data.extraspacing or 1

View file

@ -36,43 +36,25 @@ local Tileset = require(cwd .. "tileset")
local Autotile = require(cwd .. "autotile") local Autotile = require(cwd .. "autotile")
local Background = require(cwd .. "background") local Background = require(cwd .. "background")
-- INIT FUNCTIONS
-- Initilizing and configuring option
function Assets:new() function Assets:new()
self.sprites = {} self:clear()
self.sfx = {}
self.fonts = {}
self.music = nil
self:clearBackgrounds()
self:clearFonts()
self:clearAutotile()
self:clearTileset()
self.images = {}
self.isActive = true self.isActive = true
end end
function Assets:init()
self.sprites = {}
self.sfx = {}
self.fonts = {}
self.music = nil
self.backgrounds= {}
self:clearFonts()
self.images = {}
end
function Assets:clear() function Assets:clear()
-- TODO: destroy individually each texture/image when assets are cleared -- TODO: destroy individually each texture/image when assets are cleared
self.sprites = {} self:clearSprites()
self.sfx = {} self:clearSFX()
self.fonts = {} self:clearFonts()
self.music = nil self:resetMusic()
self.backgrounds= {} self:clearBackgrounds()
self:clearFonts() self:clearFonts()
self.images = {} self:clearImages()
end end
function Assets:update(dt) function Assets:update(dt)
@ -81,7 +63,8 @@ function Assets:update(dt)
end end
end end
-- SFX et Musique -- SFX & MUSICS
-- Handle sound effects and musics
function Assets:addSFX(name, filepath) function Assets:addSFX(name, filepath)
self:newSFX(name, filepath) self:newSFX(name, filepath)
@ -96,14 +79,6 @@ function Assets:clearSFX()
self.sfx = {} self.sfx = {}
end 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) function Assets:playSFX(filename)
if not (self.sfx[filename] == nil) then if not (self.sfx[filename] == nil) then
self.sfx[filename]:stop() self.sfx[filename]:stop()
@ -112,9 +87,11 @@ function Assets:playSFX(filename)
end end
end end
function Assets:playMusic() function Assets:setMusic(filename)
if not (self.music == nil) then if filename ~= nil then
love.audio.play(self.music) love.audio.stop( )
self.music = love.audio.newSource(filename, "stream" )
self.music:setVolume(core.options.data.audio.music / 100)
end end
end end
@ -122,7 +99,18 @@ function Assets:silence()
love.audio.stop() love.audio.stop()
end end
-- Background -- function Assets:resetMusic()
self.music = nil
end
function Assets:playMusic()
if not (self.music == nil) then
love.audio.play(self.music)
end
end
-- IMAGES FUNCTIONS
-- Create directly texture items
function Assets:addImage(name, filename) function Assets:addImage(name, filename)
self.images[name] = Texture(filename) self.images[name] = Texture(filename)
@ -132,39 +120,42 @@ 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) self.images[name]:draw(x, y, r, sx, sy, ox, oy, kx, ky)
end end
-- Images -- function Assets:clearImages()
self.images = {}
end
-- BACKGROUNDS FUNCTIONS
-- Automatic tiling texture
function Assets:addBackground(name, filepath)
-- TODO: rework entirely background to work at any size
self.backgrounds[name] = Background(filepath)
end
function Assets:clearBackgrounds() function Assets:clearBackgrounds()
self.backgrounds = {} self.backgrounds = {}
end end
function Assets:addBackground(name, filepath) -- SPRITES FUNCTIONS
self.backgrounds[name] = Background(filepath) -- Animated tileset
end
-- SPRITES --
function Assets:addSprite(name, filepath) function Assets:addSprite(name, filepath)
self.sprites[name] = Sprite(filepath) self.sprites[name] = Sprite(filepath)
end end
function Assets:clearSprites()
self.sprites = {}
end
function Assets:animationsUpdate(dt) function Assets:animationsUpdate(dt)
for i,v in pairs(self.sprites) do for i,v in pairs(self.sprites) do
v:update(dt) v:update(dt)
end end
end end
-- FONTS -- function Assets:clearSprites()
self.sprites = {}
function Assets:clearFonts()
self.fonts = {}
end end
-- FONTS FUNCTIONS
-- Handles fonts and imagesfonts
function Assets:addFont(key, filename, size) function Assets:addFont(key, filename, size)
local font = Font(filename, size) local font = Font(filename, size)
self.fonts[key] = font self.fonts[key] = font
@ -179,7 +170,12 @@ function Assets:getFont(filename)
return self.fonts[filename] return self.fonts[filename]
end end
-- Tileset function Assets:clearFonts()
self.fonts = {}
end
-- TILESET FUNCTIONS
-- Automatically create quads for a texture
function Assets:addTileset(name, filepath) function Assets:addTileset(name, filepath)
self.tileset[name] = Tileset(filepath) self.tileset[name] = Tileset(filepath)
@ -189,7 +185,8 @@ function Assets:clearTileset()
self.tileset = {} self.tileset = {}
end end
-- Autotile -- AUTOTILE FUNCTIONS
-- Automatically draw tiles
function Assets:addAutotile(name, tilesize) function Assets:addAutotile(name, tilesize)
self.autotile[name] = Autotile(name, tilesize) self.autotile[name] = Autotile(name, tilesize)

View file

@ -29,6 +29,9 @@ local cwd = (...):gsub('%.sprites$', '') .. "."
local Animator = require(cwd .. "animator") local Animator = require(cwd .. "animator")
local Tileset = require(cwd .. "tileset") local Tileset = require(cwd .. "tileset")
-- INIT FUNCTIONS
-- Initilizing and configuring option
function Sprite:new(filepath) function Sprite:new(filepath)
self.tileset = Tileset(filepath) self.tileset = Tileset(filepath)
self.data = require(filepath) self.data = require(filepath)
@ -60,10 +63,16 @@ function Sprite:changeAnimation(name, restart)
self.animator:changeAnimation(name, restart) self.animator:changeAnimation(name, restart)
end end
-- INFO FUNCTIONS
-- get information with these functions
function Sprite:animationExist(name) function Sprite:animationExist(name)
return self.animator:animationExist(name) return self.animator:animationExist(name)
end end
-- DRAW FUNCTIONS
-- Draw sprites using these functions
function Sprite:drawAnimation(x, y, r, sx, sy, ox, oy, kx, ky) function Sprite:drawAnimation(x, y, r, sx, sy, ox, oy, kx, ky)
self.animator:draw(x, y, r, sx, sy, ox, oy, kx, ky) self.animator:draw(x, y, r, sx, sy, ox, oy, kx, ky)
end end

View file

@ -32,6 +32,9 @@ local function getMask(x, y, r, g, b, a)
return 1, 1, 1, a return 1, 1, 1, a
end end
-- INIT FUNCTIONS
-- Initilizing and configuring option
function Texture:new(filename) function Texture:new(filename)
self.imageData = love.image.newImageData(filename) self.imageData = love.image.newImageData(filename)
@ -42,10 +45,16 @@ function Texture:new(filename)
self.mask = love.graphics.newImage( maskData ) self.mask = love.graphics.newImage( maskData )
end end
-- INFO FUNCTIONS
-- get information with these functions
function Texture:getDimensions() function Texture:getDimensions()
return self.image:getDimensions() return self.image:getDimensions()
end end
-- DRAW FUNCTIONS
-- Draw texture using these functions
function Texture:draw(x, y, r, sx, sy, ox, oy, kx, ky) function Texture:draw(x, y, r, sx, sy, ox, oy, kx, ky)
love.graphics.draw(self.image, x, y, r, sx, sy, ox, oy, kx, ky) love.graphics.draw(self.image, x, y, r, sx, sy, ox, oy, kx, ky)
end end

View file

@ -31,6 +31,9 @@ local cwd = (...):gsub('%.tileset$', '') .. "."
local Texture = require(cwd .. "texture") local Texture = require(cwd .. "texture")
-- INIT FUNCTIONS
-- Initilizing and configuring option
function Tileset:new(filepath) function Tileset:new(filepath)
self.texture = Texture(filepath .. ".png") self.texture = Texture(filepath .. ".png")
@ -66,6 +69,9 @@ function Tileset:createQuads()
end end
-- INFO FUNCTIONS
-- get information with these functions
function Tileset:getTileID_Grid(x, y) function Tileset:getTileID_Grid(x, y)
local n = (y - 1) * self.gridWidth + x local n = (y - 1) * self.gridWidth + x
@ -80,6 +86,9 @@ function Tileset:getTile(n)
return self.quads[n] return self.quads[n]
end end
-- DRAW FUNCTIONS
-- Draw tileset using these functions
function Tileset:drawTile_Grid(i, j, x, y, r, sx, sy, ox, oy, kx, ky) function Tileset:drawTile_Grid(i, j, x, y, r, sx, sy, ox, oy, kx, ky)
local tileID = self:getTileID_Grid(i, j) local tileID = self:getTileID_Grid(i, j)
self.texture:drawQuad(self.quads[tileID], x, y, r, sx, sy, ox, oy, kx, ky) self.texture:drawQuad(self.quads[tileID], x, y, r, sx, sy, ox, oy, kx, ky)
@ -98,5 +107,4 @@ function Tileset:drawTileMask(id, x, y, r, sx, sy, ox, oy, kx, ky)
self.texture:drawMaskQuad(self.quads[id], x, y, r, sx, sy, ox, oy, kx, ky) self.texture:drawMaskQuad(self.quads[id], x, y, r, sx, sy, ox, oy, kx, ky)
end end
return Tileset return Tileset