03_Sprites.as 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include "Scripts/Utilities/Sample.as"
  7. // Number of sprites to draw
  8. const uint NUM_SPRITES = 100;
  9. Array<Sprite@> sprites;
  10. void Start()
  11. {
  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. }
  19. void CreateSprites()
  20. {
  21. // Get rendering window size as floats
  22. float width = graphics.width;
  23. float height = graphics.height;
  24. // Get the Urho3D fish texture
  25. Texture2D@ decalTex = cache.GetResource("Texture2D", "Textures/UrhoDecal.dds");
  26. for (uint i = 0; i < NUM_SPRITES; ++i)
  27. {
  28. // Create a new sprite, set it to use the texture
  29. Sprite@ sprite = Sprite();
  30. sprite.texture = decalTex;
  31. // The UI root element is as big as the rendering window, set random position within it
  32. sprite.position = Vector2(Random() * width, Random() * height);
  33. // Set sprite size & hotspot in its center
  34. sprite.size = IntVector2(128, 128);
  35. sprite.hotSpot = IntVector2(64, 64);
  36. // Set random rotation in degrees and random scale
  37. sprite.rotation = Random() * 360.0f;
  38. sprite.SetScale(Random(1.0f) + 0.5f);
  39. // Set random color and additive blending mode
  40. sprite.color = Color(Random(0.5f) + 0.5f, Random(0.5f) + 0.5f, Random(0.5f) + 0.5f);
  41. sprite.blendMode = BLEND_ADD;
  42. // Add as a child of the root UI element
  43. ui.root.AddChild(sprite);
  44. // Store sprite's velocity as a custom variable
  45. sprite.vars["Velocity"] = Vector2(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f);
  46. // Store sprites to our own container for easy movement update iteration
  47. sprites.Push(sprite);
  48. }
  49. }
  50. void MoveSprites(float timeStep)
  51. {
  52. float width = graphics.width;
  53. float height = graphics.height;
  54. // Go through all sprites
  55. for (uint i = 0; i < sprites.length; ++i)
  56. {
  57. Sprite@ sprite = sprites[i];
  58. // Rotate
  59. float newRot = sprite.rotation + timeStep * 30.0f;
  60. sprite.rotation = newRot;
  61. // Move, wrap around rendering window edges
  62. Vector2 newPos = sprite.position + sprite.vars["Velocity"].GetVector2() * timeStep;
  63. if (newPos.x < 0.0f)
  64. newPos.x += width;
  65. if (newPos.x >= width)
  66. newPos.x -= width;
  67. if (newPos.y < 0.0f)
  68. newPos.y += height;
  69. if (newPos.y >= height)
  70. newPos.y -= height;
  71. sprite.position = newPos;
  72. }
  73. }
  74. void SubscribeToEvents()
  75. {
  76. // Subscribe HandleUpdate() function for processing update events
  77. SubscribeToEvent("Update", "HandleUpdate");
  78. }
  79. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  80. {
  81. // Take the frame time step, which is stored as a float
  82. float timeStep = eventData["TimeStep"].GetFloat();
  83. // Move sprites, scale movement with time step
  84. MoveSprites(timeStep);
  85. }
  86. // Create XML patch instructions for screen joystick layout specific to this sample app
  87. String patchInstructions =
  88. "<patch>" +
  89. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  90. " <attribute name=\"Is Visible\" value=\"false\" />" +
  91. " </add>" +
  92. "</patch>";