Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / effects / AutoDestroySprite.cs
@小李xl 小李xl on 12 Nov 2023 1 KB 修复特效回收问题
  1. using Godot;
  2. using Godot.Collections;
  3.  
  4. /// <summary>
  5. /// 到期自动销毁的帧动画
  6. /// </summary>
  7. public partial class AutoDestroySprite : AnimatedSprite2D, IEffect
  8. {
  9. /// <summary>
  10. /// 播放的动画名称
  11. /// </summary>
  12. [Export]
  13. public string AnimationName { get; set; } = "default";
  14. /// <summary>
  15. /// 延时销毁时间
  16. /// </summary>
  17. [Export]
  18. public float DelayTime { get; set; } = 2f;
  19.  
  20. /// <summary>
  21. /// 子节点包含的例子特效, 在创建完成后自动播放
  22. /// </summary>
  23. [Export]
  24. public Array<GpuParticles2D> Particles2D { get; set; }
  25. public bool IsDestroyed { get; private set; }
  26. public bool IsRecycled { get; set; }
  27. public string Logotype { get; set; }
  28.  
  29. private double _timer;
  30. private bool _isPlay;
  31. public virtual void PlayEffect()
  32. {
  33. if (Particles2D != null)
  34. {
  35. foreach (var gpuParticles2D in Particles2D)
  36. {
  37. gpuParticles2D.Emitting = true;
  38. }
  39. }
  40. _timer = 0;
  41. _isPlay = true;
  42. Play(AnimationName);
  43. }
  44. public override void _Process(double delta)
  45. {
  46. if (!_isPlay)
  47. {
  48. return;
  49. }
  50. _timer += delta;
  51. if (_timer >= DelayTime)
  52. {
  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. 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. }