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 frame = GameApplication.Instance.TargetFps * _animationTime;
  70. var stepPixel = _movePixel / frame;
  71. for (var i = 0; i < frame; i++)
  72. {
  73. pos.X = L_Panel.Instance.Position.X;
  74. pos.Y -= stepPixel;
  75. L_Panel.Instance.Position = pos;
  76. yield return 0;
  77. }
  78.  
  79. yield return new WaitForSeconds(3.5f);
  80. //向下移动
  81. for (var i = 0; i < frame; i++)
  82. {
  83. pos.X = L_Panel.Instance.Position.X;
  84. pos.Y += stepPixel;
  85. L_Panel.Instance.Position = pos;
  86. yield return 0;
  87. }
  88.  
  89. HideUi();
  90. _id = -1;
  91. }
  92. private static BottomTipsPanel _instance;
  93. public static void Init()
  94. {
  95. _instance = UiManager.CreateUi<BottomTipsPanel>(UiManager.UiName.BottomTips);
  96. }
  97.  
  98. /// <summary>
  99. /// 打开Tips, 并设置图标和内容
  100. /// </summary>
  101. /// <param name="icon">显示图标</param>
  102. /// <param name="message">显示消息</param>
  103. public static void ShowTips(string icon, string message)
  104. {
  105. ShowTips(ResourceManager.Load<Texture2D>(icon), message);
  106. }
  107.  
  108. /// <summary>
  109. /// 打开Tips, 并设置图标和内容
  110. /// </summary>
  111. /// <param name="icon">显示图标</param>
  112. /// <param name="message">显示消息</param>
  113. public static void ShowTips(Texture2D icon, string message)
  114. {
  115. _instance.PlayInStep(icon, message);
  116. }
  117. }