Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / effects / AutoDestroyParticles.cs
@小李xl 小李xl on 4 Jan 2024 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. ZIndex = 1;
  29. Emitting = true;
  30. Restart();
  31. if (Particles2D != null)
  32. {
  33. foreach (var gpuParticles2D in Particles2D)
  34. {
  35. gpuParticles2D.Emitting = true;
  36. Restart();
  37. }
  38. }
  39. _timer = 0;
  40. _isPlay = true;
  41. }
  42.  
  43. public override void _Process(double delta)
  44. {
  45. if (!_isPlay)
  46. {
  47. return;
  48. }
  49. _timer += delta;
  50. if (_timer >= DelayTime)
  51. {
  52. Emitting = false;
  53. if (Particles2D != null)
  54. {
  55. foreach (var gpuParticles2D in Particles2D)
  56. {
  57. gpuParticles2D.Emitting = false;
  58. }
  59. }
  60. _isPlay = false;
  61. ObjectPool.Reclaim(this);
  62. }
  63. }
  64.  
  65.  
  66. public void Destroy()
  67. {
  68. if (IsDestroyed)
  69. {
  70. return;
  71. }
  72.  
  73. IsDestroyed = true;
  74. QueueFree();
  75. }
  76.  
  77.  
  78. public void OnReclaim()
  79. {
  80. GetParent().RemoveChild(this);
  81. }
  82.  
  83. public void OnLeavePool()
  84. {
  85. }
  86. }