255 lines
7.3 KiB
C#
255 lines
7.3 KiB
C#
using System.Collections.Generic;
|
|
using game;
|
|
using UnityEngine;
|
|
|
|
public class BaseMarketSectionUI : MonoBehaviour
|
|
{
|
|
public static string SectionPrefab => "UI/Templates/DefaultSection";
|
|
|
|
private static string PageTemplate => "UI/Templates/PageTemplate";
|
|
|
|
public bool NeedPurchaseConfirmation { get; set; }
|
|
private BaseShop Shop { get; set; }
|
|
|
|
private ShopPageUI _pageTemplate;
|
|
private Transform _pagesContainer;
|
|
private List<ShopPageUI> _pages = new List<ShopPageUI>();
|
|
|
|
private ConfBaseShopItem _selectedItem;
|
|
private BaseItemPreviewer _itemPreviewer;
|
|
|
|
private ConfirmationWindow _confirmationWindow;
|
|
private ItemClickResult _itemClickResult;
|
|
|
|
public void Init()
|
|
{
|
|
_pagesContainer = transform.Find("View/Content");
|
|
_pageTemplate = Assets.Load<ShopPageUI>(PageTemplate);
|
|
}
|
|
|
|
public void SetShop(BaseShop shop)
|
|
{
|
|
Shop = shop;
|
|
FillItems();
|
|
}
|
|
|
|
public void SetPreviewContainer(Transform container)
|
|
{
|
|
_itemPreviewer = FactorySectionItemPreview.CreateItemPreview(Shop.Type);
|
|
_itemPreviewer.gameObject.SetParent(container);
|
|
|
|
SyncShopItemPreview();
|
|
}
|
|
|
|
private void FillItems()
|
|
{
|
|
int itemsCount = Shop.ItemsCount;
|
|
for (int i = 0; i < itemsCount; i++)
|
|
AddItem(Shop.GetItemAt(i));
|
|
|
|
// SyncShopItemsView();
|
|
}
|
|
|
|
private void AddItem(ConfBaseShopItem confItem)
|
|
{
|
|
ShopPageUI pageForItem = GetOrCreatePageForNextItem();
|
|
pageForItem.SetItemStateGetter(Shop.GetItemShopState);
|
|
pageForItem.SetItemClickAction(OnItemClick);
|
|
pageForItem.AddItem(confItem);
|
|
}
|
|
|
|
private ShopPageUI GetOrCreatePageForNextItem()
|
|
{
|
|
if (_pages.Count > 0 && _pages.Last().CanAddItem())
|
|
return _pages.Last();
|
|
|
|
ShopPageUI page = Instantiate(_pageTemplate, _pagesContainer);
|
|
page.Init();
|
|
page.gameObject.SetActive(true);
|
|
_pages.Add(page);
|
|
return page;
|
|
}
|
|
|
|
private void SyncSelectedItemConfInUI()
|
|
{
|
|
foreach (ShopPageUI page in _pages)
|
|
{
|
|
if (page.HaveItemWithConf(_selectedItem))
|
|
page.SetSelectedItem(_selectedItem);
|
|
else
|
|
page.ResetSelectedItem();
|
|
}
|
|
}
|
|
|
|
private bool TryGetSelectedItem(out uint itemId)
|
|
{
|
|
foreach (ShopPageUI page in _pages)
|
|
{
|
|
if (page.TryGetSelectedItemId(out itemId))
|
|
return true;
|
|
}
|
|
|
|
itemId = 0;
|
|
return false;
|
|
}
|
|
|
|
private void OnItemClick(ConfBaseShopItem itemConf, ItemClickResult result)
|
|
{
|
|
ItemSaveState itemState = Shop.GetStateForItem(itemConf);
|
|
|
|
EnumClickResult clickResult = EnumClickResult.None;
|
|
if (TryGetSelectedItem(out uint itemId))
|
|
{
|
|
bool wasSelected = itemConf.itemAmount.protoId == itemId;
|
|
if (wasSelected)
|
|
{
|
|
if (itemState.IsEquipped)
|
|
clickResult = EnumClickResult.None;
|
|
else if (itemState.IsHas)
|
|
{
|
|
Shop.TryEquipItem(itemConf);
|
|
clickResult = EnumClickResult.Equipped;
|
|
}
|
|
else
|
|
{
|
|
if (Shop.HaveMoney(itemConf.price))
|
|
{
|
|
if (NeedPurchaseConfirmation)
|
|
{
|
|
_itemClickResult = result;
|
|
OpenConfirmationWindow(itemConf);
|
|
return;
|
|
}
|
|
|
|
PurchaseAndEquipItem(itemConf);
|
|
clickResult = EnumClickResult.Purchased;
|
|
}
|
|
else
|
|
clickResult = EnumClickResult.NoMoney;
|
|
}
|
|
}
|
|
else if (itemState.IsHas && !itemState.IsEquipped)
|
|
{
|
|
Shop.TryEquipItem(itemConf);
|
|
clickResult = EnumClickResult.Equipped;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (itemState.IsEquipped)
|
|
clickResult = EnumClickResult.None;
|
|
else if (itemState.IsHas)
|
|
{
|
|
Shop.TryEquipItem(itemConf);
|
|
clickResult = EnumClickResult.Equipped;
|
|
}
|
|
}
|
|
|
|
result?.Invoke(clickResult);
|
|
ChangeSelectedItem(itemConf);
|
|
SyncShopItemsView();
|
|
}
|
|
|
|
private void OpenConfirmationWindow(ConfBaseShopItem purchasingItem)
|
|
{
|
|
_confirmationWindow = UI.GetOrCreateWindow<ConfirmationWindow>();
|
|
_confirmationWindow.SetItemForBuy(purchasingItem);
|
|
_confirmationWindow.OnHaveResult += ConfirmationWindowResult;
|
|
_confirmationWindow.Show();
|
|
}
|
|
|
|
private void ConfirmationWindowResult(ConfBaseShopItem item, EnumClickResult result)
|
|
{
|
|
_confirmationWindow.OnHaveResult -= ConfirmationWindowResult;
|
|
if (result == EnumClickResult.Purchased)
|
|
PurchaseAndEquipItem(item);
|
|
|
|
_itemClickResult?.Invoke(result);
|
|
}
|
|
|
|
private void PurchaseAndEquipItem(ConfBaseShopItem item)
|
|
{
|
|
Shop.TryBuyItem(item);
|
|
Shop.TryEquipItem(item);
|
|
SyncShopItemsView();
|
|
}
|
|
|
|
private void SelectEquipmentOrFirstItem()
|
|
{
|
|
ConfBaseShopItem selectionItem = GetEquipmentItem();
|
|
if (selectionItem == null)
|
|
selectionItem = Shop.Items[0];
|
|
|
|
ChangeSelectedItem(selectionItem);
|
|
}
|
|
|
|
private ConfBaseShopItem GetEquipmentItem()
|
|
{
|
|
foreach (var item in Shop.Items)
|
|
{
|
|
if (Shop.GetItemShopState(item) == ItemShopState.Equipment)
|
|
return item;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private void ChangeSelectedItem(ConfBaseShopItem itemConf, bool withUpdate = true)
|
|
{
|
|
_selectedItem = itemConf;
|
|
|
|
SyncShopItemPreview();
|
|
if (withUpdate)
|
|
SyncSelectedItemConfInUI();
|
|
}
|
|
|
|
private void SyncShopItemsView()
|
|
{
|
|
foreach (ShopItemUI itemUI in Items())
|
|
itemUI.SyncView();
|
|
}
|
|
|
|
private void SyncShopItemPreview()
|
|
{
|
|
if (_itemPreviewer != null && _selectedItem != null)
|
|
_itemPreviewer.ChangeItem(_selectedItem, NeedShadedItem(_selectedItem));
|
|
}
|
|
|
|
private IEnumerable<ShopItemUI> Items()
|
|
{
|
|
foreach (var page in _pages)
|
|
{
|
|
foreach (var item in page.Items)
|
|
yield return item;
|
|
}
|
|
}
|
|
|
|
public void Open()
|
|
{
|
|
gameObject.SetActive(true);
|
|
|
|
SelectEquipmentOrFirstItem();
|
|
if (_itemPreviewer != null)
|
|
_itemPreviewer.Show();
|
|
}
|
|
|
|
public void Hide()
|
|
{
|
|
gameObject.SetActive(false);
|
|
|
|
if (_itemPreviewer != null)
|
|
_itemPreviewer.Hide();
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
if (_itemPreviewer != null)
|
|
Destroy(_itemPreviewer);
|
|
}
|
|
|
|
private bool NeedShadedItem(ConfBaseShopItem item)
|
|
{
|
|
return Shop.GetItemShopState(item) == ItemShopState.NotPurchased &&
|
|
Shop.HaveMoney(item.price);
|
|
}
|
|
} |