03_Sprites.lua 3.2 KB

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