FlyRewardView.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using James.Util;
  2. using JamesCore;
  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. public void StopCor()
  43. {
  44. if (isPlaying)
  45. {
  46. StopCoroutine(cor);
  47. cor = null;
  48. }
  49. if (mParentOfCurrency != null)
  50. {
  51. DestroyImmediate(mParentOfCurrency.gameObject);
  52. }
  53. mCallback = null;
  54. }
  55. IEnumerator BoombAndCollectCurrency(int count, Vector3 to)
  56. {
  57. List<Transform> currencyObj = new List<Transform>();
  58. Transform createObj = null;
  59. for (int i = 0; i < count; i++)
  60. {
  61. createObj = CreateCurrencyObj();
  62. currencyObj.Add(createObj);
  63. StartCoroutine(BoomOut(createObj));
  64. }
  65. yield return new WaitForSeconds(timeToBoom);
  66. for (int i = 1; i < currencyObj.Count; ++i)
  67. {
  68. StartCoroutine(FlyToPos(currencyObj[i], to, timeToWait * i));
  69. }
  70. yield return StartCoroutine(FlyToPos(currencyObj[0], to, timeToWait * currencyObj.Count));
  71. Destroy(mParentOfCurrency.gameObject);
  72. cor = null;
  73. mCallback?.Invoke();
  74. }
  75. IEnumerator BoomOut(Transform fromObj)
  76. {
  77. Vector3 from = fromObj.transform.position;
  78. Vector3 to = new Vector3(Random.Range(-boomRange.x, boomRange.x), Random.Range(-boomRange.y, boomRange.y), Random.Range(-boomRange.z, boomRange.z)) + from;
  79. //DebugEx.Log("BoomOut from="+from+" to="+to);
  80. float timer = 0;
  81. while (timer < timeToBoom)
  82. {
  83. timer += Time.deltaTime;
  84. Vector3 pos = Vector3.Lerp(from, to, timer / timeToBoom);
  85. fromObj.position = pos;
  86. yield return null;
  87. }
  88. }
  89. IEnumerator FlyToPos(Transform fromObj, Vector3 to, float delay)
  90. {
  91. yield return new WaitForSeconds(delay);
  92. Vector3 from = fromObj.transform.position;
  93. float timer = 0;
  94. while (timer < timeToFly)
  95. {
  96. timer += Time.deltaTime;
  97. float val = timer / timeToFly;
  98. float factor = animationCurve.Evaluate(val);
  99. Vector3 pos = Vector3.Lerp(from, to, factor);
  100. fromObj.position = pos;
  101. yield return null;
  102. }
  103. Core.Sound.Play(James.Sound.SoundEnum.SE_GETCURRENCY.ToString(), James.Sound.MuteType.SoundEff, false);
  104. }
  105. Transform CreateCurrencyObj()
  106. {
  107. GameObject obj = GameObject.Instantiate(flyIcon.gameObject);
  108. obj.SetActive(true);
  109. obj.transform.SetParent(mParentOfCurrency);
  110. obj.transform.localScale = Vector3.one;
  111. obj.transform.localEulerAngles = Vector3.zero;
  112. obj.transform.position = mParentOfCurrency.transform.position;
  113. if (playFrameAnim)
  114. {
  115. // SequenceOfFramesAni sf = obj.GetComponent<SequenceOfFramesAni>();
  116. // sf.Play();
  117. }
  118. return obj.transform;
  119. }
  120. void CreateParent()
  121. {
  122. GameObject obj = new GameObject("parentOfCurrency");
  123. mParentOfCurrency = obj.transform;
  124. mParentOfCurrency.SetParent(this.transform);
  125. mParentOfCurrency.position = mStartPos;
  126. mParentOfCurrency.localScale = Vector3.one;
  127. obj.transform.localEulerAngles = Vector3.zero;
  128. }
  129. }