Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / ui / bottomTips / BottomTipsPanel.cs
  1. using System.Collections;
  2. using Godot;
  3.  
  4. namespace UI.BottomTips;
  5.  
  6. /// <summary>
  7. /// 底部提示面板
  8. /// </summary>
  9. public partial class BottomTipsPanel : BottomTips
  10. {
  11. private long _id = -1;
  12. private float _offsetY;
  13. //动画播放时间
  14. private float _animationTime = 0.5f;
  15. //动画移动的像素
  16. private int _movePixel = 153;
  17.  
  18. public override void OnCreateUi()
  19. {
  20. _offsetY = L_Panel.Instance.Position.Y - (Position.Y + Size.Y);
  21. }
  22.  
  23. /// <summary>
  24. /// 执行入场流程
  25. /// </summary>
  26. public void PlayInStep(Texture2D icon, string message)
  27. {
  28. if (_id >= 0)
  29. {
  30. StopCoroutine(_id);
  31. HideUi();
  32. }
  33. SetIcon(icon);
  34. SetMessage(message);
  35. _id = StartCoroutine(RunAnimation());
  36. ShowUi();
  37. }
  38.  
  39. /// <summary>
  40. /// 设置图标
  41. /// </summary>
  42. public void SetIcon(Texture2D icon)
  43. {
  44. S_TextureRect.Instance.Texture = icon;
  45. }
  46. /// <summary>
  47. /// 设置文本内容
  48. /// </summary>
  49. public void SetMessage(string message)
  50. {
  51. S_Label.Instance.Text = message;
  52. }
  53.  
  54. private IEnumerator RunAnimation()
  55. {
  56. //还原位置
  57. var pos = L_Panel.Instance.Position;
  58. pos.Y = Position.Y + Size.Y + _offsetY;
  59. L_Panel.Instance.Position = pos;
  60.  
  61. L_Panel.Instance.ResetSize();
  62. yield return 0;
  63. //重新计算中心点
  64. pos.X = Size.X / 2 - L_Panel.Instance.Size.X / 2;
  65. L_Panel.Instance.Position = pos;
  66. yield return 0;
  67.  
  68. //向上移动
  69. var time = 0f;
  70. while (time < _animationTime)
  71. {
  72. L_Panel.Instance.Position = new Vector2(
  73. pos.X,
  74. pos.Y - Mathf.Lerp(0, _movePixel, Mathf.Min(time / _animationTime, 1))
  75. );
  76. time += (float)GetProcessDeltaTime();
  77. yield return 0;
  78. }
  79.  
  80. yield return new WaitForSeconds(3.5f);
  81. //向下移动
  82. while (time > 0)
  83. {
  84. L_Panel.Instance.Position = new Vector2(
  85. pos.X,
  86. pos.Y - Mathf.Lerp(0, _movePixel, Mathf.Max(time / _animationTime, 0))
  87. );
  88. time -= (float)GetProcessDeltaTime();
  89. yield return 0;
  90. }
  91.  
  92. HideUi();
  93. _id = -1;
  94. }
  95. private static BottomTipsPanel _instance;
  96. public static void Init()
  97. {
  98. _instance = UiManager.CreateUi<BottomTipsPanel>(UiManager.UiName.BottomTips);
  99. }
  100.  
  101. /// <summary>
  102. /// 打开Tips, 并设置图标和内容
  103. /// </summary>
  104. /// <param name="icon">显示图标</param>
  105. /// <param name="message">显示消息</param>
  106. public static void ShowTips(string icon, string message)
  107. {
  108. ShowTips(ResourceManager.Load<Texture2D>(icon), message);
  109. }
  110.  
  111. /// <summary>
  112. /// 打开Tips, 并设置图标和内容
  113. /// </summary>
  114. /// <param name="icon">显示图标</param>
  115. /// <param name="message">显示消息</param>
  116. public static void ShowTips(Texture2D icon, string message)
  117. {
  118. _instance.PlayInStep(icon, message);
  119. }
  120. }