feat(actors/visuals): add support for sprite
This commit is contained in:
parent
6a4d90149f
commit
72ecc647cd
2 changed files with 105 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
||||||
local Rectangle = require "framework.scenes.world.actors.visuals.rectangle"
|
local Rectangle = require "framework.scenes.world.actors.visuals.rectangle"
|
||||||
|
local Sprite = require "framework.scenes.world.actors.visuals.sprite"
|
||||||
local Visual = Object:extend()
|
local Visual = Object:extend()
|
||||||
|
|
||||||
local Vector3D = require "framework.libs.brinevector3D"
|
local Vector3D = require "framework.libs.brinevector3D"
|
||||||
|
@ -8,6 +9,8 @@ function Visual:_initVisual(def)
|
||||||
self.visualMode = def.mode or ""
|
self.visualMode = def.mode or ""
|
||||||
if (self.visualMode == "box") then
|
if (self.visualMode == "box") then
|
||||||
self:_initBox(def.color or {r = 0, g = 0, b = 0})
|
self:_initBox(def.color or {r = 0, g = 0, b = 0})
|
||||||
|
elseif (self.visualMode == "sprite") then
|
||||||
|
self:_initSprite(def.assetName, def.clone == true, def.origin or {}, def.getHitboxes == true)
|
||||||
end
|
end
|
||||||
|
|
||||||
if (self.visual ~= nil) then
|
if (self.visual ~= nil) then
|
||||||
|
@ -66,6 +69,10 @@ end
|
||||||
-- SPRITE HANDLING
|
-- SPRITE HANDLING
|
||||||
-- Handle the sprite mode for visual
|
-- Handle the sprite mode for visual
|
||||||
|
|
||||||
|
function Visual:_initSprite(name, isClone, origin, getHitboxes)
|
||||||
|
self.visual = Sprite(self, name, isClone, origin, getHitboxes)
|
||||||
|
end
|
||||||
|
|
||||||
-- TILE HANDLING
|
-- TILE HANDLING
|
||||||
-- Handle the tile mode for visual
|
-- Handle the tile mode for visual
|
||||||
|
|
||||||
|
|
98
framework/scenes/world/actors/visuals/sprite.lua
Normal file
98
framework/scenes/world/actors/visuals/sprite.lua
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
local Sprite = Object:extend()
|
||||||
|
local Vector3D = require "framework.libs.brinevector3D"
|
||||||
|
|
||||||
|
-- SPRITES FUNCTIONS
|
||||||
|
-- Handle the sprite of the actor
|
||||||
|
|
||||||
|
function Sprite:new(owner, name, isClone, origin, getHitboxes)
|
||||||
|
self.owner = owner
|
||||||
|
self.animators = self.owner.world.animators
|
||||||
|
self.name = name or nil
|
||||||
|
self.origin = Vector3D(origin.x or 0, origin.y or 0, 0)
|
||||||
|
self.scale = {x = 1, y = 1}
|
||||||
|
self.exist = (name ~= nil)
|
||||||
|
self.getHitboxes = getHitboxes
|
||||||
|
if (isClone) then
|
||||||
|
self:clone()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:clone()
|
||||||
|
if self.name ~= nil then
|
||||||
|
self.spriteClone = self.animators:clone(self.name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:activateCallbacks()
|
||||||
|
if (self.spriteClone ~= nil) then
|
||||||
|
self.spriteClone:setCallbackTarget(self.owner)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:isCloned()
|
||||||
|
return (self.spriteClone ~= nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:getAnimator()
|
||||||
|
if (self:isCloned()) then
|
||||||
|
return self.spriteClone
|
||||||
|
else
|
||||||
|
return self.animators:get(self.name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:changeAnimation(animation, restart)
|
||||||
|
self:getAnimator():changeAnimation(animation, restart)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:setCustomSpeed(customSpeed)
|
||||||
|
self:getAnimator():setCustomSpeed(customSpeed)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:update(dt)
|
||||||
|
if (self.spriteClone ~= nil) then
|
||||||
|
self.spriteClone:update(dt)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:setScalling(scale)
|
||||||
|
self.scale = {x = scale.x or self.scale.x, y = scale.y or self.scale.y}
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:getCurrentAnimation()
|
||||||
|
return self:getAnimator():getCurrentAnimation()
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:getScalling()
|
||||||
|
return {x = self.scale.x, y = self.scale.y}
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:getFrame()
|
||||||
|
return self:getAnimator():getAbsoluteFrame()
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:getRelativeFrame()
|
||||||
|
return self:getAnimator().frame
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:getAnimationDuration()
|
||||||
|
return self:getAnimator():getAnimationDuration()
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:draw(position)
|
||||||
|
if (self.name == nil) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- TODO: use vector
|
||||||
|
local x, y = position.x + self.origin.x, position.y + self.origin.y
|
||||||
|
local sx, sy = self.scale.x, self.scale.y
|
||||||
|
|
||||||
|
if (self.spriteClone ~= nil) then
|
||||||
|
self.spriteClone:draw(x, y, 0, sx, sy)
|
||||||
|
else
|
||||||
|
self.animators:draw(self.name, x, y, 0, sx, sy)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return Sprite
|
Loading…
Add table
Reference in a new issue