Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / manager / SpecialEffectManager.cs
  1.  
  2. using System.Collections.Generic;
  3. using Godot;
  4.  
  5. /// <summary>
  6. /// 特效管理器
  7. /// </summary>
  8. public static class SpecialEffectManager
  9. {
  10.  
  11. private static Stack<SpecialEffect> _specialEffectStack = new Stack<SpecialEffect>();
  12.  
  13. /// <summary>
  14. /// 基础特效播放类, 用于播放序列帧动画特效, 播完就回收
  15. /// </summary>
  16. private class SpecialEffect : AnimatedSprite
  17. {
  18. //记录循环次数
  19. public int LoopCount;
  20.  
  21. private int currLoopCount = 0;
  22. public override void _Ready()
  23. {
  24. Connect("animation_finished", this, nameof(OnAnimationFinished));
  25. }
  26.  
  27. //动画结束
  28. private void OnAnimationFinished()
  29. {
  30. currLoopCount++;
  31. if (currLoopCount >= LoopCount)
  32. {
  33. Over();
  34. }
  35. }
  36.  
  37. private void Over()
  38. {
  39. currLoopCount = 0;
  40. RecycleSpecialEffect(this);
  41. }
  42. }
  43.  
  44. /// <summary>
  45. /// 在场景指定位置播放一个特效, 特效必须是 SpriteFrames 类型
  46. /// </summary>
  47. /// <param name="path">特效SpriteFrames资源路径</param>
  48. /// <param name="animName">动画名称</param>
  49. /// <param name="pos">坐标</param>
  50. /// <param name="rotation">旋转角度, 弧度制</param>
  51. /// <param name="scale">缩放</param>
  52. /// <param name="offset">图像偏移</param>
  53. /// <param name="zIndex">层级</param>
  54. /// <param name="speed">播放速度</param>
  55. /// <param name="loopCount">循环次数, 到达该次数特效停止播放</param>
  56. public static void Play(string path, string animName, Vector2 pos, float rotation, Vector2 scale, Vector2 offset, int zIndex = 0, float speed = 1, int loopCount = 1)
  57. {
  58. var spriteFrames = ResourceManager.Load<SpriteFrames>(path);
  59. var specialEffect = GetSpecialEffect();
  60. specialEffect.GlobalPosition = pos;
  61. specialEffect.Rotation = rotation;
  62. specialEffect.Scale = scale;
  63. specialEffect.ZIndex = zIndex;
  64. specialEffect.Offset = offset;
  65. specialEffect.SpeedScale = speed;
  66. specialEffect.LoopCount = loopCount;
  67. specialEffect.Frames = spriteFrames;
  68. specialEffect.Play(animName);
  69. GameApplication.Instance.Room.GetRoot(true).AddChild(specialEffect);
  70. }
  71.  
  72. private static SpecialEffect GetSpecialEffect()
  73. {
  74. if (_specialEffectStack.Count > 0)
  75. {
  76. return _specialEffectStack.Pop();
  77. }
  78.  
  79. return new SpecialEffect();
  80. }
  81. /// <summary>
  82. /// 回收2D音频播放节点
  83. /// </summary>
  84. private static void RecycleSpecialEffect(SpecialEffect inst)
  85. {
  86. var parent = inst.GetParent();
  87. if (parent != null)
  88. {
  89. parent.RemoveChild(inst);
  90. }
  91.  
  92. inst.Playing = false;
  93. inst.Frames = null;
  94. _specialEffectStack.Push(inst);
  95. }
  96. }