|
|
@@ -1,84 +1,68 @@
|
|
|
--- Create an AIAgent for the box
|
|
|
-_modelNode:setAgent(AIAgent.create())
|
|
|
|
|
|
--- Store state machine for agent
|
|
|
-_stateMachine = _modelNode:getAgent():getStateMachine()
|
|
|
+local this = nil
|
|
|
+local animations = { }
|
|
|
|
|
|
--- Register AI states
|
|
|
-stateIdle = _stateMachine:addState("idle")
|
|
|
+function attached(node)
|
|
|
+ this = node
|
|
|
|
|
|
-stateSpinning = _stateMachine:addState("spinning")
|
|
|
-stateSpinning:addScriptCallback("update", "spinningUpdate")
|
|
|
+ -- Create an AIAgent for the box
|
|
|
+ node:setAgent(AIAgent.create())
|
|
|
|
|
|
-stateSpinning = _stateMachine:addState("sliding")
|
|
|
-stateSpinning:addScriptCallback("enter", "slidingEnter")
|
|
|
-stateSpinning:addScriptCallback("exit", "slidingExit")
|
|
|
+ -- Get state machine
|
|
|
+ local stateMachine = node:getAgent():getStateMachine()
|
|
|
|
|
|
-stateBouncing = _stateMachine:addState("bouncing")
|
|
|
-stateBouncing:addScriptCallback("enter", "bouncingEnter")
|
|
|
-stateBouncing:addScriptCallback("exit", "bouncingExit")
|
|
|
+ -- Register AI states
|
|
|
+ stateMachine:addState("idle")
|
|
|
+ stateMachine:addState("spin")
|
|
|
+ stateMachine:addState("slide")
|
|
|
+ stateMachine:addState("bounce")
|
|
|
+ stateMachine:addState("scale")
|
|
|
|
|
|
-stateScale = _stateMachine:addState("scale")
|
|
|
-stateScale:addScriptCallback("enter", "scaleEnter")
|
|
|
-stateScale:addScriptCallback("exit", "scaleExit")
|
|
|
+ -- Set initial state
|
|
|
+ stateMachine:setState("spin")
|
|
|
|
|
|
--- Set initial state
|
|
|
-_stateMachine:setState("spinning")
|
|
|
-
|
|
|
--- Create animations
|
|
|
-_slidingClip = _modelNode:createAnimation("sliding", Transform.ANIMATE_TRANSLATE(), 6, { 0, 250, 750, 1250, 1750, 2000 }, { 0,0,0, 2,0,0, 2,0,-4, -2,0,-4, -2,0,0, 0,0,0 }, Curve.LINEAR):getClip()
|
|
|
-_slidingClip:setRepeatCount(AnimationClip.REPEAT_INDEFINITE())
|
|
|
-_bouncingClip = _modelNode:createAnimation("bouncing", Transform.ANIMATE_TRANSLATE_Y(), 3, { 0, 500, 1000 }, { 0, 0.75, 0 }, Curve.CUBIC_IN_OUT):getClip()
|
|
|
-_bouncingClip:setRepeatCount(AnimationClip.REPEAT_INDEFINITE())
|
|
|
-_scaleClip = _modelNode:createAnimation("scale", Transform.ANIMATE_SCALE(), 3, { 0, 750, 1500 }, { 1,1,1, 2,2,2, 1,1,1 }, Curve.QUADRATIC_IN_OUT):getClip()
|
|
|
-_scaleClip:setRepeatCount(AnimationClip.REPEAT_INDEFINITE())
|
|
|
-
|
|
|
--- Called by game.lua to toggle AI state
|
|
|
-function toggleState()
|
|
|
- local state = _stateMachine:getActiveState():getId()
|
|
|
- if state == "spinning" then
|
|
|
- _stateMachine:setState("sliding")
|
|
|
- elseif state == "sliding" then
|
|
|
- _stateMachine:setState("bouncing")
|
|
|
- elseif state == "bouncing" then
|
|
|
- _stateMachine:setState("scale")
|
|
|
- elseif state == "scale" then
|
|
|
- _stateMachine:setState("idle")
|
|
|
- elseif state == "idle" then
|
|
|
- _stateMachine:setState("spinning")
|
|
|
- end
|
|
|
+ -- Create animations, storing them in a table keyed on state name
|
|
|
+ animations["slide"] = node:createAnimation("slide", Transform.ANIMATE_TRANSLATE(), 6, { 0, 250, 750, 1250, 1750, 2000 }, { 0,0,0, 2,0,0, 2,0,-4, -2,0,-4, -2,0,0, 0,0,0 }, Curve.LINEAR):getClip()
|
|
|
+ animations["slide"]:setRepeatCount(AnimationClip.REPEAT_INDEFINITE())
|
|
|
+ animations["bounce"] = node:createAnimation("bounce", Transform.ANIMATE_TRANSLATE_Y(), 3, { 0, 500, 1000 }, { 0, 0.75, 0 }, Curve.CUBIC_IN_OUT):getClip()
|
|
|
+ animations["bounce"]:setRepeatCount(AnimationClip.REPEAT_INDEFINITE())
|
|
|
+ animations["scale"] = node:createAnimation("scale", Transform.ANIMATE_SCALE(), 3, { 0, 750, 1500 }, { 1,1,1, 2,2,2, 1,1,1 }, Curve.QUADRATIC_IN_OUT):getClip()
|
|
|
+ animations["scale"]:setRepeatCount(AnimationClip.REPEAT_INDEFINITE())
|
|
|
end
|
|
|
|
|
|
--- SPINNING state handlers
|
|
|
-function spinningUpdate(agent, state, elapsedTime)
|
|
|
- _modelNode:rotateY(elapsedTime * math.rad(0.05))
|
|
|
-end
|
|
|
-
|
|
|
--- SLIDING state handlers
|
|
|
-function slidingEnter(agent, state)
|
|
|
- _slidingClip:play()
|
|
|
-end
|
|
|
-
|
|
|
-function slidingExit(agent, state)
|
|
|
- _slidingClip:pause()
|
|
|
-end
|
|
|
-
|
|
|
--- BOUNCING state handlers
|
|
|
-function bouncingEnter(agent, state)
|
|
|
- _bouncingClip:play()
|
|
|
+function stateEnter(node, state)
|
|
|
+ local clip = animations[state:getId()]
|
|
|
+ if clip then
|
|
|
+ clip:play()
|
|
|
+ end
|
|
|
end
|
|
|
|
|
|
-function bouncingExit(agent, state)
|
|
|
- _bouncingClip:pause()
|
|
|
+function stateExit(node, state)
|
|
|
+ local clip = animations[state:getId()]
|
|
|
+ if clip then
|
|
|
+ clip:pause()
|
|
|
+ end
|
|
|
end
|
|
|
|
|
|
--- SCALE state handlers
|
|
|
-function scaleEnter(agent, state)
|
|
|
- _scaleClip:play()
|
|
|
+function stateUpdate(node, state, t)
|
|
|
+ if state:getId() == "spin" then
|
|
|
+ node:rotateY(t * math.rad(0.05))
|
|
|
+ end
|
|
|
end
|
|
|
|
|
|
-function scaleExit(agent, state)
|
|
|
- _scaleClip:pause()
|
|
|
+-- Put into the global table so it can be called by game.lua to toggle AI state
|
|
|
+function _G.toggleState()
|
|
|
+ local stateMachine = this:getAgent():getStateMachine()
|
|
|
+ local state = stateMachine:getActiveState():getId()
|
|
|
+ if state == "spin" then
|
|
|
+ stateMachine:setState("slide")
|
|
|
+ elseif state == "slide" then
|
|
|
+ stateMachine:setState("bounce")
|
|
|
+ elseif state == "bounce" then
|
|
|
+ stateMachine:setState("scale")
|
|
|
+ elseif state == "scale" then
|
|
|
+ stateMachine:setState("idle")
|
|
|
+ elseif state == "idle" then
|
|
|
+ stateMachine:setState("spin")
|
|
|
+ end
|
|
|
end
|
|
|
-
|
|
|
-
|