03_Sprites.as 3.7 KB

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