Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / map / fog / FogMaskBase.cs
@小李xl 小李xl on 11 Oct 2023 2 KB 过道迷雾过渡, 开发完成
  1.  
  2. using System;
  3. using System.Collections;
  4. using Godot;
  5.  
  6. public partial class FogMaskBase : PointLight2D, IDestroy
  7. {
  8. public bool IsDestroyed { get; private set; }
  9. /// <summary>
  10. /// 是否探索过迷雾
  11. /// </summary>
  12. public bool IsExplored { get; set; }
  13. /// <summary>
  14. /// 迷雾透明度值, 这个值在调用 TransitionAlpha() 时改变, 用于透明度判断
  15. /// </summary>
  16. public float TargetAlpha { get; set; }
  17. private long _cid = -1;
  18. /// <summary>
  19. /// 使颜色的 alpha 通道过渡到指定的值
  20. /// </summary>
  21. /// <param name="targetAlpha">透明度值</param>
  22. public void TransitionAlpha(float targetAlpha)
  23. {
  24. TargetAlpha = targetAlpha;
  25. if (_cid >= 0)
  26. {
  27. World.Current.StopCoroutine(_cid);
  28. }
  29. _cid = World.Current.StartCoroutine(RunTransitionAlpha(targetAlpha, GameConfig.FogTransitionTime, false));
  30. }
  31.  
  32. /// <summary>
  33. /// 使颜色的 alpha 通道过渡到指定的值
  34. /// </summary>
  35. /// <param name="tartAlpha">初始透明度</param>
  36. /// <param name="targetAlpha">透明度值</param>
  37. public void TransitionAlpha(float tartAlpha, float targetAlpha)
  38. {
  39. Color = new Color(1, 1, 1, tartAlpha);
  40. TransitionAlpha(targetAlpha);
  41. }
  42.  
  43. /// <summary>
  44. /// 使颜色的 alpha 通道过渡到 GameConfig.DarkFogAlpha
  45. /// </summary>
  46. public void TransitionToDark()
  47. {
  48. TransitionAlpha(GameConfig.DarkFogAlpha);
  49. }
  50. /// <summary>
  51. /// 使颜色的 alpha 通道过渡到 1
  52. /// </summary>
  53. public void TransitionToLight()
  54. {
  55. TransitionAlpha(1);
  56. }
  57.  
  58. /// <summary>
  59. /// 使颜色的 alpha 通道过渡到 0, 然后隐藏该 Fog
  60. /// </summary>
  61. /// <param name="time">过渡时间</param>
  62. public void TransitionToHide(float time = GameConfig.FogTransitionTime)
  63. {
  64. TargetAlpha = 0;
  65. if (Visible)
  66. {
  67. if (_cid >= 0)
  68. {
  69. World.Current.StopCoroutine(_cid);
  70. }
  71. _cid = World.Current.StartCoroutine(RunTransitionAlpha(TargetAlpha, time, true));
  72. }
  73. }
  74.  
  75. private IEnumerator RunTransitionAlpha(float targetAlpha, float time, bool hide)
  76. {
  77. var originColor = Color;
  78. var a = originColor.A;
  79. var delta = Mathf.Abs(a - targetAlpha) / time;
  80. while (Math.Abs(a - targetAlpha) > 0.001f)
  81. {
  82. a = Mathf.MoveToward(a, targetAlpha, delta * (float)World.Current.GetProcessDeltaTime());
  83. Color = new Color(1, 1, 1, a);
  84. yield return null;
  85. }
  86. _cid = -1;
  87. if (hide)
  88. {
  89. this.SetActive(false);
  90. }
  91. }
  92. public void Destroy()
  93. {
  94. if (IsDestroyed)
  95. {
  96. return;
  97. }
  98.  
  99. IsDestroyed = true;
  100. QueueFree();
  101. }
  102. }