Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / effects / AutoDestroySprite.cs
@小李xl 小李xl on 4 Jan 2024 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. ZIndex = 1;
  34. if (Particles2D != null)
  35. {
  36. foreach (var gpuParticles2D in Particles2D)
  37. {
  38. gpuParticles2D.Emitting = true;
  39. }
  40. }
  41. _timer = 0;
  42. _isPlay = true;
  43. Play(AnimationName);
  44. }
  45. public override void _Process(double delta)
  46. {
  47. if (!_isPlay)
  48. {
  49. return;
  50. }
  51. _timer += delta;
  52. if (_timer >= DelayTime)
  53. {
  54. if (Particles2D != null)
  55. {
  56. foreach (var gpuParticles2D in Particles2D)
  57. {
  58. gpuParticles2D.Emitting = false;
  59. }
  60. }
  61. _isPlay = false;
  62. ObjectPool.Reclaim(this);
  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. }