03_Sprites.lua 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 = ShortStringHash("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