using System; using System.Collections.Generic; using CIG.Extensions; using CIGEnums; using SUISS.Core; using SUISSEngine; using UnityEngine; namespace CIG { public sealed class CurrencyAnimator : SingletonMonobehaviour { private void Start() { this._rectTransform = (RectTransform)base.transform; SingletonMonobehaviour.Instance.BalanceChangedEvent += this.OnBalanceChanged; } protected override void OnDestroy() { if (SingletonMonobehaviour.IsAvailable) { SingletonMonobehaviour.Instance.BalanceChangedEvent -= this.OnBalanceChanged; } this._animationSources.Clear(); this.CancelInvoke(new Action(this.PlayAnimationQueue)); base.OnDestroy(); } public void PlayAnimations(Currencies currencies, object earnSource) { int keyCount = currencies.KeyCount; for (int i = 0; i < keyCount; i++) { CurrencyValue currency = currencies.GetCurrency(i); if (currency.Value > 0m) { this.PlayAnimation(currency, earnSource); } } } public void RegisterCurrencySource(CurrencyAnimationSource source) { if (this._animationSources.Contains(source)) { UnityEngine.Debug.LogWarningFormat(source, "Currency Animation Source '{0}' already registered.", new object[] { source.name }); return; } this._animationSources.Add(source); } public void UnregisterCurrencySource(CurrencyAnimationSource source) { this._animationSources.Remove(source); } private Vector3 CalcSourcePosition(CurrencyAnimPositionType positionType, Vector3 position) { switch (positionType) { case CurrencyAnimPositionType.RawPosition: return position; case CurrencyAnimPositionType.ScreenPosition: return this._uiCamera.ScreenToWorldPoint(position); case CurrencyAnimPositionType.WorldPosition: { Camera islandCamera = CityIsland.Current.isometricIsland.islandCamera; return this._uiCamera.ScreenToWorldPoint(islandCamera.WorldToScreenPoint(position)); } default: UnityEngine.Debug.LogWarningFormat("Did you forget to cover a case in the CurrencyAnimator? - Missing case: '{0}'", new object[] { positionType }); return this._rectTransform.position; } } private CurrencyAnimationSource FindAnimationSource(CurrencyValue currency, object earnSource) { if (earnSource == null) { return null; } CurrencyAnimType animType = this.GetAnimType(currency.Currency); CurrencyAnimationSource currencyAnimationSource = this._animationSources.Find((CurrencyAnimationSource x) => x.AnimationSource == earnSource && x.CurrencyType == animType); if (currencyAnimationSource != null) { return currencyAnimationSource; } if (animType != CurrencyAnimType.Any) { currencyAnimationSource = this._animationSources.Find((CurrencyAnimationSource x) => x.AnimationSource == earnSource && x.CurrencyType == CurrencyAnimType.Any); } return currencyAnimationSource; } private void QueueAnimation(CurrencyValue currency, object earnSource) { this._animationQueue.Add(new CurrencyAnimator.EarnedCurrency(currency, earnSource)); if (!this.IsInvoking(new Action(this.PlayAnimationQueue))) { this.InvokeNextFrame(new Action(this.PlayAnimationQueue)); } } private void PlayAnimationQueue() { int count = this._animationQueue.Count; for (int i = 0; i < count; i++) { this.PlayAnimation(this._animationQueue[i]); } this._animationQueue.Clear(); } private void PlayAnimation(CurrencyValue currency, object earnSource) { CurrencyAnimationSource currencyAnimationSource = this.FindAnimationSource(currency, earnSource); if (earnSource != null && currencyAnimationSource == null) { this.QueueAnimation(currency, earnSource); } else { this.PlayAnimation(currency, currencyAnimationSource); } } private void PlayAnimation(CurrencyAnimator.EarnedCurrency earnedCurrency) { CurrencyAnimationSource currencyAnimationSource = this.FindAnimationSource(earnedCurrency.Currency, earnedCurrency.EarnSource); if (currencyAnimationSource == null) { UnityEngine.Debug.LogWarningFormat("No CurrencyAnimationSource found for Earn Source '{0}' when trying to earn currency type '{1}'", new object[] { earnedCurrency.EarnSource, this.GetAnimType(earnedCurrency.Currency.Currency) }); } this.PlayAnimation(earnedCurrency.Currency, currencyAnimationSource); } private void PlayAnimation(CurrencyValue currency, CurrencyAnimationSource animationSource = null) { Vector3 startPosition; if (animationSource != null) { startPosition = this.CalcSourcePosition(animationSource.PositionType, animationSource.Position); } else { startPosition = this._rectTransform.position; } FlyingCurrency flyingCurrencyPrefab = this.GetFlyingCurrencyPrefab(currency.Currency); FlyingCurrency flyingCurrency = UnityEngine.Object.Instantiate(flyingCurrencyPrefab, base.transform); flyingCurrency.gameObject.SetActive(true); flyingCurrency.PlayAnimation(currency, startPosition); } private CurrencyAnimType GetAnimType(string currencyName) { if (currencyName != null) { if (currencyName == "Cash") { return CurrencyAnimType.Cash; } if (currencyName == "Gold") { return CurrencyAnimType.Gold; } if (currencyName == "LevelUp" || currencyName == "XP") { return CurrencyAnimType.XP; } } return CurrencyAnimType.Any; } private FlyingCurrency GetFlyingCurrencyPrefab(string currencyName) { if (currencyName != null) { if (currencyName == "Cash") { return this._flyingCashPrefab; } if (currencyName == "Gold") { return this._flyingGoldPrefab; } if (currencyName == "XP") { return this._flyingXPPrefab; } if (currencyName == "LevelUp") { return this._flyingXPPrefab; } } UnityEngine.Debug.LogErrorFormat("Unknown currency: {0}", new object[] { currencyName }); return this._flyingCashPrefab; } private void OnBalanceChanged(Currencies oldBalance, Currencies newBalance, object earnSource) { Currencies currencies = newBalance - oldBalance; this.PlayAnimations(currencies, earnSource); } [SerializeField] private Camera _uiCamera; [SerializeField] private FlyingCurrency _flyingCashPrefab; [SerializeField] private FlyingCurrency _flyingGoldPrefab; [SerializeField] private FlyingCurrency _flyingXPPrefab; private readonly List _animationSources = new List(); private readonly List _animationQueue = new List(); private RectTransform _rectTransform; private class EarnedCurrency { public EarnedCurrency(CurrencyValue currency, object earnSource) { this.Currency = currency; this.EarnSource = earnSource; } public CurrencyValue Currency { get; private set; } public object EarnSource { get; private set; } } } }