24_Urho2DSprite.lua 8.2 KB

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