Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added collisionFramesEvent and sfxFramesEvent #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions AnimatedSprite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ function AnimatedSprite:init(imagetable, states, animate)
nextAnimation = nil,

onFrameChangedEvent = emptyFunc,
onFrameCollisionEvent = emptyFunc,
onFrameSfxEvent = emptyFunc,
onStateChangedEvent = emptyFunc,
onLoopFinishedEvent = emptyFunc,
onAnimationEndEvent = emptyFunc
Expand Down Expand Up @@ -169,6 +171,12 @@ local function addState(self, params)
params.firstFrameIndex = 1
params.framesCount = #params.frames
end
if (params.collisionFrames ~= nil) then
state["collisionFrames"] = params.collisionFrames -- Frames when the collision callback is called
end
if (params.sfxFrames ~= nil) then
state["sfxFrames"] = params.sfxFrames -- Frames when the sfx callback is called
end
if (type(params.firstFrameIndex) == "string") then
local thatState = self.states[params.firstFrameIndex]
state["firstFrameIndex"] = thatState.firstFrameIndex + thatState.framesCount
Expand All @@ -191,6 +199,8 @@ local function addState(self, params)
state["xScale"] = params.xScale -- Optional scale for horizontal axis
state["yScale"] = params.yScale -- Optional scale for vertical axis

state["onFrameCollisionEvent"] = params.onFrameCollisionEvent -- Event that will be raised when animation moves to the next frame
state["onFrameSfxEvent"] = params.onFrameSfxEvent -- Event that will be raised when animation moves to the next frame
state["onFrameChangedEvent"] = params.onFrameChangedEvent -- Event that will be raised when animation moves to the next frame
state["onStateChangedEvent"] = params.onStateChangedEvent -- Event that will be raised when animation state changes
state["onLoopFinishedEvent"] = params.onLoopFinishedEvent -- Event that will be raised when animation changes to the final frame
Expand Down Expand Up @@ -389,6 +399,31 @@ function AnimatedSprite:printAllStates()
printTable(self.states)
end

local function isInTable(t, valueToCheck)
for index, value in ipairs(t) do
if (value == valueToCheck) then
return true
end
end

return false
end


local function checkMarkedFrames(self, state, markedFrames, callback)
if (markedFrames == nil or state.frames == nil
or markedFrames == nil or callback == nil) then
return
end

local actualAnimationFrameNumber = state.frames[self._currentFrame]
local isMarkedFrame = isInTable(markedFrames, actualAnimationFrameNumber)

if (isMarkedFrame) then
callback(self)
end
end

---Procees the animation to the next step without redrawing the sprite
local function processAnimation(self)
local state = self.states[self.currentState]
Expand All @@ -397,6 +432,8 @@ local function processAnimation(self)
value += state.firstFrameIndex
self._currentFrame = value
state.onFrameChangedEvent(self)
checkMarkedFrames(self, state, state.collisionFrames, state.onFrameCollisionEvent)
checkMarkedFrames(self, state, state.sfxFrames, state.onFrameSfxEvent)
end

local reverse = state.reverse
Expand All @@ -410,9 +447,13 @@ local function processAnimation(self)
self._loopsFinished += 1
state.onFrameChangedEvent(self)
state.onLoopFinishedEvent(self)
checkMarkedFrames(self, state, state.collisionFrames, state.onFrameCollisionEvent)
checkMarkedFrames(self, state, state.sfxFrames, state.onFrameSfxEvent)
return
else
state.onFrameChangedEvent(self)
checkMarkedFrames(self, state, state.collisionFrames, state.onFrameCollisionEvent)
checkMarkedFrames(self, state, state.sfxFrames, state.onFrameSfxEvent)
end
setImage(self)
return
Expand All @@ -422,6 +463,8 @@ local function processAnimation(self)
self._loopsFinished += 1
state.onFrameChangedEvent(self)
state.onLoopFinishedEvent(self)
checkMarkedFrames(self, state, state.collisionFrames, state.onFrameCollisionEvent)
checkMarkedFrames(self, state, state.sfxFrames, state.onFrameSfxEvent)
return
end

Expand Down
4 changes: 3 additions & 1 deletion tests/unit-tests/source/assets/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
},
{
"name":"random",
"frames": [1, 2, 3, 7, 8, 13, 14, 15]
"frames": [1, 2, 3, 7, 8, 13, 14, 15],
"collisionFrames": [3, 7, 8],
"sfxFrames": [3]
}
]
46 changes: 44 additions & 2 deletions tests/unit-tests/source/tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ function TestInit:jsonTests()
yScale = 1,
nextAnimation = nil,
onFrameChangedEvent = emptyFunc,
onFrameCollisionEvent = emptyFunc,
onFrameSfxEvent = emptyFunc,
onStateChangedEvent = emptyFunc,
onLoopFinishedEvent = emptyFunc,
onAnimationEndEvent = emptyFunc
Expand Down Expand Up @@ -88,7 +90,9 @@ function TestInit:jsonTests()
animationStartingFrame = 1,
firstFrameIndex = 1,
frames = {1, 2, 3, 7, 8, 13, 14, 15},
framesCount = 8
framesCount = 8,
collisionFrames = {3, 7, 8},
sfxFrames = {3}
}
}

Expand Down Expand Up @@ -229,6 +233,10 @@ function TestInit:test10()
luaunit.assertEquals(#self.sprite.imagetable, #imagetable)
end

function TestInit:test11()
self.sprite = AnimatedSprite.new("assets/test")
end

function TestInit:tearDown()
self.sprite = nil
end
Expand Down Expand Up @@ -337,6 +345,36 @@ function TestAnimation:test5()
TestAnimation:proceedAnimation("random", 8, 1, 3)
end

function TestAnimation:test11()

self.sprite.onFrameCollisionEvent = function (aniSprite)
print("onFrameCollisionEvent " .. aniSprite._currentFrame)
luaunit.assertNotIsNil(aniSprite.currentstate.collisionFrames)
end

self.sprite.onFrameSfxEvent = function (aniSprite)
print("onFrameSfxEvent " .. aniSprite._currentFrame)
luaunit.assertNotIsNil(aniSprite.currentstate.sfxFrames)
end

self.sprite:changeState("random", true)

luaunit.assertNotIsNil(self.sprite.states[self.sprite.currentState].collisionFrames)
luaunit.assertNotIsNil(self.sprite.states[self.sprite.currentState].sfxFrames)


--[1, 2, 3, 7, 8, 13, 14, 15]
-- TestAnimation:proceedAnimation("random", 1, 0, 3)
-- TestAnimation:proceedAnimation("random", 2, 0, 3)
-- TestAnimation:proceedAnimation("random", 3, 0, 3)
-- TestAnimation:proceedAnimation("random", 7, 0, 3)
-- TestAnimation:proceedAnimation("random", 8, 0, 3)
-- TestAnimation:proceedAnimation("random", 13, 0, 3)
-- TestAnimation:proceedAnimation("random", 14, 0, 3)
-- TestAnimation:proceedAnimation("random", 15, 1, 3)
end


function TestAnimation:tearDown()
self.sprite = nil
end
Expand All @@ -351,11 +389,15 @@ function TestEvents:setUp()
loop = 2,
nextAnimation = "second",
onFrameChangedEvent = function() output = output.."F" end,
onFrameCollisionEvent = function() output = output.."C" end,
onFrameSfxEvent = function() output = output.."X" end,
onStateChangedEvent = function() output = output.."S" end,
onLoopFinishedEvent = function() output = output.."L" end,
onAnimationEndEvent = function() output = output.."A" end})
self.sprite:addState("second", 5, 4, {
onFrameChangedEvent = function() output = output.."f" end,
onFrameCollisionEvent = function() output = output.."c" end,
onFrameSfxEvent = function() output = output.."x" end,
onStateChangedEvent = function() output = output.."s" end,
onLoopFinishedEvent = function() output = output.."l" end,
onAnimationEndEvent = function() output = output.."a" end})
Expand All @@ -374,5 +416,5 @@ function TestEvents:test1()
end

function TestEvents:tearDown()
self.sprite = nil
self.sprite = nil
end