ai.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. -- Create an AIAgent for the box
  2. _modelNode:setAgent(AIAgent.create())
  3. -- Store state machine for agent
  4. _stateMachine = _modelNode:getAgent():getStateMachine()
  5. -- Register AI states
  6. stateIdle = _stateMachine:addState("idle")
  7. stateSpinning = _stateMachine:addState("spinning")
  8. stateSpinning:addScriptCallback("update", "spinningUpdate")
  9. stateSpinning = _stateMachine:addState("sliding")
  10. stateSpinning:addScriptCallback("enter", "slidingEnter")
  11. stateSpinning:addScriptCallback("exit", "slidingExit")
  12. stateBouncing = _stateMachine:addState("bouncing")
  13. stateBouncing:addScriptCallback("enter", "bouncingEnter")
  14. stateBouncing:addScriptCallback("exit", "bouncingExit")
  15. stateScale = _stateMachine:addState("scale")
  16. stateScale:addScriptCallback("enter", "scaleEnter")
  17. stateScale:addScriptCallback("exit", "scaleExit")
  18. -- Set initial state
  19. _stateMachine:setState("spinning")
  20. -- Create animations
  21. _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()
  22. _slidingClip:setRepeatCount(AnimationClip.REPEAT_INDEFINITE())
  23. _bouncingClip = _modelNode:createAnimation("bouncing", Transform.ANIMATE_TRANSLATE_Y(), 3, { 0, 500, 1000 }, { 0, 0.75, 0 }, Curve.CUBIC_IN_OUT):getClip()
  24. _bouncingClip:setRepeatCount(AnimationClip.REPEAT_INDEFINITE())
  25. _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()
  26. _scaleClip:setRepeatCount(AnimationClip.REPEAT_INDEFINITE())
  27. -- Called by game.lua to toggle AI state
  28. function toggleState()
  29. local state = _stateMachine:getActiveState():getId()
  30. if state == "spinning" then
  31. _stateMachine:setState("sliding")
  32. elseif state == "sliding" then
  33. _stateMachine:setState("bouncing")
  34. elseif state == "bouncing" then
  35. _stateMachine:setState("scale")
  36. elseif state == "scale" then
  37. _stateMachine:setState("idle")
  38. elseif state == "idle" then
  39. _stateMachine:setState("spinning")
  40. end
  41. end
  42. -- SPINNING state handlers
  43. function spinningUpdate(agent, state, elapsedTime)
  44. _modelNode:rotateY(elapsedTime * math.rad(0.05))
  45. end
  46. -- SLIDING state handlers
  47. function slidingEnter(agent, state)
  48. _slidingClip:play()
  49. end
  50. function slidingExit(agent, state)
  51. _slidingClip:pause()
  52. end
  53. -- BOUNCING state handlers
  54. function bouncingEnter(agent, state)
  55. _bouncingClip:play()
  56. end
  57. function bouncingExit(agent, state)
  58. _bouncingClip:pause()
  59. end
  60. -- SCALE state handlers
  61. function scaleEnter(agent, state)
  62. _scaleClip:play()
  63. end
  64. function scaleExit(agent, state)
  65. _scaleClip:pause()
  66. end