Files
vampire-like/Assets/GameMain/Scripts/Presentation/Shop/ShopForm.Logic.cs
T
2026-10-03 18:30:31 +08:00

197 lines
5.8 KiB
C#

using System.Collections.Generic;
using SepCore.Event;
using UnityEngine;
using UnityGameFramework.Runtime;
namespace SepCore.UI
{
public partial class ShopForm : UGuiForm
{
private readonly List<ShopGoodsItem> _goodsItems = new List<ShopGoodsItem>();
private ShopContext _context;
private bool _listenersBound;
public void RefreshUI(ShopContext context)
{
EnsureButtonListeners();
_context = context;
if (context.NeedRefreshGoodsItems)
{
View.titleFormatText.Set(context.CurrentLevel);
View.continueButtonFormatText.Set(context.CurrentLevel + 1);
RefreshGoodsItems(context.GoodsItems);
context.NeedRefreshGoodsItems = false;
}
if (context.NeedRefreshPrice)
{
View.refreshButtonFormatText.Set(context.RefreshPrice);
context.NeedRefreshPrice = false;
}
if (context.NeedRefreshPlayerCoin)
{
View.playerCoinFormatText.Set(context.PlayerCoin);
context.NeedRefreshPlayerCoin = false;
}
if (context.NeedRefreshPropList)
{
View.propList.OnInit(context.PropListContext);
context.NeedRefreshPropList = false;
}
if (context.NeedRefreshWeaponList)
{
View.weaponList.OnInit(context.WeaponListContext);
context.NeedRefreshWeaponList = false;
}
if (context.NeedRefreshStat)
{
View.statForm.OnInit(context.StatContext);
context.NeedRefreshStat = false;
}
bool weaponCapacityFull = context.WeaponListContext.CurrentCount >= context.WeaponListContext.MaxCount;
foreach (ShopGoodsItem item in _goodsItems)
{
item.RefreshAvailability(context.PlayerCoin, weaponCapacityFull);
}
View.refreshButton.interactable = context.PlayerCoin >= context.RefreshPrice;
}
protected override void OnOpen(object userData)
{
base.OnOpen(userData);
SetAttributesVisible(false);
if (userData is not ShopContext context)
{
Log.Error("ShopForm requires ShopContext as userData.");
return;
}
RefreshUI(context);
}
protected override void OnClose(bool isShutdown, object userData)
{
SetAttributesVisible(false);
_context = null;
for (int i = 0; i < _goodsItems.Count; i++)
{
_goodsItems[i].OnReset();
}
View.propList.OnReset();
View.weaponList.OnReset();
base.OnClose(isShutdown, userData);
}
private void EnsureButtonListeners()
{
if (_listenersBound)
{
return;
}
_listenersBound = true;
View.continueButton.onClick.AddListener(OnContinueButtonClick);
View.refreshButton.onClick.AddListener(OnRefreshButtonClick);
View.attributeToggleButton.onClick.AddListener(ShowAttributes);
View.attributeCloseButton.onClick.AddListener(HideAttributes);
View.attributeDismissButton.onClick.AddListener(HideAttributes);
}
private void RefreshGoodsItems(List<GoodsItemContext> goodsItems)
{
EnsureGoodsItemCount(goodsItems.Count);
for (int i = 0; i < _goodsItems.Count; i++)
{
ShopGoodsItem item = _goodsItems[i];
if (i < goodsItems.Count && goodsItems[i] != null)
{
item.Init(goodsItems[i], i);
item.gameObject.SetActive(true);
}
else
{
if (i < goodsItems.Count)
{
item.MarkPurchased();
item.gameObject.SetActive(true);
}
else
{
item.OnReset();
item.gameObject.SetActive(false);
}
}
}
}
private void EnsureGoodsItemCount(int count)
{
while (_goodsItems.Count < count)
{
ShopGoodsItem item = Instantiate(View.goodsItemTemplate, View.GoodsItemRoot);
_goodsItems.Add(item);
}
}
public void OnContinueButtonClick()
{
GameEntry.Event.Fire(this, ShopContinueEventArgs.Create());
}
public void OnRefreshButtonClick()
{
GameEntry.Event.Fire(this, ShopRefreshEventArgs.Create(_context.RefreshPrice));
}
private void ShowAttributes()
{
SetAttributesVisible(true);
}
private void HideAttributes()
{
SetAttributesVisible(false);
}
private void SetAttributesVisible(bool visible)
{
View.attributeDrawer.gameObject.SetActive(visible);
View.attributeToggleButton.gameObject.SetActive(!visible);
}
protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
{
base.OnUpdate(elapseSeconds, realElapseSeconds);
if (!View.attributeDrawer.gameObject.activeSelf)
{
return;
}
#if ENABLE_INPUT_SYSTEM
var keyboard = UnityEngine.InputSystem.Keyboard.current;
if (keyboard != null && keyboard.escapeKey.wasPressedThisFrame)
#else
if (Input.GetKeyDown(KeyCode.Escape))
#endif
{
HideAttributes();
}
}
}
}