24_Urho2DSprite.lua 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. -- Urho2D sprite example.
  2. -- This sample demonstrates:
  3. -- - Creating a 2D scene with sprite
  4. -- - Displaying the scene using the Renderer subsystem
  5. -- - Handling keyboard to move and zoom 2D camera
  6. -- - Usage of Lua Closure to update scene
  7. require "LuaScripts/Utilities/Sample"
  8. function Start()
  9. -- Execute the common startup for samples
  10. SampleStart()
  11. -- Create the scene content
  12. CreateScene()
  13. -- Create the UI content
  14. CreateInstructions()
  15. -- Setup the viewport for displaying the scene
  16. SetupViewport()
  17. -- Hook up to the frame update events
  18. SubscribeToEvents()
  19. end
  20. function CreateScene()
  21. scene_ = Scene()
  22. -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  23. -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
  24. -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  25. -- optimizing manner
  26. scene_:CreateComponent("Octree")
  27. -- Create a scene node for the camera, which we will move around
  28. -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  29. cameraNode = scene_:CreateChild("Camera")
  30. -- Set an initial position for the camera scene node above the plane
  31. cameraNode.position = Vector3(0.0, 0.0, -10.0)
  32. local camera = cameraNode:CreateComponent("Camera")
  33. camera.orthographic = true
  34. camera.orthoSize = graphics.height * PIXEL_SIZE
  35. local sprite = cache:GetResource("Sprite2D", "Urho2D/Aster.png")
  36. if sprite == nil then
  37. return
  38. end
  39. local spriteNodes = {}
  40. local NUM_SPRITES = 200
  41. local halfWidth = graphics.width * PIXEL_SIZE * 0.5
  42. local halfHeight = graphics.height * PIXEL_SIZE * 0.5
  43. for i = 1, NUM_SPRITES do
  44. local spriteNode = scene_:CreateChild("StaticSprite2D")
  45. spriteNode.position = Vector3(Random(-halfWidth, halfWidth), Random(-halfHeight, halfHeight), 0.0)
  46. local staticSprite = spriteNode:CreateComponent("StaticSprite2D")
  47. -- Set color
  48. staticSprite.color = Color(Random(1.0), Random(1.0), Random(1.0), 1.0)
  49. -- Set blend mode
  50. staticSprite.blendMode = BLEND_ALPHA
  51. -- Set sprite
  52. staticSprite.sprite = sprite
  53. -- Set move speed
  54. spriteNode.moveSpeed = Vector3(Random(-2.0, 2.0), Random(-2.0, 2.0), 0.0)
  55. -- Set rotate speed
  56. spriteNode.rotateSpeed = Random(-90.0, 90.0)
  57. table.insert(spriteNodes, spriteNode)
  58. end
  59. scene_.Update = function(self, timeStep)
  60. for _, spriteNode in ipairs(spriteNodes) do
  61. local position = spriteNode.position
  62. local moveSpeed = spriteNode.moveSpeed
  63. local newPosition = position + moveSpeed * timeStep
  64. if newPosition.x < -halfWidth or newPosition.x > halfWidth then
  65. newPosition.x = position.x
  66. moveSpeed.x = -moveSpeed.x
  67. end
  68. if newPosition.y < -halfHeight or newPosition.y > halfHeight then
  69. newPosition.y = position.y
  70. moveSpeed.y = -moveSpeed.y
  71. end
  72. spriteNode.position = newPosition
  73. spriteNode:Roll(spriteNode.rotateSpeed * timeStep)
  74. end
  75. end
  76. local animationSet = cache:GetResource("AnimationSet2D", "Urho2D/GoldIcon.scml")
  77. if animationSet == nil then
  78. return
  79. end
  80. local spriteNode = scene_:CreateChild("AnimatedSprite2D")
  81. spriteNode.position = Vector3(0.0, 0.0, -1.0)
  82. local animatedSprite = spriteNode:CreateComponent("AnimatedSprite2D")
  83. -- Set animation
  84. animatedSprite:SetAnimation(animationSet, "idle")
  85. end
  86. function CreateInstructions()
  87. -- Construct new Text object, set string to display and font to use
  88. local instructionText = ui.root:CreateChild("Text")
  89. instructionText:SetText("Use WASD keys and mouse to move, Use PageUp PageDown to zoom.")
  90. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  91. -- Position the text relative to the screen center
  92. instructionText.horizontalAlignment = HA_CENTER
  93. instructionText.verticalAlignment = VA_CENTER
  94. instructionText:SetPosition(0, ui.root.height / 4)
  95. end
  96. function SetupViewport()
  97. -- 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
  98. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  99. -- use, but now we just use full screen and default render path configured in the engine command line options
  100. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  101. renderer:SetViewport(0, viewport)
  102. end
  103. function MoveCamera(timeStep)
  104. -- Do not move if the UI has a focused element (the console)
  105. if ui.focusElement ~= nil then
  106. return
  107. end
  108. -- Movement speed as world units per second
  109. local MOVE_SPEED = 4.0
  110. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  111. if input:GetKeyDown(KEY_W) then
  112. cameraNode:Translate(Vector3(0.0, 1.0, 0.0) * MOVE_SPEED * timeStep)
  113. end
  114. if input:GetKeyDown(KEY_S) then
  115. cameraNode:Translate(Vector3(0.0, -1.0, 0.0) * MOVE_SPEED * timeStep)
  116. end
  117. if input:GetKeyDown(KEY_A) then
  118. cameraNode:Translate(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  119. end
  120. if input:GetKeyDown(KEY_D) then
  121. cameraNode:Translate(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  122. end
  123. if input:GetKeyDown(KEY_PAGEUP) then
  124. local camera = cameraNode:GetComponent("Camera")
  125. camera.zoom = camera.zoom * 1.01
  126. end
  127. if input:GetKeyDown(KEY_PAGEDOWN) then
  128. local camera = cameraNode:GetComponent("Camera")
  129. camera.zoom = camera.zoom * 0.99
  130. end
  131. end
  132. function SubscribeToEvents()
  133. -- Subscribe HandleUpdate() function for processing update events
  134. SubscribeToEvent("Update", "HandleUpdate")
  135. -- Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  136. UnsubscribeFromEvent("SceneUpdate")
  137. end
  138. function HandleUpdate(eventType, eventData)
  139. -- Take the frame time step, which is stored as a float
  140. local timeStep = eventData:GetFloat("TimeStep")
  141. -- Move the camera, scale movement with time step
  142. MoveCamera(timeStep)
  143. -- Update scene
  144. scene_:Update(timeStep)
  145. end
  146. -- Create XML patch instructions for screen joystick layout specific to this sample app
  147. function GetScreenJoystickPatchString()
  148. return
  149. "<patch>" ..
  150. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" ..
  151. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" ..
  152. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" ..
  153. " <element type=\"Text\">" ..
  154. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  155. " <attribute name=\"Text\" value=\"PAGEUP\" />" ..
  156. " </element>" ..
  157. " </add>" ..
  158. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" ..
  159. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" ..
  160. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" ..
  161. " <element type=\"Text\">" ..
  162. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  163. " <attribute name=\"Text\" value=\"PAGEDOWN\" />" ..
  164. " </element>" ..
  165. " </add>" ..
  166. "</patch>"
  167. end