123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- using SSTR.Util;
- using SSTRCore;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class FlyRewardView : MonoBehaviour
- {
- public bool playFrameAnim;
- public int maxNum;
- public int minNum;
- public Vector3 boomRange; //炸出范围
- public float timeToBoom; //飞出时间
- public float timeToWait; //飞出后每个的停滞时间
- public float timeToFly; //飞去固定位置的时间
- public Image flyIcon;
- public AnimationCurve animationCurve = new AnimationCurve(new Keyframe(0f, 0f, 0f, 1f), new Keyframe(1f, 1f, 1f, 0f));
- private Coroutine cor;
- private Transform mParentOfCurrency;
- private Vector3 mStartPos;
- public bool isPlaying { get { return cor != null; } }
- private System.Action mCallback;
- private void OnEnable()
- {
- flyIcon.gameObject.SetActive(false);
- }
- private void OnDisable()
- {
- StopCor();
- }
- public void FlyCurrency(Vector3 to, System.Action callback)
- {
- if (!isPlaying)
- {
- mStartPos = this.transform.position;
- CreateParent();
- int coinCount = Random.Range(minNum, maxNum + 1);
- cor = StartCoroutine(BoombAndCollectCurrency(coinCount, to));
- mCallback = callback;
- }
- }
-
- // 判断物体是否在特定椭圆区域内
- public bool IsInsideEllipse(GameObject obj, Vector3 center, Vector2 radii)
- {
- Vector3 localPos = obj.transform.position - center;
- float xRatio = localPos.x / radii.x;
- float yRatio = localPos.y / radii.y;
- return xRatio * xRatio + yRatio * yRatio <= 1;
- }
- public void StopCor()
- {
- if (isPlaying)
- {
- StopCoroutine(cor);
- cor = null;
- }
- if (mParentOfCurrency != null)
- {
- DestroyImmediate(mParentOfCurrency.gameObject);
- }
- mCallback = null;
- }
- IEnumerator BoombAndCollectCurrency(int count, Vector3 to)
- {
- List<Transform> currencyObj = new List<Transform>();
- Transform createObj = null;
- for (int i = 0; i < count; i++)
- {
- createObj = CreateCurrencyObj();
- currencyObj.Add(createObj);
- StartCoroutine(BoomOut(createObj));
- }
- yield return new WaitForSeconds(timeToBoom);
- for (int i = 1; i < currencyObj.Count; ++i)
- {
- StartCoroutine(FlyToPos(currencyObj[i], to, timeToWait * i));
- }
- yield return StartCoroutine(FlyToPos(currencyObj[0], to, timeToWait * currencyObj.Count));
- Destroy(mParentOfCurrency.gameObject);
- cor = null;
- mCallback?.Invoke();
- }
- IEnumerator BoomOut(Transform fromObj)
- {
- Vector3 from = fromObj.transform.position;
- Vector3 to = new Vector3(Random.Range(-boomRange.x, boomRange.x), Random.Range(-boomRange.y, boomRange.y), Random.Range(-boomRange.z, boomRange.z)) + from;
- //DebugEx.Log("BoomOut from="+from+" to="+to);
- float timer = 0;
- while (timer < timeToBoom)
- {
- timer += Time.deltaTime;
- Vector3 pos = Vector3.Lerp(from, to, timer / timeToBoom);
- fromObj.position = pos;
- yield return null;
- }
- }
- IEnumerator FlyToPos(Transform fromObj, Vector3 to, float delay)
- {
- yield return new WaitForSeconds(delay);
- Vector3 from = fromObj.transform.position;
- float timer = 0;
- while (timer < timeToFly)
- {
- timer += Time.deltaTime;
- float val = timer / timeToFly;
- float factor = animationCurve.Evaluate(val);
- Vector3 pos = Vector3.Lerp(from, to, factor);
- fromObj.position = pos;
- yield return null;
- }
- Core.Sound.Play(SSTR.Sound.SoundEnum.UI_huode_jinbi.ToString(), SSTR.Sound.MuteType.SoundEff, false);
- }
- Transform CreateCurrencyObj()
- {
- GameObject obj = GameObject.Instantiate(flyIcon.gameObject);
- obj.SetActive(true);
- obj.transform.SetParent(mParentOfCurrency);
- obj.transform.localScale = Vector3.one;
- obj.transform.localEulerAngles = Vector3.zero;
- obj.transform.position = mParentOfCurrency.transform.position;
- if (playFrameAnim)
- {
- // SequenceOfFramesAni sf = obj.GetComponent<SequenceOfFramesAni>();
- // sf.Play();
- }
- return obj.transform;
- }
- void CreateParent()
- {
- GameObject obj = new GameObject("parentOfCurrency");
- mParentOfCurrency = obj.transform;
- mParentOfCurrency.SetParent(this.transform);
- mParentOfCurrency.position = mStartPos;
- mParentOfCurrency.localScale = Vector3.one;
- obj.transform.localEulerAngles = Vector3.zero;
- }
- }
|