2
0

SpawnTrail.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Stride.Core;
  7. using Stride.Engine;
  8. namespace MyTrailEffect
  9. {
  10. /// <summary>
  11. /// A script which spawns a timed instance from a source prefab.
  12. /// </summary>
  13. public class SpawnTrail : AsyncScript
  14. {
  15. private float timeIntervalCountdown = 0f;
  16. public AnimationComponent Animation { get; set; }
  17. private TimeSpan lastTime = TimeSpan.FromMinutes(60);
  18. /// <summary>
  19. /// Source to the prefab, selectable by the user
  20. /// </summary>
  21. [DataMember(10)]
  22. [Display("Source")]
  23. public Prefab SourcePrefab;
  24. /// <summary>
  25. /// Should the prefab follow the entity's transform component on change or not
  26. /// </summary>
  27. [DataMember(20)]
  28. [Display("Following")]
  29. public bool Following { get; set; } = true;
  30. /// <summary>
  31. /// How long before the prefab instance is deleted, selectable by the user
  32. /// </summary>
  33. [DataMember(30)]
  34. [Display("Timeout")]
  35. public float InstanceTimeout = 3f;
  36. /// <summary>
  37. /// Set the time interval (in seconds) at which to spawn new instances. Set it to 0 to deactivate.
  38. /// </summary>
  39. [DataMember(50)]
  40. [Display("Start time")]
  41. public float TimeInterval { get; set; } = 0f;
  42. public override async Task Execute()
  43. {
  44. var canTrigger = lastTime > Animation.PlayingAnimations[0].CurrentTime;
  45. lastTime = Animation.PlayingAnimations[0].CurrentTime;
  46. while (Game.IsRunning)
  47. {
  48. await Script.NextFrame();
  49. canTrigger |= lastTime > Animation.PlayingAnimations[0].CurrentTime;
  50. lastTime = Animation.PlayingAnimations[0].CurrentTime;
  51. if (canTrigger && Animation.PlayingAnimations[0].CurrentTime.TotalSeconds >= TimeInterval)
  52. {
  53. canTrigger = false;
  54. SpawnInstance();
  55. }
  56. }
  57. }
  58. /// <summary>
  59. /// Will add a cloned entity from the prefab to the scene, wait for the specified time and delete it
  60. /// </summary>
  61. protected void SpawnInstance()
  62. {
  63. if (SourcePrefab == null)
  64. return;
  65. Func<Task> spawnTask = async () =>
  66. {
  67. // Clone
  68. var spawnedEntities = SourcePrefab.Instantiate();
  69. // Add
  70. foreach (var prefabEntity in spawnedEntities)
  71. {
  72. if (Following)
  73. {
  74. Entity.AddChild(prefabEntity);
  75. }
  76. else
  77. {
  78. prefabEntity.Transform.UpdateLocalMatrix();
  79. var worldMatrix = prefabEntity.Transform.LocalMatrix * Entity.Transform.WorldMatrix;
  80. worldMatrix.Decompose(out prefabEntity.Transform.Scale, out prefabEntity.Transform.Rotation, out prefabEntity.Transform.Position);
  81. SceneSystem.SceneInstance.RootScene.Entities.Add(prefabEntity);
  82. }
  83. }
  84. // Countdown
  85. var secondsCountdown = InstanceTimeout;
  86. while (secondsCountdown > 0f)
  87. {
  88. await Script.NextFrame();
  89. secondsCountdown -= (float)Game.UpdateTime.Elapsed.TotalSeconds;
  90. }
  91. // Remove
  92. foreach (var clonedEntity in spawnedEntities)
  93. {
  94. if (Following)
  95. {
  96. Entity.RemoveChild(clonedEntity);
  97. }
  98. else
  99. {
  100. SceneSystem.SceneInstance.RootScene.Entities.Remove(clonedEntity);
  101. }
  102. }
  103. // Cleanup
  104. spawnedEntities.Clear();
  105. };
  106. Script.AddTask(spawnTask);
  107. }
  108. }
  109. }