141 lines
4.5 KiB
C#
141 lines
4.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using System;
|
|
using System.Linq;
|
|
using UnityEngine.AddressableAssets;
|
|
|
|
namespace BITFALL
|
|
{
|
|
public interface IEntityInventoryCallback
|
|
{
|
|
void OnAdd(IBasicItem item);
|
|
void OnRemove(IBasicItem item);
|
|
}
|
|
public abstract class EntityInventory : EntityComponent, IBasicItemContainer
|
|
{
|
|
/// <summary>
|
|
/// 数据字典
|
|
/// </summary>
|
|
Dictionary<int, IBasicItem> dictionary = new();
|
|
/// <summary>
|
|
/// 隐式接口实现
|
|
/// </summary>
|
|
public int Id => entity.Id;
|
|
/// <summary>
|
|
/// 工厂方法
|
|
/// </summary>
|
|
public event Func<IBasicItem, bool> AddFactory;
|
|
public event Func<IBasicItem, bool> RemoveFactory;
|
|
public event Func<IBasicItem, bool> DropFactory;
|
|
/// <summary>
|
|
/// 回调
|
|
/// </summary>
|
|
public event Action<IBasicItem> OnAdd;
|
|
public event Action<IBasicItem> OnRemove;
|
|
public event Action<IBasicItem> OnSet;
|
|
public event Action<IBasicItem> OnDrop;
|
|
public event Action<IBasicItemContainer> OnRebuild;
|
|
|
|
public virtual bool Add(IBasicItem item)
|
|
{
|
|
var pars = new object[] { item };
|
|
if (AddFactory != null)
|
|
foreach (var x in AddFactory.GetInvocationList())
|
|
{
|
|
if (x.Method.Invoke(x.Target, pars) is false)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
if (dictionary.TryAdd(item.Id, item))
|
|
{
|
|
OnAdd?.Invoke(item);
|
|
foreach (var x in entity.GetCallbacks<IEntityInventoryCallback>())
|
|
{
|
|
x.OnAdd(item);
|
|
}
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
public virtual IBasicItem[] GetItems() => dictionary.Values.ToArray();
|
|
public virtual bool Remove(IBasicItem item)
|
|
{
|
|
return Remove(item.Id);
|
|
}
|
|
public virtual bool Remove(int id)
|
|
{
|
|
|
|
if (dictionary.TryGetValue(id, out var item))
|
|
{
|
|
var pars = new object[] { item };
|
|
if(RemoveFactory is not null)
|
|
foreach (var x in RemoveFactory.GetInvocationList())
|
|
{
|
|
if (x.Method.Invoke(x.Target, pars) is false)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
dictionary.Remove(id);
|
|
foreach (var x in entity.GetCallbacks<IEntityInventoryCallback>())
|
|
{
|
|
x.OnRemove(item);
|
|
}
|
|
OnRemove?.Invoke(item);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public virtual bool Remove(Func<IBasicItem, bool> removeFactory)
|
|
{
|
|
bool isRemoved = false;
|
|
foreach (var x in dictionary.Values.ToArray())
|
|
{
|
|
if (removeFactory.Invoke(x))
|
|
{
|
|
Remove(x.Id);
|
|
isRemoved = true;
|
|
}
|
|
}
|
|
return isRemoved;
|
|
}
|
|
public bool TryGetItem(Func<IBasicItem, bool> func, out IBasicItem item)
|
|
{
|
|
return dictionary.Values.TryGetAny(func, out item);
|
|
}
|
|
public bool Drop(int Id)
|
|
{
|
|
if (dictionary.TryGetValue(Id, out var item))
|
|
{
|
|
if (Remove(item))
|
|
{
|
|
var pars = new object[] { item };
|
|
if (DropFactory is not null)
|
|
foreach (var x in DropFactory.GetInvocationList())
|
|
{
|
|
if (x.Method.Invoke(x.Target, pars) is false)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
var prefab = Addressables.LoadAssetAsync<AssetableItem>(item.AdressablePath).WaitForCompletion();
|
|
var instance = Instantiate(prefab.GetPrefab(), transform.position, transform.rotation);
|
|
instance.CopyItemsFrom(item);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
} |