|
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using CIG;
- using CIG.Translation;
- using CIGEnums;
- using SUISS.Core;
- using SUISSEngine;
- using UnityEngine;
- using UnityEngine.UI;
-
- public class BuildingShopItemsView : ShopItemsView
- {
- //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
- public event BuildingShopItemsView.BuildingShopItemClickedHandler BuildingShopItemClickedEvent;
-
- private void FireBuildingShopItemClickedEvent(CIGBuilding building)
- {
- if (this.BuildingShopItemClickedEvent != null)
- {
- this.BuildingShopItemClickedEvent(building);
- }
- }
-
- //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
- public event BuildingShopItemsView.TutorialDialogSetHandler TutorialDialogSetEvent;
-
- private void FireTutorialDialogSetEvent(ILocalizedString tutorialDialogLocalizedString)
- {
- if (this.TutorialDialogSetEvent != null)
- {
- this.TutorialDialogSetEvent(tutorialDialogLocalizedString);
- }
- }
-
- //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
- public event BuildingShopItemsView.TutorialDialogClearedHandler TutorialDialogClearedEvent;
-
- private void FireTutorialDialogClearedEvent()
- {
- if (this.TutorialDialogClearedEvent != null)
- {
- this.TutorialDialogClearedEvent();
- }
- }
-
- public override void Clear()
- {
- this.UpdateLastPosition();
- if (this._tutorialDialogActive)
- {
- this._scrollRect.horizontal = true;
- this.FireTutorialDialogClearedEvent();
- this._tutorialDialogActive = false;
- }
- if (this._tutorialArrowActive)
- {
- this._attentionArrow.transform.SetParent(base.transform);
- this._attentionArrow.SetActive(false);
- this._tutorialArrowActive = false;
- }
- this._buildings.Clear();
- this._recyclerView.PushInstances();
- }
-
- public override void Close()
- {
- this.UpdateLastPosition();
- }
-
- protected override void UpdateItems(ShopType category)
- {
- this._currentCategory = new ShopType?(category);
- switch (category)
- {
- case ShopType.Shop_residential:
- this._buildings = this.FilterBuildings(SingletonMonobehaviour<GameObjectManager>.Instance.Residential);
- this.SetTutorialDialog(CIGTutorialState.BuildHouse, category, Localization.Key("tutorial_build_house_grass"));
- break;
- case ShopType.Shop_commercial:
- this._buildings = this.FilterBuildings(SingletonMonobehaviour<GameObjectManager>.Instance.Commercial);
- this.SetTutorialDialog(CIGTutorialState.BuildFarm, category, Localization.Key("tutorial_build_commercial"));
- break;
- case ShopType.Shop_community:
- this._buildings = this.FilterBuildings(SingletonMonobehaviour<GameObjectManager>.Instance.Community);
- break;
- case ShopType.Shop_decorations:
- this._buildings = this.FilterBuildings(SingletonMonobehaviour<GameObjectManager>.Instance.Decoration);
- break;
- default:
- UnityEngine.Debug.LogWarning("[BuildingShopItemsView] No ShowBuildings implementation for category " + category);
- this._buildings = this.FilterBuildings(SingletonMonobehaviour<GameObjectManager>.Instance.Residential);
- break;
- }
- this._recyclerView.Init(this._buildings.Count, new Func<GameObject, int, bool>(this.InitShopItem));
- this._scrollRect.horizontalNormalizedPosition = ((!this._lastScrollPositionPerTab.ContainsKey(category)) ? 0f : this._lastScrollPositionPerTab[category]);
- }
-
- private void SetTutorialDialog(CIGTutorialState targetState, ShopType category, ILocalizedString tutorialText)
- {
- if (SingletonMonobehaviour<CIGTutorialManager>.Instance.state == targetState)
- {
- this._scrollRect.horizontal = false;
- this._lastScrollPositionPerTab[category] = 0f;
- this.FireTutorialDialogSetEvent(tutorialText);
- this._tutorialDialogActive = true;
- }
- }
-
- private List<CIGBuilding> FilterBuildings(IList<CIGBuilding> buildings)
- {
- int num = Mathf.Max(SingletonMonobehaviour<CIGGameState>.Instance.MaxLevel, 150);
- List<CIGBuilding> list = new List<CIGBuilding>();
- List<CIGBuilding> list2 = new List<CIGBuilding>();
- List<CIGBuilding> list3 = new List<CIGBuilding>();
- int count = buildings.Count;
- for (int i = 0; i < count; i++)
- {
- CIGBuilding cigbuilding = buildings[i];
- if (!cigbuilding.activatable)
- {
- if (cigbuilding.unlockLevels.Length == 0 || cigbuilding.unlockLevels[0] <= num)
- {
- int strictMax = cigbuilding.strictMax;
- if (strictMax < 0 || strictMax < SingletonMonobehaviour<CIGGameStats>.Instance.NumberOf(cigbuilding.CachedName))
- {
- if (cigbuilding.IsUnlocked)
- {
- list.Add(cigbuilding);
- if (cigbuilding.NextUnlockLevel != 2147483647)
- {
- list3.Add(cigbuilding);
- }
- }
- else if (cigbuilding.NextUnlockLevel != 2147483647)
- {
- list2.Add(cigbuilding);
- }
- }
- }
- }
- }
- CIGPurchasedBuildingsManager purchasedBuildings = SingletonMonobehaviour<CIGPurchasedBuildingsManager>.Instance;
- list.Sort(delegate(CIGBuilding a, CIGBuilding b)
- {
- bool flag = purchasedBuildings.IsUnconsumed(a);
- bool flag2 = purchasedBuildings.IsUnconsumed(b);
- if (flag && !flag2)
- {
- return -1;
- }
- if (!flag && flag2)
- {
- return 1;
- }
- int num4 = (a.unlockLevels.Length <= 0) ? 0 : a.unlockLevels[0];
- int num5 = (b.unlockLevels.Length <= 0) ? 0 : b.unlockLevels[0];
- return num4 - num5;
- });
- list2.Sort((CIGBuilding a, CIGBuilding b) => a.NextUnlockLevel - b.NextUnlockLevel);
- list3.Sort((CIGBuilding a, CIGBuilding b) => a.NextUnlockLevel - b.NextUnlockLevel);
- this._firstLockedIndex = list.Count;
- int num2 = 0;
- int num3 = 0;
- while (num2 < list2.Count || num3 < list3.Count)
- {
- if (num2 == list2.Count)
- {
- list.Add(list3[num3++]);
- }
- else if (num3 == list3.Count)
- {
- list.Add(list2[num2++]);
- }
- else
- {
- int nextUnlockLevel = list2[num2].NextUnlockLevel;
- int nextUnlockLevel2 = list3[num3].NextUnlockLevel;
- list.Add((nextUnlockLevel > nextUnlockLevel2) ? list3[num3++] : list2[num2++]);
- }
- }
- return list;
- }
-
- private bool InitShopItem(GameObject go, int index)
- {
- if (index < 0 || index >= this._buildings.Count)
- {
- return false;
- }
- BuildingShopItem buildingShopItem = this.GetBuildingShopItem(go);
- CIGBuilding building = this._buildings[index];
- buildingShopItem.Init(building, index >= this._firstLockedIndex, new Action<CIGBuilding>(this.OnBuildingShopItemClicked));
- if (!this._tutorialArrowActive && this.IsTutorialItem(building))
- {
- this._attentionArrow.transform.SetParent(buildingShopItem.transform);
- this._attentionArrow.transform.localPosition = Vector3.zero;
- this._attentionArrow.transform.localScale = Vector3.one;
- this._attentionArrow.SetActive(true);
- this._tutorialArrowActive = true;
- }
- return true;
- }
-
- private BuildingShopItem GetBuildingShopItem(GameObject go)
- {
- if (!this._buildingShopItems.ContainsKey(go))
- {
- this._buildingShopItems[go] = go.GetComponent<BuildingShopItem>();
- }
- return this._buildingShopItems[go];
- }
-
- private bool IsTutorialItem(CIGBuilding building)
- {
- CIGTutorialState state = SingletonMonobehaviour<CIGTutorialManager>.Instance.state;
- return (building.tile.requiredGridType == 1 && state == CIGTutorialState.BuildHouse && building is CIGResidentialBuilding) || (state == CIGTutorialState.BuildFarm && building is CIGCommercialBuilding);
- }
-
- private void OnBuildingShopItemClicked(CIGBuilding building)
- {
- this.FireBuildingShopItemClickedEvent(building);
- }
-
- private void UpdateLastPosition()
- {
- if (this._currentCategory != null)
- {
- this._lastScrollPositionPerTab[this._currentCategory.Value] = this._scrollRect.horizontalNormalizedPosition;
- this._currentCategory = null;
- }
- }
-
- private const SurfaceType TutorialSurfaceType = SurfaceType.Grass;
-
- [SerializeField]
- protected ScrollRect _scrollRect;
-
- [SerializeField]
- private RecyclerGridLayoutGroup _recyclerView;
-
- [SerializeField]
- private GameObject _attentionArrow;
-
- private List<CIGBuilding> _buildings = new List<CIGBuilding>();
-
- private Dictionary<GameObject, BuildingShopItem> _buildingShopItems = new Dictionary<GameObject, BuildingShopItem>();
-
- private int _firstLockedIndex = int.MaxValue;
-
- private bool _tutorialDialogActive;
-
- private bool _tutorialArrowActive;
-
- private ShopType? _currentCategory;
-
- private Dictionary<ShopType, float> _lastScrollPositionPerTab = new Dictionary<ShopType, float>();
-
- public delegate void BuildingShopItemClickedHandler(CIGBuilding building);
-
- public delegate void TutorialDialogSetHandler(ILocalizedString tutorialDialogLocalizedString);
-
- public delegate void TutorialDialogClearedHandler();
- }
|