Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / effects / AutoDestroyParticles.cs
@小李xl 小李xl on 12 Nov 2023 1 KB 修复特效回收问题
  1.  
  2. using Godot;
  3. using Godot.Collections;
  4.  
  5. /// <summary>
  6. /// 到期自动销毁的粒子特效
  7. /// </summary>
  8. public partial class AutoDestroyParticles : GpuParticles2D, IEffect
  9. {
  10. /// <summary>
  11. /// 延时销毁时间
  12. /// </summary>
  13. [Export]
  14. public float DelayTime { get; set; } = 2f;
  15. /// <summary>
  16. /// 子节点包含的例子特效, 在创建完成后自动播放
  17. /// </summary>
  18. [Export]
  19. public Array<GpuParticles2D> Particles2D { get; set; }
  20. public bool IsDestroyed { get; private set; }
  21. public bool IsRecycled { get; set; }
  22. public string Logotype { get; set; }
  23.  
  24. private double _timer;
  25. private bool _isPlay;
  26. public virtual void PlayEffect()
  27. {
  28. Emitting = true;
  29. Restart();
  30. if (Particles2D != null)
  31. {
  32. foreach (var gpuParticles2D in Particles2D)
  33. {
  34. gpuParticles2D.Emitting = true;
  35. Restart();
  36. }
  37. }
  38. _timer = 0;
  39. _isPlay = true;
  40. }
  41.  
  42. public override void _Process(double delta)
  43. {
  44. if (!_isPlay)
  45. {
  46. return;
  47. }
  48. _timer += delta;
  49. if (_timer >= DelayTime)
  50. {
  51. Emitting = false;
  52. if (Particles2D != null)
  53. {
  54. foreach (var gpuParticles2D in Particles2D)
  55. {
  56. gpuParticles2D.Emitting = false;
  57. }
  58. }
  59. _isPlay = false;
  60. ObjectPool.Reclaim(this);
  61. }
  62. }
  63.  
  64.  
  65. public void Destroy()
  66. {
  67. if (IsDestroyed)
  68. {
  69. return;
  70. }
  71.  
  72. IsDestroyed = true;
  73. QueueFree();
  74. }
  75.  
  76.  
  77. public void OnReclaim()
  78. {
  79. GetParent().RemoveChild(this);
  80. }
  81.  
  82. public void OnLeavePool()
  83. {
  84. }
  85. }