2019-03-16 12:27:38 +01:00
|
|
|
-- assets/sprite :: the assets object, which is basically a tileset animated by
|
|
|
|
-- an animator object. An animator object is always tied to a sprite, but a sprite
|
|
|
|
-- can use different animator in order to make several animator share the same sprite
|
|
|
|
|
|
|
|
--[[
|
|
|
|
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 Sprite = Object:extend()
|
2019-03-16 13:04:26 +01:00
|
|
|
local cwd = (...):gsub('%.sprites$', '') .. "."
|
|
|
|
|
|
|
|
local Animator = require(cwd .. "animator")
|
|
|
|
local Tileset = require(cwd .. "tileset")
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2019-04-09 18:47:52 +02:00
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- Initilizing and configuring option
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Sprite:new(filepath)
|
|
|
|
self.tileset = Tileset(filepath)
|
|
|
|
self.data = require(filepath)
|
|
|
|
|
|
|
|
self.animator = Animator(self)
|
|
|
|
|
|
|
|
self.customSpeed = 0
|
|
|
|
|
|
|
|
self:changeToDefaultAnimation(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Sprite:update(dt)
|
|
|
|
self.animator:update(dt)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Sprite:clone()
|
|
|
|
return Animator(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Sprite:setCustomSpeed(customSpeed)
|
|
|
|
self.animator:setCustomSpeed(customSpeed)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Sprite:changeToDefaultAnimation(restart)
|
|
|
|
self.animator:changeToDefaultAnimation(restart)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Sprite:changeAnimation(name, restart)
|
|
|
|
self.animator:changeAnimation(name, restart)
|
|
|
|
end
|
|
|
|
|
2019-04-09 18:47:52 +02:00
|
|
|
-- INFO FUNCTIONS
|
|
|
|
-- get information with these functions
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Sprite:animationExist(name)
|
|
|
|
return self.animator:animationExist(name)
|
|
|
|
end
|
|
|
|
|
2019-05-06 20:43:51 +02:00
|
|
|
function Sprite:getDimensions()
|
|
|
|
return self.tileset:getDimensions()
|
|
|
|
end
|
|
|
|
|
2019-04-09 18:47:52 +02:00
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- Draw sprites using these functions
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
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
|
|
|
|
|
2019-03-18 17:28:43 +01:00
|
|
|
function Sprite:drawAnimationMask(x, y, r, sx, sy, ox, oy, kx, ky)
|
|
|
|
self.animator:drawMask(x, y, r, sx, sy, ox, oy, kx, ky)
|
|
|
|
end
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Sprite:drawFrame(frame, x, y, r, sx, sy, ox, oy, kx, ky)
|
|
|
|
self.tileset:drawTile(frame, x, y, r, sx, sy, ox, oy, kx, ky)
|
|
|
|
end
|
|
|
|
|
2019-03-18 17:28:43 +01:00
|
|
|
function Sprite:drawFrameMask(frame, x, y, r, sx, sy, ox, oy, kx, ky)
|
|
|
|
self.tileset:drawTileMask(frame, x, y, r, sx, sy, ox, oy, kx, ky)
|
|
|
|
end
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Sprite:drawPart(x, y, w, h, r, sx, sy, ox, oy, kx, ky)
|
|
|
|
local w = math.floor(w)
|
|
|
|
local h = math.floor(h)
|
|
|
|
|
|
|
|
if w >= 0 and h <= 0 then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
love.graphics.setScissor(x - ox, y - oy, w, h)
|
|
|
|
self:drawAnimation(x, y, r, sx, sy, ox, oy, kx, ky)
|
|
|
|
love.graphics.setScissor( )
|
|
|
|
end
|
|
|
|
|
|
|
|
return Sprite
|