using asap.core; using cfg; using GameCore; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using TMPro; using UniRx; using UnityEngine; using UnityEngine.UI; /// /// hasBought: 0 未购买 1 已购买 /// public class ItemShopRecord { public int ItemID; public float Discount; public int hasBought; public ItemShopRecord(int itemID, float discount, int hasBought) { ItemID = itemID; this.Discount = discount; this.hasBought = hasBought; } } public class ItemsShopPanel : MonoBehaviour { #region Field&&Property [SerializeField] private GameObject go_refreshFree, go_refreshDiamonIcon, go_fishCardSlot, go_itemSlot; [SerializeField] private Transform tran_fishCardParent, tran_itemSlotParent; [SerializeField] private List _energyShopSlots = new List(); private List _itemShopSlots = new List(); [SerializeField] private TMP_Text txt_curMap, txt_itemShopTimer, txt_refreshCost; [SerializeField] private Button btn_chagneMap, btn_refreshGray, btn_refresh; private List _itemStores; private List _energySlotDatas; private List _fishCardSlotDatas; private PlayerShopData _shopData; private int curSlelectingMapID; private TimeSpan _autoRefreshTimer; #endregion IDisposable disposable; #region Function private void Awake() { go_itemSlot.SetActive(false); go_fishCardSlot.SetActive(false); var tables = GContext.container.Resolve(); _itemStores = tables.TbItemStore.DataList; _shopData = GContext.container.Resolve(); _energySlotDatas = tables.TbResourceStore.DataList.Where(x => x.Type == 1 && x.IsShow == 1).ToList(); _fishCardSlotDatas = tables.TbResourceStore.DataList.Where(x => x.Type == 2).ToList(); disposable = GContext.OnEvent().Where(x => x.IsConfirm).Subscribe(x => OnChangeMapID(x)); } protected void Start() { btn_refresh.onClick.AddListener(OnRefresh); btn_refreshGray.onClick.AddListener(OnCannotRefersh); btn_chagneMap.onClick.AddListener(OnChagneFishCardMap); RefreshView(); } protected void OnDestroy() { disposable?.Dispose(); disposable = null; btn_refresh.onClick.RemoveAllListeners(); } private void OnEnable() { StopAllCoroutines(); StartCoroutine(IE_SetTimer()); } private void RefreshView() { RefreshEnergySlots(); RefreshItemSlots(); RefreshRefreshInfo(); RefreshFishCardSlots(); StopAllCoroutines(); StartCoroutine(IE_SetTimer()); } private void RefreshItemBars() { RefreshItemSlots(); RefreshRefreshInfo(); StopAllCoroutines(); StartCoroutine(IE_SetTimer()); } private void RefreshFishCardSlots() { var curMapID = GContext.container.Resolve().currentMapId; var mapData = GContext.container.Resolve().GetMapData(curMapID); OnChangeMapID(new FishCardShopSlot.SChangeFishCardMapEvent { MapData = mapData }); for (int i = 0; i < _fishCardSlotDatas.Count; i++) { var fishCardSlot = Instantiate(go_fishCardSlot, tran_fishCardParent).GetComponent(); fishCardSlot.SetData(_fishCardSlotDatas[i], curMapID); } } private void OnChangeMapID(FishCardShopSlot.SChangeFishCardMapEvent data) { curSlelectingMapID = data.MapData.ID; txt_curMap.text = LocalizationMgr.GetText(data.MapData.Name_l10n_key); } private void RefreshEnergySlots() { for (int i = 0; i < _energySlotDatas.Count; i++) { if (i >= _energyShopSlots.Count) { Debug.LogError("EnergySLot Not Enough!"); return; } _energyShopSlots[i].SetData(_energySlotDatas[i]); } } private void RefreshItemSlots() { foreach (var slot in _itemShopSlots) { Destroy(slot.gameObject); } _itemShopSlots.Clear(); var shopDatas = _shopData.GetItemShopData(); foreach (var data in shopDatas) { var slot = Instantiate(go_itemSlot, tran_itemSlotParent).GetComponent(); slot.SetData(data); _itemShopSlots.Add(slot); } } private void RefreshRefreshInfo() { var cost = _shopData.GetDiamondShopRefreshCost(); if (cost == 0) { go_refreshFree.SetActive(true); go_refreshDiamonIcon.SetActive(false); txt_refreshCost.gameObject.SetActive(false); } else { go_refreshFree.SetActive(false); go_refreshDiamonIcon.SetActive(true); txt_refreshCost.gameObject.SetActive(true); txt_refreshCost.text = cost.ToString(); } var refreshInfo = _shopData.GetDiamondShopRefreshInfo(); if (refreshInfo.refresh <= 0) { btn_refreshGray.transform.parent.gameObject.SetActive(true); btn_refresh.transform.parent.gameObject.SetActive(false); } else { btn_refreshGray.transform.parent.gameObject.SetActive(false); btn_refresh.transform.parent.gameObject.SetActive(true); } } private void OnRefresh() { var diamondCount = GContext.container.Resolve().diamond; var cost = _shopData.GetDiamondShopRefreshCost(); if (diamondCount >= cost) { GContext.container.Resolve().AddDiamond(-cost); _shopData.RefreshDiamondShop(); RefreshItemBars(); #if AGG using (var e = GEvent.GameEvent("click_free_button")) { var refreshInfo = _shopData.GetDiamondShopRefreshInfo(); e.AddContent("cost_diamond_count", cost) .AddContent("click_free_count", refreshInfo.refresh); } #endif } else ToastPanel.Show(LocalizationMgr.GetText("UI_ToastPanel_59")); } private void OnCannotRefersh() { ToastPanel.Show(LocalizationMgr.GetText("UI_ToastPanel_64")); } private async void OnChagneFishCardMap() { var selectPanel = await UIManager.Instance.ShowUI(UITypes.FishingShopSelectMapPopupPanel); selectPanel.GetComponent().Init(curSlelectingMapID); } private void OnTimerEnd() { GContext.container.Resolve().AutoRefreshDiamonShop(); RefreshItemBars(); } private IEnumerator IE_SetTimer() { _autoRefreshTimer = _shopData.GetDiamondShopRefreshTime(); var miliSeconds = _autoRefreshTimer.Milliseconds; txt_itemShopTimer.text = LocalizationMgr.GetFormatTextValue("UI_COMMON_refresh", ConvertTools.ConvertTime2(_autoRefreshTimer)); yield return new WaitForSeconds(miliSeconds / 1000); _autoRefreshTimer.Subtract(TimeSpan.FromMilliseconds(miliSeconds)); txt_itemShopTimer.text = LocalizationMgr.GetFormatTextValue("UI_COMMON_refresh", ConvertTools.ConvertTime2(_autoRefreshTimer)); while (_autoRefreshTimer.TotalSeconds > 0) { yield return new WaitForSeconds(1); _autoRefreshTimer = _autoRefreshTimer.Subtract(TimeSpan.FromSeconds(1)); txt_itemShopTimer.text = LocalizationMgr.GetFormatTextValue("UI_COMMON_refresh", ConvertTools.ConvertTime2(_autoRefreshTimer)); } OnTimerEnd(); } #endregion }