You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

304 lines
8.1 KiB

  1. using System;
  2. using CIG;
  3. using CIG.Translation;
  4. using CIGEnums;
  5. using Engine.DependencyTree;
  6. using SUISS.Core;
  7. using SUISSEngine;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. public class QuestItem : MonoBehaviour
  11. {
  12. public void OnDestroy()
  13. {
  14. this.Deinit();
  15. }
  16. public void Initialize(Dependency quest)
  17. {
  18. this.Deinit();
  19. this._currencyAnimationSource.Init(this._currencyAnimationObject);
  20. this._quest = quest;
  21. if (this._quest != null)
  22. {
  23. this._quest.AchievedChangedEvent += this.OnAchievedChanged;
  24. }
  25. if (this.IsVideoQuest)
  26. {
  27. SingletonMonobehaviour<VideoAds1Manager>.Instance.AvailabilityChangedEvent += this.OnVideoAvailabilityChanged;
  28. }
  29. this.ShowQuest();
  30. }
  31. public void OnClaimRewardClicked()
  32. {
  33. if (this._level != null && !this._level.RewardClaimed && this._level.IsAchieved)
  34. {
  35. Currencies currencies = this._level.Reward;
  36. if (this._quest.IsAchieved && !this._quest.RewardClaimed)
  37. {
  38. currencies += this._quest.Reward;
  39. }
  40. if (!currencies.IsEmpty())
  41. {
  42. CIGGameState instance = SingletonMonobehaviour<CIGGameState>.Instance;
  43. instance.EarnCurrencies(currencies, this._currencyAnimationObject);
  44. ServiceLocator.Find<Messenger>().Invoke<InterestingRatingTriggerEvent<CIGInterestingRatingTriggerEventType>>(new InterestingRatingTriggerEvent<CIGInterestingRatingTriggerEventType>(CIGInterestingRatingTriggerEventType.ClaimedAchievementAward));
  45. SingletonMonobehaviour<CIGAudioManager>.Instance.PlayClip(Clip.Victory);
  46. }
  47. this._level.RewardClaimed = true;
  48. if (this._quest.IsAchieved)
  49. {
  50. this._quest.RewardClaimed = true;
  51. if (SingletonMonobehaviour<CIGDailyQuestManager>.Instance.IsActiveDailyQuest(this._quest))
  52. {
  53. }
  54. }
  55. if (this._quest.IsAchieved)
  56. {
  57. this.ShowQuest();
  58. }
  59. else
  60. {
  61. this.FindCurrentLevel();
  62. }
  63. }
  64. else if (this.IsVideoQuest)
  65. {
  66. SingletonMonobehaviour<VideoAds1Manager>.Instance.ShowVideoQuestAd();
  67. }
  68. }
  69. private void Deinit()
  70. {
  71. this._currencyAnimationSource.Deinit();
  72. if (this._quest != null)
  73. {
  74. this._quest.AchievedChangedEvent -= this.OnAchievedChanged;
  75. }
  76. if (this._level != null)
  77. {
  78. this._level.AchievedChangedEvent -= this.OnAchievedChanged;
  79. this._level.ProgressValueChangedEvent -= this.OnProgressChanged;
  80. }
  81. if (SingletonMonobehaviour<VideoAds1Manager>.IsAvailable)
  82. {
  83. SingletonMonobehaviour<VideoAds1Manager>.Instance.AvailabilityChangedEvent -= this.OnVideoAvailabilityChanged;
  84. }
  85. this._quest = null;
  86. this._level = null;
  87. }
  88. private void OnVideoAvailabilityChanged()
  89. {
  90. if (this.IsVideoQuest && (!this._level.IsAchieved || this._level.RewardClaimed))
  91. {
  92. this._claimRewardButton.interactable = SingletonMonobehaviour<VideoAds1Manager>.Instance.IsReady;
  93. }
  94. }
  95. private void FindCurrentLevel()
  96. {
  97. if (this._level != null)
  98. {
  99. this._level.AchievedChangedEvent -= this.OnAchievedChanged;
  100. this._level.ProgressValueChangedEvent -= this.OnProgressChanged;
  101. }
  102. this._level = null;
  103. if (this._quest != null)
  104. {
  105. foreach (Dependency dependency in this._quest.Children)
  106. {
  107. if (!dependency.IsAchieved || !dependency.RewardClaimed)
  108. {
  109. this._level = dependency;
  110. break;
  111. }
  112. }
  113. if (this._level == null && this._quest.Children.Count > 0)
  114. {
  115. this._level = this._quest.Children[this._quest.Children.Count - 1];
  116. }
  117. }
  118. if (this._level != null)
  119. {
  120. this._level.AchievedChangedEvent += this.OnAchievedChanged;
  121. this._level.ProgressValueChangedEvent += this.OnProgressChanged;
  122. }
  123. this.ShowLevel();
  124. }
  125. private void ShowQuest()
  126. {
  127. this._questIcon.sprite = SingletonMonobehaviour<QuestSpriteAssetCollection>.Instance.GetAsset(this._quest.SpriteName);
  128. this.FindCurrentLevel();
  129. }
  130. private void ShowLevel()
  131. {
  132. if (this._level != null)
  133. {
  134. Dependency dependency = this._level;
  135. if (!dependency.HasTitle)
  136. {
  137. dependency = this._quest;
  138. }
  139. string text;
  140. if (!dependency.HasTitle)
  141. {
  142. text = string.Empty;
  143. }
  144. else if (dependency.IsQuantityTitle)
  145. {
  146. text = ((!CIGUtilities.IsSingular(this._level.ProgressMaximumValue)) ? dependency.TitlePlural : dependency.TitleSingular);
  147. }
  148. else
  149. {
  150. text = dependency.Title;
  151. }
  152. this._nameText.LocalizedString = ((!string.IsNullOrEmpty(text)) ? Localization.Format(Localization.Key(text), this._level.TitleArguments) : Localization.EmptyLocalizedString);
  153. int num;
  154. int progressMaximumValue;
  155. if (this._level.IsAchieved)
  156. {
  157. num = this._level.ProgressMaximumValue;
  158. progressMaximumValue = this._level.ProgressMaximumValue;
  159. }
  160. else
  161. {
  162. num = this._level.ProgressValue;
  163. progressMaximumValue = this._level.ProgressMaximumValue;
  164. }
  165. this._progressBarSlider.value = (float)num / (float)progressMaximumValue;
  166. int i = Mathf.FloorToInt((float)num / this._level.ValueModifier);
  167. int i2 = Mathf.FloorToInt((float)progressMaximumValue / this._level.ValueModifier);
  168. this._progressText.LocalizedString = Localization.Format(Localization.Key("progress"), new ILocalizedString[]
  169. {
  170. Localization.Integer(i),
  171. Localization.Integer(i2)
  172. });
  173. this._progressBarFillImage.sprite = ((!this._level.IsAchieved) ? this._progressFillInProgress : this._progressFillCompleted);
  174. if (this._level.IsAchieved && !this._level.RewardClaimed)
  175. {
  176. this.EnableClaimButton();
  177. }
  178. else
  179. {
  180. this.DisableClaimButton();
  181. }
  182. this.UpdateReward(this._level.Reward);
  183. if (this._achievedQuestIcon != null)
  184. {
  185. this._achievedQuestIcon.gameObject.SetActive(this._level.IsAchieved && this._level.RewardClaimed);
  186. }
  187. }
  188. }
  189. private void UpdateReward(Currencies reward)
  190. {
  191. bool flag = CIGUtilities.HasCash(reward);
  192. bool flag2 = CIGUtilities.HasGold(reward);
  193. this._rewardText.LocalizedString = Localization.Integer(reward.GetValue((!flag) ? ((!flag2) ? "XP" : "Gold") : "Cash"));
  194. Sprite asset = SingletonMonobehaviour<UISpriteAssetCollection>.Instance.GetAsset(UISpriteType.XPSmall);
  195. if (flag)
  196. {
  197. asset = SingletonMonobehaviour<UISpriteAssetCollection>.Instance.GetAsset(UISpriteType.CashSmall);
  198. }
  199. else if (flag2)
  200. {
  201. asset = SingletonMonobehaviour<UISpriteAssetCollection>.Instance.GetAsset(UISpriteType.GoldSmall);
  202. }
  203. this._rewardIcon.sprite = asset;
  204. }
  205. private void OnAchievedChanged(Dependency dependency, bool achieved)
  206. {
  207. if (dependency == this._quest)
  208. {
  209. this.ShowQuest();
  210. }
  211. else if (dependency == this._level)
  212. {
  213. this.ShowLevel();
  214. }
  215. }
  216. private void OnProgressChanged(Dependency dependency, int progress)
  217. {
  218. this.ShowLevel();
  219. }
  220. private void DisableClaimButton()
  221. {
  222. this._claimRewardObject.SetActive(false);
  223. this._rays.SetActive(false);
  224. this._claimRewardButton.interactable = (this.IsVideoQuest && SingletonMonobehaviour<VideoAds1Manager>.Instance.IsReady);
  225. }
  226. private void EnableClaimButton()
  227. {
  228. this._claimRewardObject.SetActive(true);
  229. this._rays.SetActive(true);
  230. this._claimRewardButton.interactable = true;
  231. }
  232. private bool IsVideoQuest
  233. {
  234. get
  235. {
  236. return this._quest != null && this._quest.Identifier.Contains("videos_gold");
  237. }
  238. }
  239. [Header("Customizations")]
  240. [SerializeField]
  241. private Sprite _progressFillInProgress;
  242. [SerializeField]
  243. private Sprite _progressFillCompleted;
  244. [Header("Links")]
  245. [SerializeField]
  246. private LocalizedText _nameText;
  247. [SerializeField]
  248. private LocalizedText _rewardText;
  249. [SerializeField]
  250. private Image _questIcon;
  251. [SerializeField]
  252. private Image _achievedQuestIcon;
  253. [SerializeField]
  254. private LocalizedText _progressText;
  255. [SerializeField]
  256. private Slider _progressBarSlider;
  257. [SerializeField]
  258. private Image _progressBarFillImage;
  259. [SerializeField]
  260. private Image _rewardIcon;
  261. [SerializeField]
  262. private GameObject _claimRewardObject;
  263. [SerializeField]
  264. private Button _claimRewardButton;
  265. [SerializeField]
  266. private GameObject _rays;
  267. [SerializeField]
  268. private CurrencyAnimationSource _currencyAnimationSource;
  269. private Dependency _quest;
  270. private Dependency _level;
  271. private object _currencyAnimationObject = new object();
  272. }