203 lines
6.7 KiB
C#
203 lines
6.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using DG.Tweening;
|
|
using URandom = UnityEngine.Random;
|
|
|
|
public class ResShower : MonoBehaviour
|
|
{
|
|
public Action DelResRefresh;
|
|
|
|
[SerializeField] GameObject mTplCurrency;
|
|
[SerializeField] RectTransform mRctBg;
|
|
[SerializeField] RectTransform mRctCurrencyIcon;
|
|
[SerializeField] Text mTxtCurrency;
|
|
[SerializeField] Text mTxtCurrencyChange;
|
|
[SerializeField] Color mAddColor = Color.green;
|
|
[SerializeField] Color mReduceColor = Color.red;
|
|
[SerializeField] bool mFixLength = true;
|
|
[SerializeField] bool mUseShort = false;
|
|
|
|
Sequence mPopTweenSequence;
|
|
Sequence mFadeTweenSequence;
|
|
|
|
private Camera mMainCam;
|
|
private Camera mUICam;
|
|
|
|
private int mCurrentNum;
|
|
private int mFinalCount;
|
|
private Vector2 mChangeOriginalPos;
|
|
|
|
public void Init(Camera pMainCam = null, Camera pUICam = null)
|
|
{
|
|
mMainCam = pMainCam;
|
|
mUICam = pUICam;
|
|
|
|
mChangeOriginalPos = mTxtCurrencyChange.rectTransform.anchoredPosition;
|
|
}
|
|
|
|
public void RefreshRes(int pCount)
|
|
{
|
|
mCurrentNum = pCount;
|
|
mTxtCurrency.text = mUseShort ? NumForShort(mCurrentNum) : mCurrentNum.ToString();
|
|
if (!mFixLength)
|
|
{
|
|
mRctBg.sizeDelta = new Vector2(mTxtCurrency.preferredWidth + 124, mRctBg.sizeDelta.y);
|
|
}
|
|
|
|
DelResRefresh?.Invoke();
|
|
}
|
|
|
|
public void FlyRes(Vector3 pWorldPos, int pFlyCount, int pCoinCount, int pFinalCount)
|
|
{
|
|
mFinalCount = pFinalCount;
|
|
float tFlyDelta = 0.02f;
|
|
|
|
for (int i = 0; i < pFlyCount; i++)
|
|
{
|
|
FlyOneCoin(pWorldPos, tFlyDelta * i);
|
|
}
|
|
|
|
PopResChange(pCoinCount);
|
|
StartCoroutine(SeperatlyAddCoin(tFlyDelta, pFlyCount, pCoinCount));
|
|
}
|
|
|
|
public void PopResChange(int pChangedNum)
|
|
{
|
|
mTxtCurrencyChange.text = pChangedNum >= 0 ? "+" + pChangedNum.ToString() : pChangedNum.ToString();
|
|
Color tColor = pChangedNum > 0 ? mAddColor : mReduceColor;
|
|
tColor.a = 0;
|
|
mTxtCurrencyChange.color = tColor;
|
|
mTxtCurrencyChange.rectTransform.anchoredPosition = mChangeOriginalPos;
|
|
|
|
mTxtCurrencyChange.rectTransform.DOKill();
|
|
mPopTweenSequence = DOTween.Sequence();
|
|
mPopTweenSequence.Append(mTxtCurrencyChange.rectTransform.DOAnchorPosY(-100, 0.3f).SetEase(Ease.OutBounce));
|
|
mPopTweenSequence.AppendInterval(0.8f);
|
|
mPopTweenSequence.Append(mTxtCurrencyChange.rectTransform.DOAnchorPosY(0, 0.3f).SetEase(Ease.InBounce));
|
|
|
|
mTxtCurrencyChange.DOKill();
|
|
mFadeTweenSequence = DOTween.Sequence();
|
|
mFadeTweenSequence.Append(mTxtCurrencyChange.DOFade(1, 0.3f));
|
|
mFadeTweenSequence.AppendInterval(0.8f);
|
|
mFadeTweenSequence.Append(mTxtCurrencyChange.DOFade(0, 0.3f));
|
|
|
|
mPopTweenSequence.Restart();
|
|
mFadeTweenSequence.Restart();
|
|
}
|
|
|
|
public void ShowNotEnoughCoin()
|
|
{
|
|
mTxtCurrency.color = Color.white;
|
|
mTxtCurrency.rectTransform.anchoredPosition = new Vector2(32, 0);
|
|
|
|
mTxtCurrency.color = Color.red;
|
|
mTxtCurrency.rectTransform.DOKill();
|
|
mTxtCurrency.rectTransform.DOPunchAnchorPos(new Vector2(0, 15), 0.5f).onComplete = () =>
|
|
{
|
|
mTxtCurrency.color = Color.white;
|
|
mTxtCurrency.rectTransform.anchoredPosition = new Vector2(32, 0);
|
|
};
|
|
}
|
|
|
|
private void FlyOneCoin(Vector3 pWorldPos, float pDelay)
|
|
{
|
|
Vector2 tStartPos = GetLocalPosInRect(pWorldPos, mRctCurrencyIcon, mMainCam, mUICam);
|
|
RectTransform tFlyCoin = Instantiate<GameObject>(mTplCurrency).GetComponent<RectTransform>();
|
|
tFlyCoin.gameObject.SetActive(true);
|
|
tFlyCoin.SetParent(mRctCurrencyIcon, false);
|
|
tFlyCoin.localScale = Vector3.one;
|
|
tFlyCoin.anchoredPosition = tStartPos;
|
|
|
|
Vector3 tJumpPos = tStartPos + new Vector2(UnityEngine.Random.Range(-250f, 250f), UnityEngine.Random.Range(-250f, 250f));
|
|
tFlyCoin.DOJumpAnchorPos(tJumpPos, URandom.Range(100, 400), 1, 0.3f);
|
|
|
|
tFlyCoin.DOAnchorPos(Vector2.zero, 0.5f).SetDelay(0.3f + pDelay).SetEase(Ease.Linear).onComplete = () =>
|
|
{
|
|
Destroy(tFlyCoin.gameObject);
|
|
};
|
|
}
|
|
|
|
private IEnumerator SeperatlyAddCoin(float pFlyDelta, int pFlyCount, int pCoinCount)
|
|
{
|
|
int tCoinPerFly = 1, tCoinLastFly = 1;
|
|
if (pCoinCount > pFlyCount)
|
|
{
|
|
tCoinPerFly = pCoinCount / pFlyCount;
|
|
tCoinLastFly = pCoinCount - tCoinPerFly * pFlyCount;
|
|
if (tCoinLastFly == 0)
|
|
tCoinLastFly = tCoinPerFly;
|
|
}
|
|
|
|
yield return new WaitForSeconds(1f);
|
|
|
|
for (int i = 0; i < pFlyCount; i++)
|
|
{
|
|
int tAddCount = i < pFlyCount - 1 ? tCoinPerFly : tCoinLastFly;
|
|
|
|
if (i == pFlyCount - 1)
|
|
{
|
|
RefreshRes(mFinalCount);
|
|
}
|
|
else
|
|
{
|
|
AddCoins(tAddCount);
|
|
}
|
|
|
|
yield return new WaitForSeconds(pFlyDelta);
|
|
}
|
|
}
|
|
|
|
private void AddCoins(int pAddCount)
|
|
{
|
|
mCurrentNum += pAddCount;
|
|
RefreshRes(mCurrentNum);
|
|
}
|
|
|
|
private void PunchIcon(RectTransform pRect)
|
|
{
|
|
pRect.localScale = Vector3.one;
|
|
pRect.DOKill();
|
|
pRect.DOPunchScale(Vector3.one * 1.2f, 0.3f);
|
|
}
|
|
|
|
private Vector2 GetLocalPosInRect(Vector3 pWorldPos, RectTransform pRect, Camera pMainCam, Camera pUICam = null)
|
|
{
|
|
Vector2 tScreenPos = RectTransformUtility.WorldToScreenPoint(pMainCam, pWorldPos);
|
|
Vector2 tLocalPos = Vector2.zero;
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(pRect, tScreenPos, pUICam, out tLocalPos);
|
|
|
|
return tLocalPos;
|
|
}
|
|
|
|
const int BILLION = 1000000000;
|
|
const int MILLION = 1000000;
|
|
const int THOUSAND = 1000;
|
|
|
|
private string NumForShort(int pNum)
|
|
{
|
|
string tNumStr = pNum.ToString();
|
|
|
|
float tShortNum = 0;
|
|
if (pNum >= BILLION)
|
|
{
|
|
tShortNum = pNum / (float)BILLION;
|
|
tNumStr = (tShortNum - ((int)tShortNum)) >= 0.1f ? string.Format("{0:F1}B", tShortNum) : string.Format("{0}B", (int)tShortNum);
|
|
}
|
|
else if (pNum >= MILLION)
|
|
{
|
|
tShortNum = pNum / (float)MILLION;
|
|
tNumStr = (tShortNum - ((int)tShortNum)) >= 0.1f ? string.Format("{0:F1}M", tShortNum) : string.Format("{0}M", (int)tShortNum);
|
|
}
|
|
else if (pNum >= THOUSAND * 10)
|
|
{
|
|
tShortNum = pNum / (float)THOUSAND;
|
|
tNumStr = (tShortNum - ((int)tShortNum)) >= 0.1f ? string.Format("{0:F1}K", tShortNum) : string.Format("{0}K", (int)tShortNum);
|
|
}
|
|
|
|
return tNumStr;
|
|
}
|
|
}
|