This commit is contained in:
CortexCore
2024-11-20 11:36:51 +08:00
parent 5d19061fab
commit ce049035e2
20 changed files with 180 additions and 184 deletions

View File

@@ -15,6 +15,7 @@ namespace BITKit
/// </summary>
public interface IRuntimeItemContainer:IBinarySerialize
{
public ValidHandle IsBusy { get; }
/// <summary>
/// 物品容器的唯一Id
/// </summary>
@@ -32,6 +33,11 @@ namespace BITKit
/// </summary>
bool Remove(int id);
/// <summary>
/// 设置物品
/// </summary>
/// <param name="id"></param>
void Set(IRuntimeItem id);
/// <summary>
/// 通过通过Id丢下物品
/// </summary>
bool Drop(int id);
@@ -75,6 +81,10 @@ namespace BITKit
public class RuntimeItemContainer : IRuntimeItemContainer
{
public RuntimeItemContainer()
{
IsBusy.AddListener(x=>OnRelease?.Invoke(!x));
}
public readonly ConcurrentDictionary<int, IRuntimeItem> Items = new();
public void Read(BinaryReader r)
{
@@ -86,11 +96,13 @@ namespace BITKit
throw new NotImplementedException();
}
public ValidHandle IsBusy { get; } = new();
public int Id { get; set; }
public IRuntimeItem[] GetItems()=>Items.Values.ToArray();
public bool Add(IRuntimeItem item)
{
using var _ = IsBusy.GetHandle();
foreach (var func in AddFactory.CastAsFunc())
{
if (func.Invoke(item) is false)
@@ -109,6 +121,7 @@ namespace BITKit
public bool Remove(int id)
{
using var _ = IsBusy.GetHandle();
if (Items.TryGetValue(id, out var item) is false) return false;
foreach (var func in RemoveFactory.CastAsFunc())
@@ -124,8 +137,17 @@ namespace BITKit
return true;
}
public void Set(IRuntimeItem id)
{
using var _ = IsBusy.GetHandle();
if (!Items.TryGetValue(id.Id, out var item)) return;
item = id;
OnSet?.Invoke(item);
}
public bool Drop(int id)
{
using var _ = IsBusy.GetHandle();
if (Items.TryGetValue(id, out var item) is false) return false;
foreach (var func in DropFactory.CastAsFunc())
{