171 lines
4.1 KiB
C#
171 lines
4.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Fantasy;
|
|
using Fantasy.Async;
|
|
using Fantasy.Entitas.Interface;
|
|
|
|
namespace NB.Game;
|
|
|
|
public sealed class ItemContainerDestroySystem : DestroySystem<PlayerItemContainerComponent>
|
|
{
|
|
protected override void Destroy(PlayerItemContainerComponent self)
|
|
{
|
|
self.CellCountMax = 0;
|
|
|
|
foreach (var (_, item) in self.Items)
|
|
{
|
|
item.Dispose();
|
|
}
|
|
|
|
self.Items.Clear();
|
|
}
|
|
}
|
|
|
|
public sealed class ItemContainerAwakeSystem : AwakeSystem<PlayerItemContainerComponent>
|
|
{
|
|
protected override void Awake(PlayerItemContainerComponent self)
|
|
{
|
|
}
|
|
}
|
|
|
|
public sealed class ItemContainerAwakeSystem1 : DeserializeSystem<PlayerItemContainerComponent>
|
|
{
|
|
protected override void Deserialize(PlayerItemContainerComponent self)
|
|
{
|
|
if (self.Slots.Count < 9)
|
|
{
|
|
var add = 9 - self.Slots.Count;
|
|
for (int i = 0; i < add; i++)
|
|
{
|
|
self.Slots.Add(0);
|
|
}
|
|
|
|
// _ = self.Save();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class PlayerItemContainerComponentSystem
|
|
{
|
|
#region Save
|
|
|
|
public static async FTask Save(this PlayerItemContainerComponent self)
|
|
{
|
|
await self.Scene.World.Database.Save(self);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Get
|
|
|
|
/// <summary>
|
|
/// 通过唯一id获取
|
|
/// </summary>
|
|
/// <param name="self"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public static bool GetItemById(this PlayerItemContainerComponent 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 PlayerItemContainerComponent self, int configId, out Item? item)
|
|
{
|
|
foreach (var (_, it) in self.Items)
|
|
{
|
|
if (it.ConfigId == configId)
|
|
{
|
|
item = it;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
item = null;
|
|
return false;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Add
|
|
|
|
public static async FTask Add(this PlayerItemContainerComponent self, Dictionary<int, int> items)
|
|
{
|
|
foreach (var (configId, count) in items)
|
|
{
|
|
await self.Add(configId, count, false);
|
|
}
|
|
|
|
await self.Save();
|
|
}
|
|
|
|
public static async FTask Add(this PlayerItemContainerComponent self, int configId, int count,
|
|
bool needSave = true)
|
|
{
|
|
var item = ItemFactory.Create(self.Scene, configId, count);
|
|
self.Items.Add(item.Id, item);
|
|
if (needSave)
|
|
{
|
|
await self.Save();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 结构转换
|
|
|
|
public static List<ItemInfo> GetItemInfos(this PlayerItemContainerComponent self)
|
|
{
|
|
List<ItemInfo> ret = new List<ItemInfo>(self.Items.Count);
|
|
foreach (var (_, item) in self.Items)
|
|
{
|
|
ret.Add(item.ToItemInfo());
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
public static ItemBindInfo GetRigInfo(this PlayerItemContainerComponent self, long id)
|
|
{
|
|
if (self.Binding.TryGetValue(id, out var info))
|
|
{
|
|
ItemBindInfo gearInfo = new ItemBindInfo();
|
|
gearInfo.Item = id;
|
|
if (info != null)
|
|
{
|
|
gearInfo.BindItems.AddRange(info);
|
|
}
|
|
|
|
return gearInfo;
|
|
}
|
|
|
|
return new ItemBindInfo() { Item = id };
|
|
}
|
|
|
|
public static List<ItemBindInfo> GetRigInfos(this PlayerItemContainerComponent self)
|
|
{
|
|
List<ItemBindInfo> ret = new List<ItemBindInfo>(self.Binding.Count);
|
|
foreach (var (rod, rigs) in self.Binding)
|
|
{
|
|
ItemBindInfo gearInfo = new ItemBindInfo();
|
|
gearInfo.Item = rod;
|
|
gearInfo.BindItems.AddRange(rigs);
|
|
ret.Add(gearInfo);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
public static List<long> GetSlots(this PlayerItemContainerComponent self)
|
|
{
|
|
return self.Slots.ToList();
|
|
}
|
|
|
|
#endregion
|
|
} |