您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

234 行
5.8 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using CIG.Translation;
  5. using CIGEnums;
  6. using SUISS.Core;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. public class HUDSaleButton : MonoBehaviour
  10. {
  11. private void OnEnable()
  12. {
  13. this.TrySubscribeBOBGOFManger();
  14. this.TrySubscribeSaleManager();
  15. this.UpdateButton();
  16. base.StartCoroutine(this._updateTimeRemainingRoutine = this.TickTimer());
  17. }
  18. private void OnDisable()
  19. {
  20. base.StopCoroutine(this._updateTimeRemainingRoutine);
  21. this.UnsubscribeManagers();
  22. }
  23. public void OnSaleClicked()
  24. {
  25. if (this.IsBOBGOFSaleActive)
  26. {
  27. SingletonMonobehaviour<PopupManager>.Instance.RequestPopup<BuyOneBuildingGetOneFreePopupState>(null);
  28. return;
  29. }
  30. CIGSaleManager.Sale activeSale = this.GetActiveSale();
  31. if (activeSale != null && activeSale.Cash && !activeSale.Gold)
  32. {
  33. SingletonMonobehaviour<PopupManager>.Instance.OpenShopMenu(ShopMenuTabs.Cash);
  34. return;
  35. }
  36. SingletonMonobehaviour<PopupManager>.Instance.OpenShopMenu(ShopMenuTabs.Gold);
  37. }
  38. private bool IsBOBGOFSaleActive
  39. {
  40. get
  41. {
  42. return this._bobgofManager != null && this._bobgofManager.State == CIGBuyOneBuildingGetOneFreeManager.BuyOneBuildingGetOneFreeState.Active;
  43. }
  44. }
  45. private void TrySubscribeBOBGOFManger()
  46. {
  47. if (!this._isSubscribedToBOBGOFManger && SingletonMonobehaviour<CIGBuyOneBuildingGetOneFreeManager>.IsAvailable)
  48. {
  49. this._bobgofManager = SingletonMonobehaviour<CIGBuyOneBuildingGetOneFreeManager>.Instance;
  50. this._bobgofManager.StateChangedEvent += this.OnBOBGOFChanged;
  51. this._isSubscribedToBOBGOFManger = true;
  52. }
  53. }
  54. private void TrySubscribeSaleManager()
  55. {
  56. if (!this._isSubscribedToSaleManger && SingletonMonobehaviour<CIGSaleManager>.IsAvailable)
  57. {
  58. this._saleManager = SingletonMonobehaviour<CIGSaleManager>.Instance;
  59. this._saleManager.SaleChanged += this.OnSaleChanged;
  60. this._isSubscribedToSaleManger = true;
  61. }
  62. }
  63. private void UnsubscribeManagers()
  64. {
  65. if (this._isSubscribedToSaleManger)
  66. {
  67. this._saleManager.SaleChanged -= this.OnSaleChanged;
  68. this._saleManager = null;
  69. this._isSubscribedToSaleManger = false;
  70. }
  71. if (this._isSubscribedToBOBGOFManger)
  72. {
  73. this._bobgofManager.StateChangedEvent -= this.OnBOBGOFChanged;
  74. this._bobgofManager = null;
  75. this._isSubscribedToBOBGOFManger = false;
  76. }
  77. }
  78. private void UpdateButton()
  79. {
  80. HUDSaleButtonType hudsaleButtonType = HUDSaleButtonType.NoSale;
  81. if (this.IsBOBGOFSaleActive)
  82. {
  83. if (this._bobgofManager.PaidBuilding != null)
  84. {
  85. hudsaleButtonType = HUDSaleButtonType.BOGOFSale;
  86. }
  87. else
  88. {
  89. UnityEngine.Debug.LogErrorFormat("BOBGOFManager.PaidBuilding is null but there is a sale active. I don't know how to handle this.", new object[0]);
  90. }
  91. ILocalizedString localizedString = Localization.Integer(1);
  92. this._dealLabel.LocalizedString = Localization.Concat(new ILocalizedString[]
  93. {
  94. localizedString,
  95. Localization.Key("CURRENCY_APPEND_PLUS"),
  96. localizedString
  97. });
  98. }
  99. else
  100. {
  101. CIGSaleManager.Sale activeSale = this.GetActiveSale();
  102. if (activeSale != null)
  103. {
  104. if (activeSale.Gold && activeSale.Cash)
  105. {
  106. hudsaleButtonType = HUDSaleButtonType.GoldCashSale;
  107. }
  108. else if (activeSale.Gold)
  109. {
  110. hudsaleButtonType = HUDSaleButtonType.GoldSale;
  111. }
  112. else if (activeSale.Cash)
  113. {
  114. hudsaleButtonType = HUDSaleButtonType.CashSale;
  115. }
  116. this._dealLabel.LocalizedString = Localization.Percentage(200);
  117. }
  118. }
  119. int count = this._saleIconObjects.Count;
  120. for (int i = 0; i < count; i++)
  121. {
  122. HUDSaleButton.HUDSaleButtonIcon hudsaleButtonIcon = this._saleIconObjects[i];
  123. hudsaleButtonIcon.GameObject.SetActive(hudsaleButtonIcon.Type == hudsaleButtonType);
  124. }
  125. bool flag = hudsaleButtonType != HUDSaleButtonType.NoSale;
  126. this._saleActiveContainer.SetActive(flag);
  127. this._saleInactiveContainer.SetActive(!flag);
  128. this.UpdateTimer();
  129. }
  130. private CIGSaleManager.Sale GetActiveSale()
  131. {
  132. return (!(this._saleManager != null)) ? null : this._saleManager.CurrentSale;
  133. }
  134. private void OnBOBGOFChanged()
  135. {
  136. this.UpdateButton();
  137. }
  138. private void OnSaleChanged(CIGSaleManager.Sale oldSale, CIGSaleManager.Sale newSale)
  139. {
  140. this.UpdateButton();
  141. }
  142. private void UpdateTimer()
  143. {
  144. if (this.IsBOBGOFSaleActive && this._bobgofManager.SaleTimeLeft > TimeSpan.Zero)
  145. {
  146. this._saleTimerLabel.LocalizedString = Localization.TimeSpan(this._bobgofManager.SaleTimeLeft, false);
  147. }
  148. else
  149. {
  150. CIGSaleManager.Sale activeSale = this.GetActiveSale();
  151. if (activeSale != null)
  152. {
  153. this._saleTimerLabel.LocalizedString = Localization.TimeSpan(activeSale.SaleTimeLeft, false);
  154. }
  155. }
  156. }
  157. private IEnumerator TickTimer()
  158. {
  159. for (;;)
  160. {
  161. this.UpdateTimer();
  162. yield return new WaitForSecondsRealtime(1f);
  163. }
  164. yield break;
  165. }
  166. [SerializeField]
  167. private Image _saleIcon;
  168. [SerializeField]
  169. private GameObject _saleActiveContainer;
  170. [SerializeField]
  171. private GameObject _saleInactiveContainer;
  172. [SerializeField]
  173. private LocalizedText _saleTimerLabel;
  174. [SerializeField]
  175. private LocalizedText _dealLabel;
  176. [SerializeField]
  177. private List<HUDSaleButton.HUDSaleButtonIcon> _saleIconObjects;
  178. private bool _isSubscribedToBOBGOFManger;
  179. private CIGBuyOneBuildingGetOneFreeManager _bobgofManager;
  180. private bool _isSubscribedToSaleManger;
  181. private CIGSaleManager _saleManager;
  182. private IEnumerator _updateTimeRemainingRoutine;
  183. [Serializable]
  184. public class HUDSaleButtonIcon
  185. {
  186. public HUDSaleButtonType Type
  187. {
  188. get
  189. {
  190. return this._type;
  191. }
  192. }
  193. public GameObject GameObject
  194. {
  195. get
  196. {
  197. return this._gameObject;
  198. }
  199. }
  200. [SerializeField]
  201. private HUDSaleButtonType _type;
  202. [SerializeField]
  203. public GameObject _gameObject;
  204. }
  205. }