将 ShopController 构建 Context 的功能迁移到 partial class 里,并调整了部分构建逻辑
This commit is contained in:
@@ -1,16 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using SepCore.Event;
|
||||
using SepCore.Definition;
|
||||
using SepCore.Entity.Weapon;
|
||||
using GameFramework.Event;
|
||||
using SepCore.DataTable;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public class ShopController : UIControllerBase<ShopContext, ShopForm>
|
||||
public partial class ShopController : UIControllerBase<ShopContext, ShopForm>
|
||||
{
|
||||
private ShopUseCase _useCase;
|
||||
private ShopRawData _rawData;
|
||||
@@ -47,188 +45,6 @@ namespace SepCore.UI
|
||||
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormComplete);
|
||||
}
|
||||
|
||||
#region BuildContext
|
||||
|
||||
private async UniTask<ShopContext> BuildContext(ShopRawData rawData)
|
||||
{
|
||||
if (rawData == null)
|
||||
{
|
||||
Log.Error("ShopFormController.BuildContext() rawData is null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
_rawData = rawData;
|
||||
|
||||
List<GoodsItemContext> goodsItems = new List<GoodsItemContext>();
|
||||
foreach (var item in rawData.GoodsItems)
|
||||
{
|
||||
var context = await CreateGoodsItemContextAsync(item);
|
||||
goodsItems.Add(context);
|
||||
}
|
||||
|
||||
return new ShopContext
|
||||
{
|
||||
CurrentLevel = rawData.CurrentLevel,
|
||||
RefreshPrice = rawData.RefreshPrice,
|
||||
PlayerCoin = rawData.PlayerCoin,
|
||||
GoodsItems = goodsItems,
|
||||
PropListContext =
|
||||
BuildDisplayListAreaContext(DisplayListAreaType.Prop, rawData.PropItems, rawData.PropMaxCount),
|
||||
WeaponListContext = BuildDisplayListAreaContext(DisplayListAreaType.Weapon, rawData.WeaponItems,
|
||||
rawData.WeaponMaxCount),
|
||||
NeedRefreshGoodsItems = true,
|
||||
NeedRefreshPlayerCoin = true,
|
||||
NeedRefreshPropList = true,
|
||||
NeedRefreshWeaponList = true,
|
||||
NeedRefreshPrice = true
|
||||
};
|
||||
}
|
||||
|
||||
private static DisplayListAreaContext BuildDisplayListAreaContext(DisplayListAreaType listType,
|
||||
IReadOnlyList<object> items,
|
||||
int maxCount)
|
||||
{
|
||||
string title = GetDisplayListTitle(listType);
|
||||
if (items == null)
|
||||
{
|
||||
return new DisplayListAreaContext
|
||||
{
|
||||
Title = title,
|
||||
CurrentCount = 0,
|
||||
MaxCount = maxCount,
|
||||
ItemContexts = System.Array.Empty<DisplayItemContext>()
|
||||
};
|
||||
}
|
||||
|
||||
DisplayItemContext[] itemContexts = new DisplayItemContext[items.Count];
|
||||
switch (listType)
|
||||
{
|
||||
case DisplayListAreaType.Weapon:
|
||||
if (items is IReadOnlyList<WeaponBase> weapons)
|
||||
{
|
||||
for (int i = 0; i < weapons.Count; i++)
|
||||
{
|
||||
WeaponBase weapon = weapons[i];
|
||||
if (weapon == null) break;
|
||||
itemContexts[i] = BuildWeaponItem(weapon);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case DisplayListAreaType.Prop:
|
||||
if (items is IReadOnlyList<PropItem> propItems)
|
||||
{
|
||||
for (int i = 0; i < propItems.Count; i++)
|
||||
{
|
||||
PropItem propItem = propItems[i];
|
||||
if (propItem == null) break;
|
||||
itemContexts[i] = BuildPropItem(propItem);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
int currentCount = itemContexts.Length;
|
||||
return new DisplayListAreaContext
|
||||
{
|
||||
Title = title,
|
||||
CurrentCount = currentCount,
|
||||
MaxCount = maxCount,
|
||||
ItemContexts = itemContexts
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetDisplayListTitle(DisplayListAreaType listType)
|
||||
{
|
||||
return listType switch
|
||||
{
|
||||
DisplayListAreaType.Weapon => "武器",
|
||||
DisplayListAreaType.Prop => "道具",
|
||||
_ => string.Empty
|
||||
};
|
||||
}
|
||||
|
||||
private static DisplayItemContext BuildPropItem(PropItem propItem)
|
||||
{
|
||||
string iconAssetName = null;
|
||||
ItemRarity rarity = ItemRarity.None;
|
||||
|
||||
if (propItem != null)
|
||||
{
|
||||
iconAssetName = propItem.IconAssetName;
|
||||
rarity = propItem.Rarity;
|
||||
}
|
||||
|
||||
return new DisplayItemContext
|
||||
{
|
||||
IconAssetName = iconAssetName,
|
||||
Rarity = rarity,
|
||||
IsWeapon = false
|
||||
};
|
||||
}
|
||||
|
||||
private static DisplayItemContext BuildWeaponItem(WeaponBase weaponBase)
|
||||
{
|
||||
string iconAssetName = null;
|
||||
ItemRarity rarity = ItemRarity.None;
|
||||
|
||||
if (weaponBase != null && weaponBase.WeaponData != null)
|
||||
{
|
||||
iconAssetName = weaponBase.WeaponData.IconAssetName;
|
||||
rarity = weaponBase.WeaponData.Rarity;
|
||||
}
|
||||
|
||||
return new DisplayItemContext
|
||||
{
|
||||
IconAssetName = iconAssetName,
|
||||
Rarity = rarity,
|
||||
IsWeapon = true
|
||||
};
|
||||
}
|
||||
|
||||
private static void AppendDisplayItemContext(DisplayListAreaContext listContext, DisplayItemContext newItem)
|
||||
{
|
||||
if (listContext == null)
|
||||
{
|
||||
Log.Error("ShopFormController.AppendDisplayItemContext() listContext is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (newItem == null)
|
||||
{
|
||||
Log.Warning("ShopFormController.AppendDisplayItemContext() newItem is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
int oldCount = listContext.ItemContexts != null ? listContext.ItemContexts.Length : 0;
|
||||
DisplayItemContext[] newContexts = new DisplayItemContext[oldCount + 1];
|
||||
if (oldCount > 0)
|
||||
{
|
||||
System.Array.Copy(listContext.ItemContexts, newContexts, oldCount);
|
||||
}
|
||||
|
||||
newContexts[oldCount] = newItem;
|
||||
listContext.ItemContexts = newContexts;
|
||||
listContext.CurrentCount = oldCount + 1;
|
||||
}
|
||||
|
||||
private async UniTask<GoodsItemContext> CreateGoodsItemContextAsync(GoodsItemRawData rawData,
|
||||
float timeout = 30f)
|
||||
{
|
||||
var context = new GoodsItemContext(rawData);
|
||||
context.Icon = context.ItemType switch
|
||||
{
|
||||
ItemType.Weapon => await GameEntry.SpriteCache.GetSprite(((DRWeapon)rawData.DataRow).IconAssetName),
|
||||
ItemType.Prop => await GameEntry.SpriteCache.GetSprite(((DRProp)rawData.DataRow).IconAssetName),
|
||||
_ => null
|
||||
};
|
||||
return context;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region UI Methods
|
||||
|
||||
public override async UniTask CloseUIAsync(object userData = null, float timeout = 30f)
|
||||
@@ -250,6 +66,7 @@ namespace SepCore.UI
|
||||
if (userData is ShopRawData rawData)
|
||||
{
|
||||
await OpenUIAsync(rawData, timeout);
|
||||
return;
|
||||
}
|
||||
|
||||
if (userData != null)
|
||||
@@ -285,18 +102,6 @@ namespace SepCore.UI
|
||||
|
||||
private async UniTask RefreshGoodsItems(ShopRefreshResult result)
|
||||
{
|
||||
if (result == null)
|
||||
{
|
||||
Log.Error("ShopFormController.RefreshGoodsItems() result is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (Context == null)
|
||||
{
|
||||
Log.Error("ShopFormController.RefreshGoodsItems() Context is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < result.GoodsItems.Count; i++)
|
||||
{
|
||||
if (i < Context.GoodsItems.Count)
|
||||
@@ -308,8 +113,8 @@ namespace SepCore.UI
|
||||
|
||||
if (Context.GoodsItems.Count != result.GoodsItems.Count)
|
||||
{
|
||||
Context.GoodsItems.RemoveRange(Context.GoodsItems.Count,
|
||||
Context.GoodsItems.Count - Context.GoodsItems.Count);
|
||||
int count = Context.GoodsItems.Count - result.GoodsItems.Count;
|
||||
Context.GoodsItems.RemoveRange(result.GoodsItems.Count, count);
|
||||
}
|
||||
|
||||
Context.RefreshPrice = result.RefreshPrice;
|
||||
@@ -321,18 +126,6 @@ namespace SepCore.UI
|
||||
|
||||
private void ApplyGoodsPurchased(ShopPurchaseResult result)
|
||||
{
|
||||
if (result == null)
|
||||
{
|
||||
Log.Error("ShopFormController.ApplyGoodsPurchased() result is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (Context == null)
|
||||
{
|
||||
Log.Error("ShopFormController.ApplyGoodsPurchased() Context is null.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (Context.GoodsItems != null && result.GoodsIndex >= 0 && result.GoodsIndex < Context.GoodsItems.Count)
|
||||
{
|
||||
Context.GoodsItems[result.GoodsIndex] = null;
|
||||
@@ -481,17 +274,17 @@ namespace SepCore.UI
|
||||
}
|
||||
|
||||
private void ShopPurchase(object sender, GameEventArgs e)
|
||||
{
|
||||
OnPurchaseRequestAsync(sender, e).Forget();
|
||||
}
|
||||
|
||||
private async UniTaskVoid OnPurchaseRequestAsync(object sender, GameEventArgs e)
|
||||
{
|
||||
if (e is not ShopPurchaseEventArgs args)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
OnPurchaseRequestAsync(args).Forget();
|
||||
}
|
||||
|
||||
private async UniTaskVoid OnPurchaseRequestAsync(ShopPurchaseEventArgs args)
|
||||
{
|
||||
ShopPurchaseResult result = await _useCase.TryPurchaseAsync(args.GoodsIndex);
|
||||
if (result == null)
|
||||
{
|
||||
@@ -582,7 +375,7 @@ namespace SepCore.UI
|
||||
_tooltipLocked = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user