03_Sprites.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. local cache = GetCache()
  12. local engine = GetEngine()
  13. local graphics = GetGraphics()
  14. local ui = GetUI()
  15. function Start()
  16. -- Execute the common startup for samples
  17. SampleStart()
  18. -- Create the sprites to the user interface
  19. CreateSprites();
  20. -- Hook up to the frame update events
  21. SubscribeToEvents();
  22. end
  23. function CreateSprites()
  24. local decalTex = cache:GetResource("Texture2D", "Textures/UrhoDecal.dds")
  25. local width = graphics.width
  26. local height = graphics.height
  27. for i = 1, numSprites do
  28. -- Create a new sprite, set it to use the texture
  29. local sprite = Sprite:new()
  30. sprite.texture = decalTex
  31. sprite:SetFullImageRect()
  32. -- The UI root element is as big as the rendering window, set random position within it
  33. sprite.position = Vector2(Random(width), Random(height))
  34. -- Set sprite size & hotspot in its center
  35. sprite:SetSize(128, 128)
  36. sprite.hotSpot = IntVector2(64, 64)
  37. -- Set random rotation in degrees and random scale
  38. sprite.rotation = Random(360.0)
  39. sprite.scale = Vector2(1.0, 1.0) * (Random(1.0) + 0.5)
  40. -- Set random color and additive blending mode
  41. sprite:SetColor(Color(Random(0.5) + 0.5, Random(0.5) + 0.5, Random(0.5) + 0.5, 1.0))
  42. sprite.blendMode = BLEND_ADD
  43. -- Add as a child of the root UI element
  44. ui.root:AddChild(sprite)
  45. -- Store sprite's velocity as a custom variable
  46. sprite:SetVar(VAR_VELOCITY, Variant(Vector2(Random(200.0) - 100.0, Random(200.0) - 100.0)))
  47. table.insert(sprites, sprite)
  48. end
  49. end
  50. function SubscribeToEvents()
  51. -- Subscribe HandleUpdate() function for processing update events
  52. SubscribeToEvent("Update", "HandleUpdate")
  53. end
  54. function MoveSprites(timeStep)
  55. local width = graphics.width
  56. local height = graphics.height
  57. for i = 1, numSprites do
  58. local sprite = sprites[i]
  59. sprite.rotation = sprite.rotation + timeStep * 30
  60. local newPos = sprite.position
  61. newPos = newPos + sprite:GetVar(VAR_VELOCITY):GetVector2() * timeStep
  62. if newPos.x >= width then
  63. newPos.x = newPos.x - width
  64. elseif newPos.x < 0 then
  65. newPos.x = newPos.x + width
  66. end
  67. if newPos.y >= height then
  68. newPos.y = newPos.y - height
  69. elseif newPos.y < 0 then
  70. newPos.y = newPos.y + height
  71. end
  72. sprite.position = newPos
  73. end
  74. end
  75. function HandleUpdate(eventType, eventData)
  76. local timeStep = eventData:GetFloat("TimeStep")
  77. MoveSprites(timeStep)
  78. end