140 lines
3.5 KiB
C#
140 lines
3.5 KiB
C#
using Fantasy;
|
|
using Fantasy.Entitas.Interface;
|
|
|
|
namespace NB.Game;
|
|
|
|
public sealed class ItemContainerDestroySystem : DestroySystem<PlayerItemContainer>
|
|
{
|
|
protected override void Destroy(PlayerItemContainer self)
|
|
{
|
|
self.CellCountMax = 0;
|
|
|
|
foreach (var (_, item) in self.Items)
|
|
{
|
|
item.Dispose();
|
|
}
|
|
|
|
self.Items.Clear();
|
|
// self.ItemsByConfigId.Clear();
|
|
// self.ItemsByType.Clear();
|
|
}
|
|
}
|
|
|
|
public static class ItemContainerSystem
|
|
{
|
|
#region Get
|
|
|
|
/// <summary>
|
|
/// 通过唯一id获取
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public static bool GetItemById(this PlayerItemContainer self, long id, out Item? item)
|
|
{
|
|
return self.Items.TryGetValue(id, out item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过配置id获取物品
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="configId"></param>
|
|
/// <param name="item"></param>
|
|
public static bool GetFistItemByConfigId(this PlayerItemContainer self, int configId, out Item? item)
|
|
{
|
|
foreach (var (_, it) in self.Items)
|
|
{
|
|
if (it.ConfigId == configId)
|
|
{
|
|
item = it;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
item = null;
|
|
return false;
|
|
// if (!self.ItemsByConfigId.TryGetValue(configId, out var itemList))
|
|
// {
|
|
// item = null;
|
|
// return;
|
|
// }
|
|
//
|
|
// item = itemList.First();
|
|
}
|
|
|
|
// /// <summary>
|
|
// /// 通过配置id获取物品
|
|
// /// </summary>
|
|
// /// <param name="self"></param>
|
|
// /// <param name="configId"></param>
|
|
// /// <param name="items"></param>
|
|
// public static void GetItemByConfigId(this ItemContainer self, uint configId, List<Item> items)
|
|
// {
|
|
// if (!self.ItemsByConfigId.TryGetValue(configId, out var itemList))
|
|
// {
|
|
// return;
|
|
// }
|
|
//
|
|
// items.AddRange(itemList);
|
|
// }
|
|
|
|
// /// <summary>
|
|
// /// 通过类型获取物品
|
|
// /// </summary>
|
|
// /// <param name="self"></param>
|
|
// /// <param name="type"></param>
|
|
// /// <param name="items"></param>
|
|
// public static void GetItemByType(this ItemContainer self, ItemType type, List<Item> items)
|
|
// {
|
|
// if (!self.ItemsByType.TryGetValue((uint)type, out var itemList))
|
|
// {
|
|
// return;
|
|
// }
|
|
//
|
|
// items.AddRange(itemList);
|
|
// }
|
|
|
|
#endregion
|
|
|
|
#region Add
|
|
|
|
public static void Add(this PlayerItemContainer self, int configId, int count)
|
|
{
|
|
var item = ItemFactory.Create(self.Scene, configId, count);
|
|
self.Items.Add(item.Id, item);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 结构转换
|
|
|
|
public static List<ItemInfo> GetItemInfos(this PlayerItemContainer self)
|
|
{
|
|
List<ItemInfo> ret = new List<ItemInfo>();
|
|
foreach (var (_, item) in self.Items)
|
|
{
|
|
ret.Add(item.ToItemInfo());
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
public static List<GearInfo> GetGearInfos(this PlayerItemContainer self)
|
|
{
|
|
List<GearInfo> ret = new List<GearInfo>();
|
|
|
|
foreach (var (rod, rigs) in self.FishingRig)
|
|
{
|
|
GearInfo gearInfo = new GearInfo();
|
|
gearInfo.Rod = rod;
|
|
gearInfo.Rigs.AddRange(rigs);
|
|
ret.Add(gearInfo);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
#endregion
|
|
} |