24_Urho2DSprite.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. local scene_ = nil
  9. local cameraNode = nil
  10. function Start()
  11. -- Execute the common startup for samples
  12. SampleStart()
  13. -- Create the scene content
  14. CreateScene()
  15. -- Create the UI content
  16. CreateInstructions()
  17. -- Setup the viewport for displaying the scene
  18. SetupViewport()
  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. local width = graphics.width
  37. local height = graphics.height
  38. camera:SetOrthoSize(Vector2(width, height))
  39. local sprite = cache:GetResource("Sprite2D", "Urho2D/Aster.png")
  40. if sprite == nil then
  41. return
  42. end
  43. local spriteNodes = {}
  44. local NUM_SPRITES = 200
  45. local halfWidth = width * 0.5
  46. local halfHeight = height * 0.5
  47. for i = 1, NUM_SPRITES do
  48. local spriteNode = scene_:CreateChild("StaticSprite2D")
  49. spriteNode.position = Vector3(Random(-halfWidth, halfWidth), Random(-halfHeight, halfHeight), 0.0)
  50. local staticSprite = spriteNode:CreateComponent("StaticSprite2D")
  51. -- Set color
  52. staticSprite.color = Color(Random(1.0), Random(1.0), Random(1.0), 1.0)
  53. -- Set blend mode
  54. staticSprite.blendMode = BLEND_ALPHA
  55. -- Set sprite
  56. staticSprite.sprite = sprite
  57. -- Set move speed
  58. spriteNode.moveSpeed = Vector3(Random(-200.0, 200.0), Random(-200.0, 200.0), 0.0)
  59. -- Set rotate speed
  60. spriteNode.rotateSpeed = Random(-90.0, 90.0)
  61. table.insert(spriteNodes, spriteNode)
  62. end
  63. scene_.Update = function(self, timeStep)
  64. for _, spriteNode in ipairs(spriteNodes) do
  65. local position = spriteNode.position
  66. local moveSpeed = spriteNode.moveSpeed
  67. local newPosition = position + moveSpeed * timeStep
  68. if newPosition.x < -halfWidth or newPosition.x > halfWidth then
  69. newPosition.x = position.x
  70. moveSpeed.x = -moveSpeed.x
  71. end
  72. if newPosition.y < -halfHeight or newPosition.y > halfHeight then
  73. newPosition.y = position.y
  74. moveSpeed.y = -moveSpeed.y
  75. end
  76. spriteNode.position = newPosition
  77. spriteNode:Roll(spriteNode.rotateSpeed * timeStep)
  78. end
  79. end
  80. local animation = cache:GetResource("Animation2D", "Urho2D/GoldIcon.anm")
  81. if animation == nil then
  82. return
  83. end
  84. local spriteNode = scene_:CreateChild("AnimatedSprite2D")
  85. spriteNode.position = Vector3(0.0, 0.0, -1.0)
  86. local animatedSprite = spriteNode:CreateComponent("AnimatedSprite2D")
  87. -- Set animation
  88. animatedSprite.animation = animation
  89. -- Set blend mode
  90. animatedSprite.blendMode = BLEND_ALPHA
  91. end
  92. function CreateInstructions()
  93. -- Construct new Text object, set string to display and font to use
  94. local instructionText = ui.root:CreateChild("Text")
  95. instructionText:SetText("Use WASD keys and mouse to move, Use PageUp PageDown to zoom.")
  96. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  97. -- Position the text relative to the screen center
  98. instructionText.horizontalAlignment = HA_CENTER
  99. instructionText.verticalAlignment = VA_CENTER
  100. instructionText:SetPosition(0, ui.root.height / 4)
  101. end
  102. function SetupViewport()
  103. -- 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
  104. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  105. -- use, but now we just use full screen and default render path configured in the engine command line options
  106. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  107. renderer:SetViewport(0, viewport)
  108. end
  109. function MoveCamera(timeStep)
  110. -- Do not move if the UI has a focused element (the console)
  111. if ui.focusElement ~= nil then
  112. return
  113. end
  114. -- Movement speed as world units per second
  115. local MOVE_SPEED = 400.0
  116. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  117. -- Use the TranslateRelative() function to move relative to the node's orientation. Alternatively we could
  118. -- multiply the desired direction with the node's orientation quaternion, and use just Translate()
  119. if input:GetKeyDown(KEY_W) then
  120. cameraNode:TranslateRelative(Vector3.UP * MOVE_SPEED * timeStep)
  121. end
  122. if input:GetKeyDown(KEY_S) then
  123. cameraNode:TranslateRelative(Vector3.DOWN * MOVE_SPEED * timeStep)
  124. end
  125. if input:GetKeyDown(KEY_A) then
  126. cameraNode:TranslateRelative(Vector3.LEFT * MOVE_SPEED * timeStep)
  127. end
  128. if input:GetKeyDown(KEY_D) then
  129. cameraNode:TranslateRelative(Vector3.RIGHT * MOVE_SPEED * timeStep)
  130. end
  131. if input:GetKeyDown(KEY_PAGEUP) then
  132. local camera = cameraNode:GetComponent("Camera")
  133. camera.zoom = camera.zoom * 1.01
  134. end
  135. if input:GetKeyDown(KEY_PAGEDOWN) then
  136. local camera = cameraNode:GetComponent("Camera")
  137. camera.zoom = camera.zoom * 0.99
  138. end
  139. end
  140. function SubscribeToEvents()
  141. -- Subscribe HandleUpdate() function for processing update events
  142. SubscribeToEvent("Update", "HandleUpdate")
  143. end
  144. function HandleUpdate(eventType, eventData)
  145. -- Take the frame time step, which is stored as a float
  146. local timeStep = eventData:GetFloat("TimeStep")
  147. -- Move the camera, scale movement with time step
  148. MoveCamera(timeStep)
  149. -- Update scene
  150. scene_:Update(timeStep)
  151. end