This commit is contained in:
CortexCore
2024-11-13 17:47:45 +08:00
parent c4af12acd7
commit 416e3322db
208 changed files with 2591757 additions and 1497 deletions

View File

@@ -1,8 +1,13 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.SqlServer.Server;
namespace BITKit
{
/// <summary>
/// 支持二进制化的的物品容器,适用于网络化物品容器
/// 支持属性
@@ -10,6 +15,7 @@ namespace BITKit
/// </summary>
public interface IRuntimeItemContainer:IBinarySerialize
{
public ValidHandle IsBusy { get; }
/// <summary>
/// 物品容器的唯一Id
/// </summary>
@@ -27,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);
@@ -67,4 +78,98 @@ namespace BITKit
/// </summary>
event Action<bool> OnRelease;
}
public class RuntimeItemContainer : IRuntimeItemContainer
{
public RuntimeItemContainer()
{
IsBusy.AddListener(x=>OnRelease?.Invoke(!x));
}
public readonly ConcurrentDictionary<int, IRuntimeItem> Items = new();
public void Read(BinaryReader r)
{
throw new NotImplementedException();
}
public void Write(BinaryWriter w)
{
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)
{
return false;
}
}
Items.Set(item.Id, item);
OnAdd?.Invoke(item);
//BIT4Log.Log<RuntimeItemContainer>($"添加了了:{item.Id}");
return true;
}
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())
{
if (func.Invoke(item) is false)
{
return false;
}
}
Items.TryRemove(item.Id);
OnRemove?.Invoke(item);
//BIT4Log.Log<RuntimeItemContainer>($"移除了:{id}");
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())
{
if (func.Invoke(item) is false)
{
return false;
}
}
Items.TryRemove(item.Id);
OnDrop?.Invoke(item);
//BIT4Log.Log<RuntimeItemContainer>($"丢下了:{id}");
return true;
}
public event Func<IRuntimeItem, bool> AddFactory;
public event Func<IRuntimeItem, bool> RemoveFactory;
public event Func<IRuntimeItem, bool> DropFactory;
public event Action<IRuntimeItem> OnAdd;
public event Action<IRuntimeItem> OnRemove;
public event Action<IRuntimeItem> OnSet;
public event Action<IRuntimeItem> OnDrop;
public event Action<IRuntimeItemContainer> OnRebuild;
public event Action<bool> OnRelease;
}
}