03_Sprites.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. -- Hook up to the frame update events
  17. SubscribeToEvents()
  18. end
  19. function CreateSprites()
  20. local decalTex = cache:GetResource("Texture2D", "Textures/UrhoDecal.dds")
  21. local width = graphics.width
  22. local height = graphics.height
  23. for i = 1, numSprites do
  24. -- Create a new sprite, set it to use the texture
  25. local sprite = Sprite:new()
  26. sprite.texture = decalTex
  27. sprite:SetFullImageRect()
  28. -- The UI root element is as big as the rendering window, set random position within it
  29. sprite.position = Vector2(Random(width), Random(height))
  30. -- Set sprite size & hotspot in its center
  31. sprite:SetSize(128, 128)
  32. sprite.hotSpot = IntVector2(64, 64)
  33. -- Set random rotation in degrees and random scale
  34. sprite.rotation = Random(360.0)
  35. sprite.scale = Vector2(1.0, 1.0) * (Random(1.0) + 0.5)
  36. -- Set random color and additive blending mode
  37. sprite:SetColor(Color(Random(0.5) + 0.5, Random(0.5) + 0.5, Random(0.5) + 0.5, 1.0))
  38. sprite.blendMode = BLEND_ADD
  39. -- Add as a child of the root UI element
  40. ui.root:AddChild(sprite)
  41. -- Store sprite's velocity as a custom variable
  42. sprite:SetVar(VAR_VELOCITY, Variant(Vector2(Random(200.0) - 100.0, Random(200.0) - 100.0)))
  43. table.insert(sprites, sprite)
  44. end
  45. end
  46. function SubscribeToEvents()
  47. -- Subscribe HandleUpdate() function for processing update events
  48. SubscribeToEvent("Update", "HandleUpdate")
  49. end
  50. function MoveSprites(timeStep)
  51. local width = graphics.width
  52. local height = graphics.height
  53. for i = 1, numSprites do
  54. local sprite = sprites[i]
  55. sprite.rotation = sprite.rotation + timeStep * 30
  56. local newPos = sprite.position
  57. newPos = newPos + sprite:GetVar(VAR_VELOCITY):GetVector2() * timeStep
  58. if newPos.x >= width then
  59. newPos.x = newPos.x - width
  60. elseif newPos.x < 0 then
  61. newPos.x = newPos.x + width
  62. end
  63. if newPos.y >= height then
  64. newPos.y = newPos.y - height
  65. elseif newPos.y < 0 then
  66. newPos.y = newPos.y + height
  67. end
  68. sprite.position = newPos
  69. end
  70. end
  71. function HandleUpdate(eventType, eventData)
  72. local timeStep = eventData:GetFloat("TimeStep")
  73. MoveSprites(timeStep)
  74. end
  75. -- Create XML patch instructions for screen joystick layout specific to this sample app
  76. function GetScreenJoystickPatchString()
  77. return
  78. "<patch>" ..
  79. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" ..
  80. " <attribute name=\"Is Visible\" value=\"false\" />" ..
  81. " </add>" ..
  82. "</patch>"
  83. end