33_Urho2DSpriterAnimation.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. -- Urho2D sprite example.
  2. -- This sample demonstrates:
  3. -- - Creating a 2D scene with spriter animation
  4. -- - Displaying the scene using the Renderer subsystem
  5. -- - Handling keyboard to move and zoom 2D camera
  6. require "LuaScripts/Utilities/Sample"
  7. local spriterNode = nil
  8. local spriterAnimationIndex = 0
  9. function Start()
  10. -- Execute the common startup for samples
  11. SampleStart()
  12. -- Create the scene content
  13. CreateScene()
  14. -- Create the UI content
  15. CreateInstructions()
  16. -- Setup the viewport for displaying the scene
  17. SetupViewport()
  18. -- Set the mouse mode to use in the sample
  19. SampleInitMouseMode(MM_FREE)
  20. -- Hook up to the frame update events
  21. SubscribeToEvents()
  22. end
  23. function CreateScene()
  24. scene_ = Scene()
  25. -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  26. -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
  27. -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  28. -- optimizing manner
  29. scene_:CreateComponent("Octree")
  30. -- Create a scene node for the camera, which we will move around
  31. -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  32. cameraNode = scene_:CreateChild("Camera")
  33. -- Set an initial position for the camera scene node above the plane
  34. cameraNode.position = Vector3(0.0, 0.0, -10.0)
  35. local camera = cameraNode:CreateComponent("Camera")
  36. camera.orthographic = true
  37. camera.orthoSize = graphics.height * PIXEL_SIZE
  38. camera.zoom = 1.5 * Min(graphics.width / 1280, graphics.height / 800) -- Set zoom according to user's resolution to ensure full visibility (initial zoom (1.5) is set for full visibility at 1280x800 resolution)
  39. local spriterAnimationSet = cache:GetResource("AnimationSet2D", "Urho2D/imp/imp.scml")
  40. if spriterAnimationSet == nil then
  41. return
  42. end
  43. spriterNode = scene_:CreateChild("SpriterAnimation")
  44. local spriterAnimatedSprite = spriterNode:CreateComponent("AnimatedSprite2D")
  45. spriterAnimatedSprite.animationSet = spriterAnimationSet
  46. spriterAnimatedSprite:SetAnimation(spriterAnimationSet:GetAnimation(spriterAnimationIndex), LM_FORCE_LOOPED)
  47. end
  48. function CreateInstructions()
  49. -- Construct new Text object, set string to display and font to use
  50. local instructionText = ui.root:CreateChild("Text")
  51. instructionText:SetText("Mouse click to play next animation, \nUse WASD keys and mouse to move, Use PageUp PageDown to zoom.")
  52. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  53. instructionText.textAlignment = HA_CENTER -- Center rows in relation to each other
  54. -- Position the text relative to the screen center
  55. instructionText.horizontalAlignment = HA_CENTER
  56. instructionText.verticalAlignment = VA_CENTER
  57. instructionText:SetPosition(0, ui.root.height / 4)
  58. end
  59. function SetupViewport()
  60. -- Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
  61. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  62. -- use, but now we just use full screen and default render path configured in the engine command line options
  63. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  64. renderer:SetViewport(0, viewport)
  65. end
  66. function MoveCamera(timeStep)
  67. -- Do not move if the UI has a focused element (the console)
  68. if ui.focusElement ~= nil then
  69. return
  70. end
  71. -- Movement speed as world units per second
  72. local MOVE_SPEED = 4.0
  73. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  74. if input:GetKeyDown(KEY_W) then
  75. cameraNode:Translate(Vector3(0.0, 1.0, 0.0) * MOVE_SPEED * timeStep)
  76. end
  77. if input:GetKeyDown(KEY_S) then
  78. cameraNode:Translate(Vector3(0.0, -1.0, 0.0) * MOVE_SPEED * timeStep)
  79. end
  80. if input:GetKeyDown(KEY_A) then
  81. cameraNode:Translate(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  82. end
  83. if input:GetKeyDown(KEY_D) then
  84. cameraNode:Translate(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  85. end
  86. if input:GetKeyDown(KEY_PAGEUP) then
  87. local camera = cameraNode:GetComponent("Camera")
  88. camera.zoom = camera.zoom * 1.01
  89. end
  90. if input:GetKeyDown(KEY_PAGEDOWN) then
  91. local camera = cameraNode:GetComponent("Camera")
  92. camera.zoom = camera.zoom * 0.99
  93. end
  94. end
  95. function SubscribeToEvents()
  96. -- Subscribe HandleUpdate() function for processing update events
  97. SubscribeToEvent("Update", "HandleUpdate")
  98. SubscribeToEvent("MouseButtonDown", "HandleMouseButtonDown")
  99. -- Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  100. UnsubscribeFromEvent("SceneUpdate")
  101. end
  102. function HandleUpdate(eventType, eventData)
  103. -- Take the frame time step, which is stored as a float
  104. local timeStep = eventData["TimeStep"]:GetFloat()
  105. -- Move the camera, scale movement with time step
  106. MoveCamera(timeStep)
  107. end
  108. function HandleMouseButtonDown(eventType, eventData)
  109. local spriterAnimatedSprite = spriterNode:GetComponent("AnimatedSprite2D")
  110. local spriterAnimationSet = spriterAnimatedSprite.animationSet
  111. spriterAnimationIndex = (spriterAnimationIndex + 1) % spriterAnimationSet.numAnimations
  112. spriterAnimatedSprite:SetAnimation(spriterAnimationSet:GetAnimation(spriterAnimationIndex), LM_FORCE_LOOPED)
  113. end
  114. -- Create XML patch instructions for screen joystick layout specific to this sample app
  115. function GetScreenJoystickPatchString()
  116. return
  117. "<patch>" ..
  118. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" ..
  119. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" ..
  120. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" ..
  121. " <element type=\"Text\">" ..
  122. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  123. " <attribute name=\"Text\" value=\"PAGEUP\" />" ..
  124. " </element>" ..
  125. " </add>" ..
  126. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" ..
  127. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" ..
  128. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" ..
  129. " <element type=\"Text\">" ..
  130. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  131. " <attribute name=\"Text\" value=\"PAGEDOWN\" />" ..
  132. " </element>" ..
  133. " </add>" ..
  134. "</patch>"
  135. end