33_Urho2DSpriterAnimation.lua 7.0 KB

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