03_Sprites.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. -- Moving sprites example.
  2. -- This sample demonstrates:
  3. -- - Adding Sprite elements to the UI
  4. -- - Storing custom data (sprite velocity) inside UI elements
  5. -- - Handling frame update events in which the sprites are moved
  6. require "LuaScripts/Utilities/Sample"
  7. local numSprites = 100
  8. local sprites = {}
  9. -- Custom variable identifier for storing sprite velocity within the UI element
  10. local VAR_VELOCITY = StringHash("Velocity")
  11. function Start()
  12. -- Execute the common startup for samples
  13. SampleStart()
  14. -- Create the sprites to the user interface
  15. CreateSprites()
  16. -- Set the mouse mode to use in the sample
  17. SampleInitMouseMode(MM_FREE)
  18. -- Hook up to the frame update events
  19. SubscribeToEvents()
  20. end
  21. function CreateSprites()
  22. local decalTex = cache:GetResource("Texture2D", "Textures/UrhoDecal.dds")
  23. local width = graphics.width
  24. local height = graphics.height
  25. for i = 1, numSprites do
  26. -- Create a new sprite, set it to use the texture
  27. local sprite = Sprite:new()
  28. sprite.texture = decalTex
  29. sprite:SetFullImageRect()
  30. -- The UI root element is as big as the rendering window, set random position within it
  31. sprite.position = Vector2(Random(width), Random(height))
  32. -- Set sprite size & hotspot in its center
  33. sprite:SetSize(128, 128)
  34. sprite.hotSpot = IntVector2(64, 64)
  35. -- Set random rotation in degrees and random scale
  36. sprite.rotation = Random(360.0)
  37. sprite.scale = Vector2(1.0, 1.0) * (Random(1.0) + 0.5)
  38. -- Set random color and additive blending mode
  39. sprite:SetColor(Color(Random(0.5) + 0.5, Random(0.5) + 0.5, Random(0.5) + 0.5, 1.0))
  40. sprite.blendMode = BLEND_ADD
  41. -- Add as a child of the root UI element
  42. ui.root:AddChild(sprite)
  43. -- Store sprite's velocity as a custom variable
  44. sprite:SetVar(VAR_VELOCITY, Variant(Vector2(Random(200.0) - 100.0, Random(200.0) - 100.0)))
  45. table.insert(sprites, sprite)
  46. end
  47. end
  48. function SubscribeToEvents()
  49. -- Subscribe HandleUpdate() function for processing update events
  50. SubscribeToEvent("Update", "HandleUpdate")
  51. end
  52. function MoveSprites(timeStep)
  53. local width = graphics.width
  54. local height = graphics.height
  55. for i = 1, numSprites do
  56. local sprite = sprites[i]
  57. sprite.rotation = sprite.rotation + timeStep * 30
  58. local newPos = sprite.position
  59. newPos = newPos + sprite:GetVar(VAR_VELOCITY):GetVector2() * timeStep
  60. if newPos.x >= width then
  61. newPos.x = newPos.x - width
  62. elseif newPos.x < 0 then
  63. newPos.x = newPos.x + width
  64. end
  65. if newPos.y >= height then
  66. newPos.y = newPos.y - height
  67. elseif newPos.y < 0 then
  68. newPos.y = newPos.y + height
  69. end
  70. sprite.position = newPos
  71. end
  72. end
  73. function HandleUpdate(eventType, eventData)
  74. local timeStep = eventData["TimeStep"]:GetFloat()
  75. MoveSprites(timeStep)
  76. end
  77. -- Create XML patch instructions for screen joystick layout specific to this sample app
  78. function GetScreenJoystickPatchString()
  79. return
  80. "<patch>" ..
  81. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" ..
  82. " <attribute name=\"Is Visible\" value=\"false\" />" ..
  83. " </add>" ..
  84. "</patch>"
  85. end