添加商店 UI
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
using GeometryTD.CustomEvent;
|
||||
using GeometryTD.Definition;
|
||||
using GameFramework.Event;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public sealed class ShopFormController : UIFormControllerCommonBase<ShopFormContext, ShopForm>
|
||||
{
|
||||
private ShopFormUseCase _useCase;
|
||||
|
||||
protected override UIFormType UIFormTypeId => UIFormType.ShopForm;
|
||||
|
||||
protected override void RefreshUI(ShopForm form, ShopFormContext context)
|
||||
{
|
||||
form.RefreshUI(context);
|
||||
}
|
||||
|
||||
protected override void SubscribeCustomEvents()
|
||||
{
|
||||
GameEntry.Event.Subscribe(ShopPurchaseRequestedEventArgs.EventId, OnPurchaseRequested);
|
||||
GameEntry.Event.Subscribe(ShopExitRequestedEventArgs.EventId, OnExitRequested);
|
||||
GameEntry.Event.Subscribe(ShopInventoryRequestedEventArgs.EventId, OnInventoryRequested);
|
||||
}
|
||||
|
||||
protected override void UnsubscribeCustomEvents()
|
||||
{
|
||||
GameEntry.Event.Unsubscribe(ShopPurchaseRequestedEventArgs.EventId, OnPurchaseRequested);
|
||||
GameEntry.Event.Unsubscribe(ShopExitRequestedEventArgs.EventId, OnExitRequested);
|
||||
GameEntry.Event.Unsubscribe(ShopInventoryRequestedEventArgs.EventId, OnInventoryRequested);
|
||||
}
|
||||
|
||||
public override int? OpenUI(object userData = null)
|
||||
{
|
||||
if (userData is ShopFormContext context)
|
||||
{
|
||||
return OpenUIInternal(context);
|
||||
}
|
||||
|
||||
if (userData is ShopFormRawData rawDataFromUserData)
|
||||
{
|
||||
return OpenUI(rawDataFromUserData);
|
||||
}
|
||||
|
||||
if (userData != null)
|
||||
{
|
||||
Log.Warning("ShopFormController.OpenUI() userData type is invalid.");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_useCase == null)
|
||||
{
|
||||
Log.Error("ShopFormController.OpenUI() useCase is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
ShopFormRawData rawData = _useCase.CreateInitialModel();
|
||||
return OpenUI(rawData);
|
||||
}
|
||||
|
||||
public int? OpenUI(ShopFormRawData rawData)
|
||||
{
|
||||
ShopFormContext context = BuildContext(rawData);
|
||||
return OpenUIInternal(context);
|
||||
}
|
||||
|
||||
public override void BindUseCase(IUIUseCase useCase)
|
||||
{
|
||||
if (!(useCase is ShopFormUseCase shopFormUseCase))
|
||||
{
|
||||
Log.Error("ShopFormController.BindUseCase() useCase is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
_useCase = shopFormUseCase;
|
||||
}
|
||||
|
||||
private void OnPurchaseRequested(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!IsEventFromCurrentForm(sender) || !(e is ShopPurchaseRequestedEventArgs args) || _useCase == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_useCase.TryPurchase(args.GoodsIndex, out ShopFormRawData rawData))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SetContext(BuildContext(rawData));
|
||||
RefreshCurrentUI();
|
||||
}
|
||||
|
||||
private void OnExitRequested(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!IsEventFromCurrentForm(sender) || !(e is ShopExitRequestedEventArgs))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.ShopNode.EndShop();
|
||||
}
|
||||
|
||||
private void OnInventoryRequested(object sender, GameEventArgs e)
|
||||
{
|
||||
if (!IsEventFromCurrentForm(sender) || !(e is ShopInventoryRequestedEventArgs))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.UIRouter.OpenUI(UIFormType.RepoForm);
|
||||
}
|
||||
|
||||
private bool IsEventFromCurrentForm(object sender)
|
||||
{
|
||||
if (Form == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(sender, Form))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (sender is Component component)
|
||||
{
|
||||
ShopForm ownerForm = component.GetComponentInParent<ShopForm>();
|
||||
return ownerForm == Form;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static ShopFormContext BuildContext(ShopFormRawData rawData)
|
||||
{
|
||||
GoodsItemContext[] goodsItemContexts = System.Array.Empty<GoodsItemContext>();
|
||||
if (rawData?.GoodsItems != null && rawData.GoodsItems.Count > 0)
|
||||
{
|
||||
goodsItemContexts = new GoodsItemContext[rawData.GoodsItems.Count];
|
||||
for (int i = 0; i < rawData.GoodsItems.Count; i++)
|
||||
{
|
||||
GoodsItemRawData item = rawData.GoodsItems[i];
|
||||
goodsItemContexts[i] = new GoodsItemContext
|
||||
{
|
||||
GoodsIndex = item?.GoodsIndex ?? i,
|
||||
Title = item?.Title ?? string.Empty,
|
||||
TypeText = item?.TypeText ?? string.Empty,
|
||||
Description = item?.Description ?? string.Empty,
|
||||
TagTexts = BuildTagTexts(item?.Tags),
|
||||
PurchaseButtonText = item != null && item.IsPurchased ? "已购买" : $"购买 {item?.Price ?? 0}",
|
||||
CanPurchase = item != null && !item.IsPurchased,
|
||||
IconAreaContext = item?.IconAreaContext
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return new ShopFormContext
|
||||
{
|
||||
GoldText = $"金币: {rawData?.PlayerGold ?? 0}",
|
||||
GoodsItems = goodsItemContexts
|
||||
};
|
||||
}
|
||||
|
||||
private static string[] BuildTagTexts(TagType[] tags)
|
||||
{
|
||||
if (tags == null || tags.Length <= 0)
|
||||
{
|
||||
return System.Array.Empty<string>();
|
||||
}
|
||||
|
||||
string[] results = new string[tags.Length];
|
||||
for (int i = 0; i < tags.Length; i++)
|
||||
{
|
||||
results[i] = tags[i].ToString();
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6dd7cf0d9ac04fbf887dd5c91ccfd8c7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user