noble animations
This commit is contained in:
@@ -49,7 +49,6 @@ Noble.Animation = {}
|
||||
-- end
|
||||
-- @see NobleSprite:init
|
||||
function Noble.Animation.new(__view)
|
||||
|
||||
local animation = {}
|
||||
|
||||
--- Properties
|
||||
@@ -117,11 +116,10 @@ function Noble.Animation.new(__view)
|
||||
-- animation.["jump"].next -- "float"
|
||||
-- animation.idle.next -- nil
|
||||
function animation:addState(__name, __startFrame, __endFrame, __next, __loop, __onComplete, __frameDuration)
|
||||
|
||||
local loop = true
|
||||
local frameDuration = 1
|
||||
if (__loop ~= nil) then loop = __loop end
|
||||
if(__frameDuration ~= nil) then frameDuration = __frameDuration end
|
||||
if (__frameDuration ~= nil) then frameDuration = __frameDuration end
|
||||
self[__name] = {
|
||||
name = __name,
|
||||
startFrame = __startFrame,
|
||||
@@ -140,7 +138,6 @@ function Noble.Animation.new(__view)
|
||||
self.currentName = __name
|
||||
self.frameDuration = frameDuration
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
--- Methods
|
||||
@@ -180,7 +177,6 @@ function Noble.Animation.new(__view)
|
||||
-- groundedLastFrame = grounded
|
||||
-- end
|
||||
function animation:setState(__animationState, __continuous, __unlessThisState)
|
||||
|
||||
if (__unlessThisState ~= nil) then
|
||||
if (type(__unlessThisState) == "string") then
|
||||
if (self.currentName == __unlessThisState) then return end
|
||||
@@ -228,25 +224,26 @@ function Noble.Animation.new(__view)
|
||||
-- animation:draw(100,100)
|
||||
-- end
|
||||
function animation:draw(__x, __y, __advance)
|
||||
|
||||
--print(self.currentName .. " > " .. self.currentFrame .. " >> " .. tostring(self.current.loop))
|
||||
|
||||
if (__advance == nil) then __advance = true end
|
||||
|
||||
if(self.currentFrame < self.current.startFrame or self.currentFrame > self.current.endFrame + 1) then
|
||||
self.currentFrame = self.current.startFrame -- Error correction.
|
||||
elseif(self.currentFrame == self.current.endFrame + 1) then -- End frame behavior.
|
||||
if (self.currentFrame < self.current.startFrame or self.currentFrame > self.current.endFrame + 1) then
|
||||
self.currentFrame = self.current.startFrame -- Error correction.
|
||||
elseif (self.currentFrame == self.current.endFrame + 1) then -- End frame behavior.
|
||||
if (self.current.next ~= nil) then
|
||||
self.currentFrame = self.current.next.startFrame -- Set to first frame of next animation.
|
||||
self.frameDurationCount = 1 -- Reset ticks.
|
||||
self.currentFrame = self.current.next.startFrame -- Set to first frame of next animation.
|
||||
self.frameDurationCount = 1 -- Reset ticks.
|
||||
self.previousFrameDurationCount = self.frameDuration
|
||||
self:setState(self.current.next) -- Set next animation state.
|
||||
self:setState(self.current.next) -- Set next animation state.
|
||||
elseif (self.current.loop == true) then
|
||||
self.currentFrame = self.current.startFrame -- Loop animation state. (TO-DO: account for continuous somehow?)
|
||||
self.frameDurationCount = 1 -- Reset ticks.
|
||||
self.currentFrame = self.current
|
||||
.startFrame -- Loop animation state. (TO-DO: account for continuous somehow?)
|
||||
self.frameDurationCount = 1 -- Reset ticks.
|
||||
self.previousFrameDurationCount = self.frameDuration
|
||||
elseif(__advance) then
|
||||
self.currentFrame = self.currentFrame - 1 -- Undo advance frame because we want to draw the same frame again.
|
||||
elseif (__advance) then
|
||||
self.currentFrame = self.currentFrame -
|
||||
1 -- Undo advance frame because we want to draw the same frame again.
|
||||
end
|
||||
|
||||
if (self.current.onComplete ~= nil) then
|
||||
@@ -260,7 +257,7 @@ function Noble.Animation.new(__view)
|
||||
|
||||
if (__advance == true) then
|
||||
self.frameDurationCount += 1
|
||||
if((self.frameDurationCount - self.previousFrameDurationCount) >= self.current.frameDuration) then
|
||||
if ((self.frameDurationCount - self.previousFrameDurationCount) >= self.current.frameDuration) then
|
||||
self.currentFrame = self.currentFrame + 1
|
||||
self.previousFrameDurationCount += self.current.frameDuration
|
||||
end
|
||||
|
@@ -56,9 +56,7 @@ class("NobleSprite").extends(Graphics.sprite)
|
||||
function NobleSprite:init(__view, __viewIsSpritesheet, __singleState, __singleStateLoop)
|
||||
NobleSprite.super.init(self)
|
||||
self.isNobleSprite = true -- This is important so other methods don't confuse this for a playdate.graphics.sprite. DO NOT modify this value at runtime.
|
||||
|
||||
if (__view ~= nil) then
|
||||
|
||||
-- __view is the path to an image or spritesheet file.
|
||||
if (type(__view) == "string") then
|
||||
self.animated = __viewIsSpritesheet -- NO NOT modify self.animated at runtime.
|
||||
@@ -76,25 +74,22 @@ function NobleSprite:init(__view, __viewIsSpritesheet, __singleState, __singleSt
|
||||
if (__singleState == true) then
|
||||
self.animation:addState("default", 1, self.animation.imageTable:getLength(), nil, singleStateLoop)
|
||||
end
|
||||
|
||||
else
|
||||
-- Create a new Graphics.image object.
|
||||
self:setImage(Graphics.image.new(__view))
|
||||
end
|
||||
|
||||
-- __view is an existing Graphics.image object.
|
||||
-- __view is an existing Graphics.image object.
|
||||
elseif (type(__view) == "userdata") then
|
||||
self.animated = false
|
||||
self:setImage(__view)
|
||||
|
||||
-- __view is an existing Noble.Animation object.
|
||||
-- __view is an existing Noble.Animation object.
|
||||
elseif (type(__view) == "table") then
|
||||
self.animated = true
|
||||
self.animation = __view
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function NobleSprite:draw()
|
||||
@@ -144,11 +139,11 @@ end
|
||||
function NobleSprite:remove()
|
||||
if (self.animation ~= nil) then
|
||||
self:stop()
|
||||
self:setUpdatesEnabled(true) -- reset!
|
||||
self:setUpdatesEnabled(true) -- reset!
|
||||
end
|
||||
Noble.currentScene():removeSprite(self)
|
||||
end
|
||||
|
||||
function NobleSprite:superRemove()
|
||||
NobleSprite.super.remove(self)
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user