175 lines
6.0 KiB
C#
175 lines
6.0 KiB
C#
using System.Collections.Generic;
|
|
using Cysharp.Threading.Tasks;
|
|
using SepCore.DataTable;
|
|
using SepCore.Definition;
|
|
using SepCore.Entity.Weapon;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace SepCore.UI
|
|
{
|
|
public partial class ShopController
|
|
{
|
|
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(rawData)
|
|
{
|
|
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)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|