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.
 
 
 

265 rivejä
8.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using CIG;
  5. using CIG.Translation;
  6. using CIGEnums;
  7. using SUISS.Core;
  8. using SUISSEngine;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. public class BuildingShopItemsView : ShopItemsView
  12. {
  13. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  14. public event BuildingShopItemsView.BuildingShopItemClickedHandler BuildingShopItemClickedEvent;
  15. private void FireBuildingShopItemClickedEvent(CIGBuilding building)
  16. {
  17. if (this.BuildingShopItemClickedEvent != null)
  18. {
  19. this.BuildingShopItemClickedEvent(building);
  20. }
  21. }
  22. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  23. public event BuildingShopItemsView.TutorialDialogSetHandler TutorialDialogSetEvent;
  24. private void FireTutorialDialogSetEvent(ILocalizedString tutorialDialogLocalizedString)
  25. {
  26. if (this.TutorialDialogSetEvent != null)
  27. {
  28. this.TutorialDialogSetEvent(tutorialDialogLocalizedString);
  29. }
  30. }
  31. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  32. public event BuildingShopItemsView.TutorialDialogClearedHandler TutorialDialogClearedEvent;
  33. private void FireTutorialDialogClearedEvent()
  34. {
  35. if (this.TutorialDialogClearedEvent != null)
  36. {
  37. this.TutorialDialogClearedEvent();
  38. }
  39. }
  40. public override void Clear()
  41. {
  42. this.UpdateLastPosition();
  43. if (this._tutorialDialogActive)
  44. {
  45. this._scrollRect.horizontal = true;
  46. this.FireTutorialDialogClearedEvent();
  47. this._tutorialDialogActive = false;
  48. }
  49. if (this._tutorialArrowActive)
  50. {
  51. this._attentionArrow.transform.SetParent(base.transform);
  52. this._attentionArrow.SetActive(false);
  53. this._tutorialArrowActive = false;
  54. }
  55. this._buildings.Clear();
  56. this._recyclerView.PushInstances();
  57. }
  58. public override void Close()
  59. {
  60. this.UpdateLastPosition();
  61. }
  62. protected override void UpdateItems(ShopType category)
  63. {
  64. this._currentCategory = new ShopType?(category);
  65. switch (category)
  66. {
  67. case ShopType.Shop_residential:
  68. this._buildings = this.FilterBuildings(SingletonMonobehaviour<GameObjectManager>.Instance.Residential);
  69. this.SetTutorialDialog(CIGTutorialState.BuildHouse, category, Localization.Key("tutorial_build_house_grass"));
  70. break;
  71. case ShopType.Shop_commercial:
  72. this._buildings = this.FilterBuildings(SingletonMonobehaviour<GameObjectManager>.Instance.Commercial);
  73. this.SetTutorialDialog(CIGTutorialState.BuildFarm, category, Localization.Key("tutorial_build_commercial"));
  74. break;
  75. case ShopType.Shop_community:
  76. this._buildings = this.FilterBuildings(SingletonMonobehaviour<GameObjectManager>.Instance.Community);
  77. break;
  78. case ShopType.Shop_decorations:
  79. this._buildings = this.FilterBuildings(SingletonMonobehaviour<GameObjectManager>.Instance.Decoration);
  80. break;
  81. default:
  82. UnityEngine.Debug.LogWarning("[BuildingShopItemsView] No ShowBuildings implementation for category " + category);
  83. this._buildings = this.FilterBuildings(SingletonMonobehaviour<GameObjectManager>.Instance.Residential);
  84. break;
  85. }
  86. this._recyclerView.Init(this._buildings.Count, new Func<GameObject, int, bool>(this.InitShopItem));
  87. this._scrollRect.horizontalNormalizedPosition = ((!this._lastScrollPositionPerTab.ContainsKey(category)) ? 0f : this._lastScrollPositionPerTab[category]);
  88. }
  89. private void SetTutorialDialog(CIGTutorialState targetState, ShopType category, ILocalizedString tutorialText)
  90. {
  91. if (SingletonMonobehaviour<CIGTutorialManager>.Instance.state == targetState)
  92. {
  93. this._scrollRect.horizontal = false;
  94. this._lastScrollPositionPerTab[category] = 0f;
  95. this.FireTutorialDialogSetEvent(tutorialText);
  96. this._tutorialDialogActive = true;
  97. }
  98. }
  99. private List<CIGBuilding> FilterBuildings(IList<CIGBuilding> buildings)
  100. {
  101. int num = Mathf.Max(SingletonMonobehaviour<CIGGameState>.Instance.MaxLevel, 150);
  102. List<CIGBuilding> list = new List<CIGBuilding>();
  103. List<CIGBuilding> list2 = new List<CIGBuilding>();
  104. List<CIGBuilding> list3 = new List<CIGBuilding>();
  105. int count = buildings.Count;
  106. for (int i = 0; i < count; i++)
  107. {
  108. CIGBuilding cigbuilding = buildings[i];
  109. if (!cigbuilding.activatable)
  110. {
  111. if (cigbuilding.unlockLevels.Length == 0 || cigbuilding.unlockLevels[0] <= num)
  112. {
  113. int strictMax = cigbuilding.strictMax;
  114. if (strictMax < 0 || strictMax < SingletonMonobehaviour<CIGGameStats>.Instance.NumberOf(cigbuilding.CachedName))
  115. {
  116. if (cigbuilding.IsUnlocked)
  117. {
  118. list.Add(cigbuilding);
  119. if (cigbuilding.NextUnlockLevel != 2147483647)
  120. {
  121. list3.Add(cigbuilding);
  122. }
  123. }
  124. else if (cigbuilding.NextUnlockLevel != 2147483647)
  125. {
  126. list2.Add(cigbuilding);
  127. }
  128. }
  129. }
  130. }
  131. }
  132. CIGPurchasedBuildingsManager purchasedBuildings = SingletonMonobehaviour<CIGPurchasedBuildingsManager>.Instance;
  133. list.Sort(delegate(CIGBuilding a, CIGBuilding b)
  134. {
  135. bool flag = purchasedBuildings.IsUnconsumed(a);
  136. bool flag2 = purchasedBuildings.IsUnconsumed(b);
  137. if (flag && !flag2)
  138. {
  139. return -1;
  140. }
  141. if (!flag && flag2)
  142. {
  143. return 1;
  144. }
  145. int num4 = (a.unlockLevels.Length <= 0) ? 0 : a.unlockLevels[0];
  146. int num5 = (b.unlockLevels.Length <= 0) ? 0 : b.unlockLevels[0];
  147. return num4 - num5;
  148. });
  149. list2.Sort((CIGBuilding a, CIGBuilding b) => a.NextUnlockLevel - b.NextUnlockLevel);
  150. list3.Sort((CIGBuilding a, CIGBuilding b) => a.NextUnlockLevel - b.NextUnlockLevel);
  151. this._firstLockedIndex = list.Count;
  152. int num2 = 0;
  153. int num3 = 0;
  154. while (num2 < list2.Count || num3 < list3.Count)
  155. {
  156. if (num2 == list2.Count)
  157. {
  158. list.Add(list3[num3++]);
  159. }
  160. else if (num3 == list3.Count)
  161. {
  162. list.Add(list2[num2++]);
  163. }
  164. else
  165. {
  166. int nextUnlockLevel = list2[num2].NextUnlockLevel;
  167. int nextUnlockLevel2 = list3[num3].NextUnlockLevel;
  168. list.Add((nextUnlockLevel > nextUnlockLevel2) ? list3[num3++] : list2[num2++]);
  169. }
  170. }
  171. return list;
  172. }
  173. private bool InitShopItem(GameObject go, int index)
  174. {
  175. if (index < 0 || index >= this._buildings.Count)
  176. {
  177. return false;
  178. }
  179. BuildingShopItem buildingShopItem = this.GetBuildingShopItem(go);
  180. CIGBuilding building = this._buildings[index];
  181. buildingShopItem.Init(building, index >= this._firstLockedIndex, new Action<CIGBuilding>(this.OnBuildingShopItemClicked));
  182. if (!this._tutorialArrowActive && this.IsTutorialItem(building))
  183. {
  184. this._attentionArrow.transform.SetParent(buildingShopItem.transform);
  185. this._attentionArrow.transform.localPosition = Vector3.zero;
  186. this._attentionArrow.transform.localScale = Vector3.one;
  187. this._attentionArrow.SetActive(true);
  188. this._tutorialArrowActive = true;
  189. }
  190. return true;
  191. }
  192. private BuildingShopItem GetBuildingShopItem(GameObject go)
  193. {
  194. if (!this._buildingShopItems.ContainsKey(go))
  195. {
  196. this._buildingShopItems[go] = go.GetComponent<BuildingShopItem>();
  197. }
  198. return this._buildingShopItems[go];
  199. }
  200. private bool IsTutorialItem(CIGBuilding building)
  201. {
  202. CIGTutorialState state = SingletonMonobehaviour<CIGTutorialManager>.Instance.state;
  203. return (building.tile.requiredGridType == 1 && state == CIGTutorialState.BuildHouse && building is CIGResidentialBuilding) || (state == CIGTutorialState.BuildFarm && building is CIGCommercialBuilding);
  204. }
  205. private void OnBuildingShopItemClicked(CIGBuilding building)
  206. {
  207. this.FireBuildingShopItemClickedEvent(building);
  208. }
  209. private void UpdateLastPosition()
  210. {
  211. if (this._currentCategory != null)
  212. {
  213. this._lastScrollPositionPerTab[this._currentCategory.Value] = this._scrollRect.horizontalNormalizedPosition;
  214. this._currentCategory = null;
  215. }
  216. }
  217. private const SurfaceType TutorialSurfaceType = SurfaceType.Grass;
  218. [SerializeField]
  219. protected ScrollRect _scrollRect;
  220. [SerializeField]
  221. private RecyclerGridLayoutGroup _recyclerView;
  222. [SerializeField]
  223. private GameObject _attentionArrow;
  224. private List<CIGBuilding> _buildings = new List<CIGBuilding>();
  225. private Dictionary<GameObject, BuildingShopItem> _buildingShopItems = new Dictionary<GameObject, BuildingShopItem>();
  226. private int _firstLockedIndex = int.MaxValue;
  227. private bool _tutorialDialogActive;
  228. private bool _tutorialArrowActive;
  229. private ShopType? _currentCategory;
  230. private Dictionary<ShopType, float> _lastScrollPositionPerTab = new Dictionary<ShopType, float>();
  231. public delegate void BuildingShopItemClickedHandler(CIGBuilding building);
  232. public delegate void TutorialDialogSetHandler(ILocalizedString tutorialDialogLocalizedString);
  233. public delegate void TutorialDialogClearedHandler();
  234. }