From 915b4a2ab8778abb125044592946b11088f23a59 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Tue, 9 Apr 2019 18:47:52 +0200 Subject: [PATCH] modules/assets: improve comments and organisation --- gamecore/modules/assets/animator.lua | 53 +++++++----- gamecore/modules/assets/autotile.lua | 6 ++ gamecore/modules/assets/background.lua | 3 + gamecore/modules/assets/fonts.lua | 8 +- gamecore/modules/assets/imagefonts.lua | 28 +++++++ gamecore/modules/assets/init.lua | 111 ++++++++++++------------- gamecore/modules/assets/sprites.lua | 9 ++ gamecore/modules/assets/texture.lua | 9 ++ gamecore/modules/assets/tileset.lua | 10 ++- 9 files changed, 158 insertions(+), 79 deletions(-) diff --git a/gamecore/modules/assets/animator.lua b/gamecore/modules/assets/animator.lua index 716263c..b64c293 100644 --- a/gamecore/modules/assets/animator.lua +++ b/gamecore/modules/assets/animator.lua @@ -24,6 +24,9 @@ local Animator = Object:extend() +-- INIT FUNCTIONS +-- Initilizing and configuring option + function Animator:new(sprite) self.sprite = sprite self.frame = 1 @@ -40,6 +43,9 @@ function Animator:setCustomSpeed(customSpeed) self.customSpeed = customSpeed or 0 end +-- UPDATE FUNCTIONS +-- Update the animation of the animator + function Animator:update(dt) if (self.currentAnimation == "") then print("warning: no current animation data") @@ -61,25 +67,8 @@ function Animator:update(dt) end end -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 - -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 +-- ANIMATION HANDLING FUNCTIONS +-- Change the animation of the animator function Animator:changeAnimation(name, restart) -- Force restart if animation name is different @@ -102,4 +91,30 @@ function Animator:changeToDefaultAnimation(restart) self:changeAnimation(self.sprite.data.metadata.defaultAnim, restart) 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 diff --git a/gamecore/modules/assets/autotile.lua b/gamecore/modules/assets/autotile.lua index 0cfaf6d..b441ab1 100644 --- a/gamecore/modules/assets/autotile.lua +++ b/gamecore/modules/assets/autotile.lua @@ -27,6 +27,9 @@ local cwd = (...):gsub('%.autotile$', '') .. "." local Tileset = require(cwd .. "tileset") local Autotile = Object:extend() +-- INIT FUNCTIONS +-- Initilizing and configuring option + function Autotile:new(filepath) self.tileset = Tileset(filepath) @@ -36,6 +39,9 @@ function Autotile:new(filepath) self.tilesize = self.metadata.width end +-- DRAW FUNCTIONS +-- Draw tileset using these functions + function Autotile:drawtile(i, j, x, y, r, sx, sy, ox, oy, kx, ky) local i = i or 1 local j = j or 1 diff --git a/gamecore/modules/assets/background.lua b/gamecore/modules/assets/background.lua index 862050b..7a0ac56 100644 --- a/gamecore/modules/assets/background.lua +++ b/gamecore/modules/assets/background.lua @@ -24,6 +24,9 @@ local Background = Object:extend() +-- INIT FUNCTIONS +-- Initilizing and configuring option + function Background:new(filepath) self.image = love.graphics.newImage(filepath) self.batch = love.graphics.newSpriteBatch(self.image , 1000 ) diff --git a/gamecore/modules/assets/fonts.lua b/gamecore/modules/assets/fonts.lua index f2bc7c9..c527144 100644 --- a/gamecore/modules/assets/fonts.lua +++ b/gamecore/modules/assets/fonts.lua @@ -25,6 +25,7 @@ local Font = Object:extend() +-- INIT FUNCTIONS -- Initilizing and configuring option function Font:new(filename, size) @@ -70,7 +71,8 @@ function Font:setLineHeight(height) self.font:setLineHeight(height) end --- get information functions +-- INFO FUNCTIONS +-- get information with these functions function Font:getHeight() local font = self.font @@ -91,7 +93,8 @@ function Font:getColor() return self.color 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) -- 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 -- 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) if self.filter == "shadow" then diff --git a/gamecore/modules/assets/imagefonts.lua b/gamecore/modules/assets/imagefonts.lua index 3cf2c1b..86f318b 100644 --- a/gamecore/modules/assets/imagefonts.lua +++ b/gamecore/modules/assets/imagefonts.lua @@ -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 Font = require(cwd.. "fonts") local ImageFont = Font:extend() +-- INIT FUNCTIONS +-- Initilizing and configuring option + function ImageFont:new(filename, extraspacing) local data = require(filename) local extraspacing = extraspacing or data.extraspacing or 1 diff --git a/gamecore/modules/assets/init.lua b/gamecore/modules/assets/init.lua index 5fe379f..04a7c02 100644 --- a/gamecore/modules/assets/init.lua +++ b/gamecore/modules/assets/init.lua @@ -36,43 +36,25 @@ local Tileset = require(cwd .. "tileset") local Autotile = require(cwd .. "autotile") local Background = require(cwd .. "background") +-- INIT FUNCTIONS +-- Initilizing and configuring option function Assets:new() - self.sprites = {} - self.sfx = {} - self.fonts = {} - self.music = nil - self:clearBackgrounds() - self:clearFonts() - self:clearAutotile() - self:clearTileset() - - self.images = {} + self:clear() 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:clearSprites() + self:clearSFX() + self:clearFonts() + self:resetMusic() + self:clearBackgrounds() self:clearFonts() - self.images = {} + self:clearImages() end function Assets:update(dt) @@ -81,7 +63,8 @@ function Assets:update(dt) end end --- SFX et Musique +-- SFX & MUSICS +-- Handle sound effects and musics function Assets:addSFX(name, filepath) self:newSFX(name, filepath) @@ -96,14 +79,6 @@ function Assets:clearSFX() 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() @@ -112,9 +87,11 @@ function Assets:playSFX(filename) end end -function Assets:playMusic() - if not (self.music == nil) then - love.audio.play(self.music) +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 @@ -122,7 +99,18 @@ function Assets:silence() love.audio.stop() 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) 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) 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() self.backgrounds = {} end -function Assets:addBackground(name, filepath) - self.backgrounds[name] = Background(filepath) -end - --- SPRITES -- - +-- SPRITES FUNCTIONS +-- Animated tileset 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 = {} +function Assets:clearSprites() + self.sprites = {} end +-- FONTS FUNCTIONS +-- Handles fonts and imagesfonts + function Assets:addFont(key, filename, size) local font = Font(filename, size) self.fonts[key] = font @@ -179,7 +170,12 @@ function Assets:getFont(filename) return self.fonts[filename] end --- Tileset +function Assets:clearFonts() + self.fonts = {} +end + +-- TILESET FUNCTIONS +-- Automatically create quads for a texture function Assets:addTileset(name, filepath) self.tileset[name] = Tileset(filepath) @@ -189,7 +185,8 @@ function Assets:clearTileset() self.tileset = {} end --- Autotile +-- AUTOTILE FUNCTIONS +-- Automatically draw tiles function Assets:addAutotile(name, tilesize) self.autotile[name] = Autotile(name, tilesize) diff --git a/gamecore/modules/assets/sprites.lua b/gamecore/modules/assets/sprites.lua index 0749b63..1282363 100644 --- a/gamecore/modules/assets/sprites.lua +++ b/gamecore/modules/assets/sprites.lua @@ -29,6 +29,9 @@ local cwd = (...):gsub('%.sprites$', '') .. "." local Animator = require(cwd .. "animator") local Tileset = require(cwd .. "tileset") +-- INIT FUNCTIONS +-- Initilizing and configuring option + function Sprite:new(filepath) self.tileset = Tileset(filepath) self.data = require(filepath) @@ -60,10 +63,16 @@ function Sprite:changeAnimation(name, restart) self.animator:changeAnimation(name, restart) end +-- INFO FUNCTIONS +-- get information with these functions + function Sprite:animationExist(name) return self.animator:animationExist(name) end +-- DRAW FUNCTIONS +-- Draw sprites using these functions + function Sprite:drawAnimation(x, y, r, sx, sy, ox, oy, kx, ky) self.animator:draw(x, y, r, sx, sy, ox, oy, kx, ky) end diff --git a/gamecore/modules/assets/texture.lua b/gamecore/modules/assets/texture.lua index 63313d9..30901f3 100644 --- a/gamecore/modules/assets/texture.lua +++ b/gamecore/modules/assets/texture.lua @@ -32,6 +32,9 @@ local function getMask(x, y, r, g, b, a) return 1, 1, 1, a end +-- INIT FUNCTIONS +-- Initilizing and configuring option + function Texture:new(filename) self.imageData = love.image.newImageData(filename) @@ -42,10 +45,16 @@ function Texture:new(filename) self.mask = love.graphics.newImage( maskData ) end +-- INFO FUNCTIONS +-- get information with these functions + function Texture:getDimensions() return self.image:getDimensions() end +-- DRAW FUNCTIONS +-- Draw texture using these functions + 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) end diff --git a/gamecore/modules/assets/tileset.lua b/gamecore/modules/assets/tileset.lua index 40ae249..a2f5dff 100644 --- a/gamecore/modules/assets/tileset.lua +++ b/gamecore/modules/assets/tileset.lua @@ -31,6 +31,9 @@ local cwd = (...):gsub('%.tileset$', '') .. "." local Texture = require(cwd .. "texture") +-- INIT FUNCTIONS +-- Initilizing and configuring option + function Tileset:new(filepath) self.texture = Texture(filepath .. ".png") @@ -66,6 +69,9 @@ function Tileset:createQuads() end +-- INFO FUNCTIONS +-- get information with these functions + function Tileset:getTileID_Grid(x, y) local n = (y - 1) * self.gridWidth + x @@ -80,6 +86,9 @@ function Tileset:getTile(n) return self.quads[n] end +-- DRAW FUNCTIONS +-- Draw tileset using these functions + function Tileset:drawTile_Grid(i, j, x, y, r, sx, sy, ox, oy, kx, ky) local tileID = self:getTileID_Grid(i, j) 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) end - return Tileset