您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 

251 行
7.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using CIG.Extensions;
  4. using CIGEnums;
  5. using SUISS.Core;
  6. using SUISSEngine;
  7. using UnityEngine;
  8. namespace CIG
  9. {
  10. public sealed class CurrencyAnimator : SingletonMonobehaviour<CurrencyAnimator>
  11. {
  12. private void Start()
  13. {
  14. this._rectTransform = (RectTransform)base.transform;
  15. SingletonMonobehaviour<CIGGameState>.Instance.BalanceChangedEvent += this.OnBalanceChanged;
  16. }
  17. protected override void OnDestroy()
  18. {
  19. if (SingletonMonobehaviour<CIGGameState>.IsAvailable)
  20. {
  21. SingletonMonobehaviour<CIGGameState>.Instance.BalanceChangedEvent -= this.OnBalanceChanged;
  22. }
  23. this._animationSources.Clear();
  24. this.CancelInvoke(new Action(this.PlayAnimationQueue));
  25. base.OnDestroy();
  26. }
  27. public void PlayAnimations(Currencies currencies, object earnSource)
  28. {
  29. int keyCount = currencies.KeyCount;
  30. for (int i = 0; i < keyCount; i++)
  31. {
  32. CurrencyValue currency = currencies.GetCurrency(i);
  33. if (currency.Value > 0m)
  34. {
  35. this.PlayAnimation(currency, earnSource);
  36. }
  37. }
  38. }
  39. public void RegisterCurrencySource(CurrencyAnimationSource source)
  40. {
  41. if (this._animationSources.Contains(source))
  42. {
  43. UnityEngine.Debug.LogWarningFormat(source, "Currency Animation Source '{0}' already registered.", new object[]
  44. {
  45. source.name
  46. });
  47. return;
  48. }
  49. this._animationSources.Add(source);
  50. }
  51. public void UnregisterCurrencySource(CurrencyAnimationSource source)
  52. {
  53. this._animationSources.Remove(source);
  54. }
  55. private Vector3 CalcSourcePosition(CurrencyAnimPositionType positionType, Vector3 position)
  56. {
  57. switch (positionType)
  58. {
  59. case CurrencyAnimPositionType.RawPosition:
  60. return position;
  61. case CurrencyAnimPositionType.ScreenPosition:
  62. return this._uiCamera.ScreenToWorldPoint(position);
  63. case CurrencyAnimPositionType.WorldPosition:
  64. {
  65. Camera islandCamera = CityIsland.Current.isometricIsland.islandCamera;
  66. return this._uiCamera.ScreenToWorldPoint(islandCamera.WorldToScreenPoint(position));
  67. }
  68. default:
  69. UnityEngine.Debug.LogWarningFormat("Did you forget to cover a case in the CurrencyAnimator? - Missing case: '{0}'", new object[]
  70. {
  71. positionType
  72. });
  73. return this._rectTransform.position;
  74. }
  75. }
  76. private CurrencyAnimationSource FindAnimationSource(CurrencyValue currency, object earnSource)
  77. {
  78. if (earnSource == null)
  79. {
  80. return null;
  81. }
  82. CurrencyAnimType animType = this.GetAnimType(currency.Currency);
  83. CurrencyAnimationSource currencyAnimationSource = this._animationSources.Find((CurrencyAnimationSource x) => x.AnimationSource == earnSource && x.CurrencyType == animType);
  84. if (currencyAnimationSource != null)
  85. {
  86. return currencyAnimationSource;
  87. }
  88. if (animType != CurrencyAnimType.Any)
  89. {
  90. currencyAnimationSource = this._animationSources.Find((CurrencyAnimationSource x) => x.AnimationSource == earnSource && x.CurrencyType == CurrencyAnimType.Any);
  91. }
  92. return currencyAnimationSource;
  93. }
  94. private void QueueAnimation(CurrencyValue currency, object earnSource)
  95. {
  96. this._animationQueue.Add(new CurrencyAnimator.EarnedCurrency(currency, earnSource));
  97. if (!this.IsInvoking(new Action(this.PlayAnimationQueue)))
  98. {
  99. this.InvokeNextFrame(new Action(this.PlayAnimationQueue));
  100. }
  101. }
  102. private void PlayAnimationQueue()
  103. {
  104. int count = this._animationQueue.Count;
  105. for (int i = 0; i < count; i++)
  106. {
  107. this.PlayAnimation(this._animationQueue[i]);
  108. }
  109. this._animationQueue.Clear();
  110. }
  111. private void PlayAnimation(CurrencyValue currency, object earnSource)
  112. {
  113. CurrencyAnimationSource currencyAnimationSource = this.FindAnimationSource(currency, earnSource);
  114. if (earnSource != null && currencyAnimationSource == null)
  115. {
  116. this.QueueAnimation(currency, earnSource);
  117. }
  118. else
  119. {
  120. this.PlayAnimation(currency, currencyAnimationSource);
  121. }
  122. }
  123. private void PlayAnimation(CurrencyAnimator.EarnedCurrency earnedCurrency)
  124. {
  125. CurrencyAnimationSource currencyAnimationSource = this.FindAnimationSource(earnedCurrency.Currency, earnedCurrency.EarnSource);
  126. if (currencyAnimationSource == null)
  127. {
  128. UnityEngine.Debug.LogWarningFormat("No CurrencyAnimationSource found for Earn Source '{0}' when trying to earn currency type '{1}'", new object[]
  129. {
  130. earnedCurrency.EarnSource,
  131. this.GetAnimType(earnedCurrency.Currency.Currency)
  132. });
  133. }
  134. this.PlayAnimation(earnedCurrency.Currency, currencyAnimationSource);
  135. }
  136. private void PlayAnimation(CurrencyValue currency, CurrencyAnimationSource animationSource = null)
  137. {
  138. Vector3 startPosition;
  139. if (animationSource != null)
  140. {
  141. startPosition = this.CalcSourcePosition(animationSource.PositionType, animationSource.Position);
  142. }
  143. else
  144. {
  145. startPosition = this._rectTransform.position;
  146. }
  147. FlyingCurrency flyingCurrencyPrefab = this.GetFlyingCurrencyPrefab(currency.Currency);
  148. FlyingCurrency flyingCurrency = UnityEngine.Object.Instantiate<FlyingCurrency>(flyingCurrencyPrefab, base.transform);
  149. flyingCurrency.gameObject.SetActive(true);
  150. flyingCurrency.PlayAnimation(currency, startPosition);
  151. }
  152. private CurrencyAnimType GetAnimType(string currencyName)
  153. {
  154. if (currencyName != null)
  155. {
  156. if (currencyName == "Cash")
  157. {
  158. return CurrencyAnimType.Cash;
  159. }
  160. if (currencyName == "Gold")
  161. {
  162. return CurrencyAnimType.Gold;
  163. }
  164. if (currencyName == "LevelUp" || currencyName == "XP")
  165. {
  166. return CurrencyAnimType.XP;
  167. }
  168. }
  169. return CurrencyAnimType.Any;
  170. }
  171. private FlyingCurrency GetFlyingCurrencyPrefab(string currencyName)
  172. {
  173. if (currencyName != null)
  174. {
  175. if (currencyName == "Cash")
  176. {
  177. return this._flyingCashPrefab;
  178. }
  179. if (currencyName == "Gold")
  180. {
  181. return this._flyingGoldPrefab;
  182. }
  183. if (currencyName == "XP")
  184. {
  185. return this._flyingXPPrefab;
  186. }
  187. if (currencyName == "LevelUp")
  188. {
  189. return this._flyingXPPrefab;
  190. }
  191. }
  192. UnityEngine.Debug.LogErrorFormat("Unknown currency: {0}", new object[]
  193. {
  194. currencyName
  195. });
  196. return this._flyingCashPrefab;
  197. }
  198. private void OnBalanceChanged(Currencies oldBalance, Currencies newBalance, object earnSource)
  199. {
  200. Currencies currencies = newBalance - oldBalance;
  201. this.PlayAnimations(currencies, earnSource);
  202. }
  203. [SerializeField]
  204. private Camera _uiCamera;
  205. [SerializeField]
  206. private FlyingCurrency _flyingCashPrefab;
  207. [SerializeField]
  208. private FlyingCurrency _flyingGoldPrefab;
  209. [SerializeField]
  210. private FlyingCurrency _flyingXPPrefab;
  211. private readonly List<CurrencyAnimationSource> _animationSources = new List<CurrencyAnimationSource>();
  212. private readonly List<CurrencyAnimator.EarnedCurrency> _animationQueue = new List<CurrencyAnimator.EarnedCurrency>();
  213. private RectTransform _rectTransform;
  214. private class EarnedCurrency
  215. {
  216. public EarnedCurrency(CurrencyValue currency, object earnSource)
  217. {
  218. this.Currency = currency;
  219. this.EarnSource = earnSource;
  220. }
  221. public CurrencyValue Currency { get; private set; }
  222. public object EarnSource { get; private set; }
  223. }
  224. }
  225. }