FlyRewardView.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using SSTR.Util;
  2. using SSTRCore;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class FlyRewardView : MonoBehaviour
  8. {
  9. public bool playFrameAnim;
  10. public int maxNum;
  11. public int minNum;
  12. public Vector3 boomRange; //炸出范围
  13. public float timeToBoom; //飞出时间
  14. public float timeToWait; //飞出后每个的停滞时间
  15. public float timeToFly; //飞去固定位置的时间
  16. public Image flyIcon;
  17. public AnimationCurve animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
  18. private Coroutine cor;
  19. private Transform mParentOfCurrency;
  20. private Vector3 mStartPos;
  21. public bool isPlaying { get { return cor != null; } }
  22. private System.Action mCallback;
  23. private void OnEnable()
  24. {
  25. flyIcon.gameObject.SetActive(false);
  26. }
  27. private void OnDisable()
  28. {
  29. StopCor();
  30. }
  31. public void FlyCurrency(Vector3 to, System.Action callback)
  32. {
  33. if (!isPlaying)
  34. {
  35. mStartPos = this.transform.position;
  36. CreateParent();
  37. int coinCount = Random.Range(minNum, maxNum + 1);
  38. cor = StartCoroutine(BoombAndCollectCurrency(coinCount, to));
  39. mCallback = callback;
  40. }
  41. }
  42. // 判断物体是否在特定椭圆区域内
  43. public bool IsInsideEllipse(GameObject obj, Vector3 center, Vector2 radii)
  44. {
  45. Vector3 localPos = obj.transform.position - center;
  46. float xRatio = localPos.x / radii.x;
  47. float yRatio = localPos.y / radii.y;
  48. return xRatio * xRatio + yRatio * yRatio <= 1;
  49. }
  50. public void StopCor()
  51. {
  52. if (isPlaying)
  53. {
  54. StopCoroutine(cor);
  55. cor = null;
  56. }
  57. if (mParentOfCurrency != null)
  58. {
  59. DestroyImmediate(mParentOfCurrency.gameObject);
  60. }
  61. mCallback = null;
  62. }
  63. IEnumerator BoombAndCollectCurrency(int count, Vector3 to)
  64. {
  65. List<Transform> currencyObj = new List<Transform>();
  66. Transform createObj = null;
  67. for (int i = 0; i < count; i++)
  68. {
  69. createObj = CreateCurrencyObj();
  70. currencyObj.Add(createObj);
  71. StartCoroutine(BoomOut(createObj));
  72. }
  73. yield return new WaitForSeconds(timeToBoom);
  74. for (int i = 1; i < currencyObj.Count; ++i)
  75. {
  76. StartCoroutine(FlyToPos(currencyObj[i], to, timeToWait * i));
  77. }
  78. yield return StartCoroutine(FlyToPos(currencyObj[0], to, timeToWait * currencyObj.Count));
  79. Destroy(mParentOfCurrency.gameObject);
  80. cor = null;
  81. mCallback?.Invoke();
  82. }
  83. IEnumerator BoomOut(Transform fromObj)
  84. {
  85. Vector3 from = fromObj.transform.position;
  86. Vector3 to = new Vector3(Random.Range(-boomRange.x, boomRange.x), Random.Range(-boomRange.y, boomRange.y), Random.Range(-boomRange.z, boomRange.z)) + from;
  87. //DebugEx.Log("BoomOut from="+from+" to="+to);
  88. float timer = 0;
  89. while (timer < timeToBoom)
  90. {
  91. timer += Time.deltaTime;
  92. Vector3 pos = Vector3.Lerp(from, to, timer / timeToBoom);
  93. fromObj.position = pos;
  94. yield return null;
  95. }
  96. }
  97. IEnumerator FlyToPos(Transform fromObj, Vector3 to, float delay)
  98. {
  99. yield return new WaitForSeconds(delay);
  100. Vector3 from = fromObj.transform.position;
  101. float timer = 0;
  102. while (timer < timeToFly)
  103. {
  104. timer += Time.deltaTime;
  105. float val = timer / timeToFly;
  106. float factor = animationCurve.Evaluate(val);
  107. Vector3 pos = Vector3.Lerp(from, to, factor);
  108. fromObj.position = pos;
  109. yield return null;
  110. }
  111. Core.Sound.Play(SSTR.Sound.SoundEnum.UI_huode_jinbi.ToString(), SSTR.Sound.MuteType.SoundEff, false);
  112. }
  113. Transform CreateCurrencyObj()
  114. {
  115. GameObject obj = GameObject.Instantiate(flyIcon.gameObject);
  116. obj.SetActive(true);
  117. obj.transform.SetParent(mParentOfCurrency);
  118. obj.transform.localScale = Vector3.one;
  119. obj.transform.localEulerAngles = Vector3.zero;
  120. obj.transform.position = mParentOfCurrency.transform.position;
  121. if (playFrameAnim)
  122. {
  123. // SequenceOfFramesAni sf = obj.GetComponent<SequenceOfFramesAni>();
  124. // sf.Play();
  125. }
  126. return obj.transform;
  127. }
  128. void CreateParent()
  129. {
  130. GameObject obj = new GameObject("parentOfCurrency");
  131. mParentOfCurrency = obj.transform;
  132. mParentOfCurrency.SetParent(this.transform);
  133. mParentOfCurrency.position = mStartPos;
  134. mParentOfCurrency.localScale = Vector3.one;
  135. obj.transform.localEulerAngles = Vector3.zero;
  136. }
  137. }