1
This commit is contained in:
@@ -94,6 +94,11 @@ namespace BITKit
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
int Value => 10;
|
||||
/// <summary>
|
||||
/// 创建运行时物品
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IRuntimeItem CreateRuntimeItem();
|
||||
}
|
||||
|
||||
public interface IRuntimeItem : ICloneable
|
||||
@@ -101,7 +106,7 @@ namespace BITKit
|
||||
/// <summary>
|
||||
/// 运行时Id
|
||||
/// </summary>
|
||||
public int Id { get; }
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 配置Id
|
||||
@@ -111,7 +116,7 @@ namespace BITKit
|
||||
/// <summary>
|
||||
/// 数量
|
||||
/// </summary>
|
||||
public int Amount { get; }
|
||||
public int Amount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 运行时属性
|
||||
@@ -133,14 +138,23 @@ namespace BITKit
|
||||
/// 被托管的物品
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public record RuntimeItem : IRuntimeItem
|
||||
public struct RuntimeItem : IRuntimeItem
|
||||
{
|
||||
public int Id { get; set; } = new Random().Next();
|
||||
public static RuntimeItem Create()
|
||||
{
|
||||
var item = new RuntimeItem()
|
||||
{
|
||||
Id = new Random().Next(),
|
||||
RuntimeProperties =
|
||||
new Dictionary<Type, IScriptableItemProperty>()
|
||||
};
|
||||
return item;
|
||||
}
|
||||
public int Id { get; set; }
|
||||
public int ScriptableId { get; set; }
|
||||
public int Amount { get; set; }
|
||||
|
||||
public IDictionary<Type, IScriptableItemProperty> RuntimeProperties { get; set; } =
|
||||
new Dictionary<Type, IScriptableItemProperty>();
|
||||
public IDictionary<Type, IScriptableItemProperty> RuntimeProperties { get; set; }
|
||||
|
||||
public event Action<IRuntimeItem> OnRuntimePropertiesChanged;
|
||||
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
13
Assets/BITKit/Core/Item/ItemEx.cs
Normal file
13
Assets/BITKit/Core/Item/ItemEx.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
/// <summary>
|
||||
/// 玩家向背包添加物品,但是被其他逻辑处理
|
||||
/// </summary>
|
||||
public sealed class PlayerInventoryAddItemOverridden : InGameException
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user