25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

172 lines
5.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using CIG;
  5. using CIGEnums;
  6. using SUISS.Core;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. public class IAPShopItemsView : ShopItemsView
  10. {
  11. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  12. public event IAPShopItemsView.IAPShopItemClickedHandler IAPShopItemClickedEvent;
  13. private void FireIAPShopItemClickedEvent(CIG3StoreProduct storeProduct)
  14. {
  15. if (this.IAPShopItemClickedEvent != null)
  16. {
  17. this.IAPShopItemClickedEvent(storeProduct);
  18. }
  19. }
  20. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  21. public event IAPShopItemsView.CraneShopItemClickedHandler CraneShopItemClickedEvent;
  22. private void FireCraneShopItemClickedEvent(decimal goldCost)
  23. {
  24. if (this.CraneShopItemClickedEvent != null)
  25. {
  26. this.CraneShopItemClickedEvent(goldCost);
  27. }
  28. }
  29. private void OnDestroy()
  30. {
  31. if (this._store != null)
  32. {
  33. this._store.InitializedEvent -= this.OnStoreInitialized;
  34. this._store = null;
  35. }
  36. }
  37. public void Init()
  38. {
  39. this._store = SingletonMonobehaviour<CIGStoreManager>.Instance.Store;
  40. IAPStoreError loadingError = this._store.LoadingError;
  41. if (loadingError == IAPStoreError.ProductsNotLoaded || loadingError == IAPStoreError.StoreInitializing)
  42. {
  43. this._store.InitializedEvent += this.OnStoreInitialized;
  44. }
  45. this.UpdateIAPItems();
  46. }
  47. public override void Clear()
  48. {
  49. int count = this._shopItems.Count;
  50. for (int i = 0; i < count; i++)
  51. {
  52. UnityEngine.Object.Destroy(this._shopItems[i].gameObject);
  53. }
  54. this._shopItems.Clear();
  55. }
  56. protected override void UpdateItems(ShopType category)
  57. {
  58. this._category = category;
  59. if (this._category == ShopType.Shop_cranes)
  60. {
  61. decimal currentCraneGoldCost = SingletonMonobehaviour<CIGBuilderManager>.Instance.CurrentCraneGoldCost;
  62. if (currentCraneGoldCost > 0m)
  63. {
  64. CraneShopItem craneShopItem = UnityEngine.Object.Instantiate<CraneShopItem>(this._craneShopItemPrefab, this._itemLayoutGroup.transform);
  65. craneShopItem.Init(SingletonMonobehaviour<CIGBuilderManager>.Instance.CurrentCraneGoldCost, new Action<decimal>(this.OnCraneShopItemClicked));
  66. this._shopItems.Add(craneShopItem);
  67. }
  68. CIG3StoreProduct currentCraneIAP = SingletonMonobehaviour<CIGBuilderManager>.Instance.CurrentCraneIAP;
  69. if (currentCraneIAP != null)
  70. {
  71. this.InitIAPShopItem(this._category, 0, currentCraneIAP, null);
  72. }
  73. }
  74. else
  75. {
  76. List<CIG3StoreProduct> list = this._sortedStoreProducts[this._category];
  77. int count = list.Count;
  78. for (int i = count - 1; i >= 0; i--)
  79. {
  80. this.InitIAPShopItem(category, i, list[i], list[0]);
  81. }
  82. }
  83. }
  84. private void UpdateIAPItems()
  85. {
  86. foreach (KeyValuePair<ShopType, Predicate<CIG3StoreProduct>> keyValuePair in IAPShopItemsView.IAPShopMapping)
  87. {
  88. List<CIG3StoreProduct> list = new List<CIG3StoreProduct>();
  89. list.AddRange(this._store.GetProducts(keyValuePair.Value));
  90. list.Sort((CIG3StoreProduct a, CIG3StoreProduct b) => a.Price.CompareTo(b.Price));
  91. this._sortedStoreProducts[keyValuePair.Key] = list;
  92. }
  93. }
  94. private void InitIAPShopItem(ShopType category, int index, CIG3StoreProduct storeProduct, CIG3StoreProduct cheapestStoreProduct)
  95. {
  96. IAPShopItem iapshopItem = UnityEngine.Object.Instantiate<IAPShopItem>(this._iapShopItemPrefab, this._itemLayoutGroup.transform);
  97. iapshopItem.Init(storeProduct, category, index, new Action<CIG3StoreProduct>(this.OnIAPShopItemClicked), cheapestStoreProduct);
  98. this._shopItems.Add(iapshopItem);
  99. }
  100. private void OnStoreInitialized()
  101. {
  102. this._store.InitializedEvent -= this.OnStoreInitialized;
  103. this.UpdateIAPItems();
  104. }
  105. private void OnIAPShopItemClicked(CIG3StoreProduct storeProduct)
  106. {
  107. this.FireIAPShopItemClickedEvent(storeProduct);
  108. }
  109. private void OnCraneShopItemClicked(decimal goldCost)
  110. {
  111. this.FireCraneShopItemClickedEvent(goldCost);
  112. }
  113. private static readonly Dictionary<ShopType, Predicate<CIG3StoreProduct>> IAPShopMapping = new Dictionary<ShopType, Predicate<CIG3StoreProduct>>
  114. {
  115. {
  116. ShopType.Shop_gold,
  117. (CIG3StoreProduct p) => p.Category == StoreProductCategory.Shop && p.Currencies.Contains("Gold")
  118. },
  119. {
  120. ShopType.Shop_goldSale,
  121. (CIG3StoreProduct p) => p.Category == StoreProductCategory.ShopSale && p.Currencies.Contains("Gold")
  122. },
  123. {
  124. ShopType.Shop_cash,
  125. (CIG3StoreProduct p) => p.Category == StoreProductCategory.Shop && p.Currencies.Contains("Cash")
  126. },
  127. {
  128. ShopType.Shop_cashSale,
  129. (CIG3StoreProduct p) => p.Category == StoreProductCategory.ShopSale && p.Currencies.Contains("Cash")
  130. },
  131. {
  132. ShopType.Shop_cranes,
  133. (CIG3StoreProduct p) => p.Category == StoreProductCategory.Cranes
  134. }
  135. };
  136. [SerializeField]
  137. private IAPShopItem _iapShopItemPrefab;
  138. [SerializeField]
  139. private CraneShopItem _craneShopItemPrefab;
  140. [SerializeField]
  141. private LayoutGroup _itemLayoutGroup;
  142. private IAPStore<CIG3StoreProduct> _store;
  143. private Dictionary<ShopType, List<CIG3StoreProduct>> _sortedStoreProducts = new Dictionary<ShopType, List<CIG3StoreProduct>>();
  144. private ShopType _category;
  145. private List<ShopItem> _shopItems = new List<ShopItem>();
  146. public delegate void IAPShopItemClickedHandler(CIG3StoreProduct storeProduct);
  147. public delegate void CraneShopItemClickedHandler(decimal goldCost);
  148. }