Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / common / NodeAnimation.cs
  1.  
  2. using System;
  3. using System.Collections;
  4. using Godot;
  5.  
  6. public static class NodeAnimation
  7. {
  8. /// <summary>
  9. /// 将物体渐变显示
  10. /// </summary>
  11. /// <param name="activityObject">指定对象</param>
  12. /// <param name="time">时间, 单位: 秒</param>
  13. /// <param name="finish">完成回调</param>
  14. public static void PlayShowAnimation(this ActivityObject activityObject, float time, Action finish = null)
  15. {
  16. activityObject.StartCoroutine(_PlayShowAnimation(activityObject, time, finish));
  17. }
  18.  
  19. /// <summary>
  20. /// 将物体渐变隐藏
  21. /// </summary>
  22. /// <param name="activityObject">指定对象</param>
  23. /// <param name="time">时间, 单位: 秒</param>
  24. /// <param name="finish">完成回调</param>
  25. public static void PlayHideAnimation(this ActivityObject activityObject, float time, Action finish = null)
  26. {
  27. activityObject.StartCoroutine(_PlayAlphaAnimation(activityObject, time, finish));
  28. }
  29.  
  30. private static IEnumerator _PlayShowAnimation(ActivityObject activityObject, float time, Action action)
  31. {
  32. activityObject.Visible = true;
  33. var modulateA = activityObject.Modulate.A;
  34. var speed = (1f - modulateA) / time;
  35. while (modulateA < 1f)
  36. {
  37. modulateA += speed * (float)activityObject.GetProcessDeltaTime();
  38. var color = activityObject.Modulate;
  39. color.A = modulateA;
  40. activityObject.Modulate = color;
  41. yield return null;
  42. }
  43. if (action != null)
  44. {
  45. action();
  46. }
  47. }
  48. private static IEnumerator _PlayAlphaAnimation(ActivityObject activityObject, float time, Action action)
  49. {
  50. var modulateA = activityObject.Modulate.A;
  51. var speed = modulateA / time;
  52. while (modulateA > 0f)
  53. {
  54. modulateA -= speed * (float)activityObject.GetProcessDeltaTime();
  55. var color = activityObject.Modulate;
  56. color.A = modulateA;
  57. activityObject.Modulate = color;
  58. yield return null;
  59. }
  60. activityObject.Visible = false;
  61.  
  62. if (action != null)
  63. {
  64. action();
  65. }
  66. }
  67.  
  68. /// <summary>
  69. /// 播放相机缩放动画
  70. /// </summary>
  71. /// <param name="camera">相机对象</param>
  72. /// <param name="targetZoom">目标缩放值</param>
  73. /// <param name="speed">速度</param>
  74. /// <param name="finish">完成回调</param>
  75. public static void PlayZoomAnimation(this GameCamera camera, Vector2 targetZoom, float speed, Action finish = null)
  76. {
  77. GameApplication.Instance.StartCoroutine(_StartZoomAnimation(camera, targetZoom, speed, finish));
  78. }
  79. private static IEnumerator _StartZoomAnimation(GameCamera camera, Vector2 targetZoom, float speed, Action finish)
  80. {
  81. var currZoom = camera.Zoom;
  82. while (targetZoom != currZoom)
  83. {
  84. currZoom = currZoom.MoveToward(targetZoom, speed * (float)camera.GetProcessDeltaTime());
  85. camera.Zoom = currZoom;
  86. yield return null;
  87. }
  88. if (finish != null)
  89. {
  90. finish();
  91. }
  92. }
  93. }