1
This commit is contained in:
@@ -16,7 +16,7 @@ namespace BITFALL
|
||||
{
|
||||
[CustomType(typeof(IEntityInventory))]
|
||||
[CustomType(typeof(IBasicItemContainer))]
|
||||
public abstract class EntityInventory : EntityComponent, IEntityInventory
|
||||
public abstract class EntityInventory : EntityComponent, IEntityInventory,IBasicItemContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据字典
|
||||
@@ -41,20 +41,30 @@ namespace BITFALL
|
||||
public event Func<IBasicItem, bool> DropFactory;
|
||||
// 回调
|
||||
public event Action<IBasicItem> OnAdd;
|
||||
public event Action<IBasicItem> OnUsed;
|
||||
public event Action<IBasicItem> OnUsedItem;
|
||||
public event Action<IBasicItem> OnRemove;
|
||||
public event Func<IBasicItem, bool> TryUseItemFactory;
|
||||
public event Action<IBasicItem> OnSet;
|
||||
public event Action<IBasicItem> OnDrop;
|
||||
public event Action<IBasicItemContainer> OnRebuild;
|
||||
public bool UseItem(IBasicItem item)
|
||||
public bool Clear(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool TryUseItem(IBasicItem item)
|
||||
{
|
||||
if (dictionary.ContainsKey(item.Id) is false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
dictionary.Remove(item.Id);
|
||||
OnUsed?.Invoke(item);
|
||||
return true;
|
||||
return TryUseItemFactory is not null && TryUseItemFactory.CastAsFunc().Any(x => x.Invoke(item));
|
||||
}
|
||||
|
||||
public void UseItem(IBasicItem item)
|
||||
{
|
||||
OnUsedItem?.Invoke(item);
|
||||
dictionary.TryRemove(item.Id);
|
||||
}
|
||||
|
||||
[Inject]
|
||||
|
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using BITFALL.Entities.Equipment;
|
||||
using BITFALL.Entities.Inventory;
|
||||
using UnityEngine;
|
||||
using BITKit;
|
||||
using BITKit.Entities;
|
||||
@@ -17,20 +18,30 @@ namespace BITFALL
|
||||
public double currentWeight;
|
||||
[Header(Constant.Header.Settings)]
|
||||
public double maxWeight =8;
|
||||
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
private IBasicItemContainer container;
|
||||
[Inject]
|
||||
private IBasicItemContainer _container;
|
||||
[Inject(true)]
|
||||
private IEntityInventory _inventory;
|
||||
[Inject(true)]
|
||||
private IEntityEquipmentContainer playerEquipContainer;
|
||||
public override void OnStart()
|
||||
{
|
||||
base.OnStart();
|
||||
container = entity.Get<IBasicItemContainer>();
|
||||
container.AddFactory += AddFactory;
|
||||
container.OnAdd += OnAdd;
|
||||
container.OnUsed += OnRemove;
|
||||
container.OnRemove += OnRemove;
|
||||
_container.AddFactory += AddFactory;
|
||||
_container.OnAdd += OnAdd;
|
||||
_container.OnRemove += OnRemove;
|
||||
|
||||
var playerEquipContainer = entity.Get<IEntityEquipmentContainer>();
|
||||
playerEquipContainer.OnEquip += OnEquip;
|
||||
playerEquipContainer.OnDeEquip += DeEquip;
|
||||
if (_inventory is not null)
|
||||
{
|
||||
_inventory.OnUsedItem += OnRemove;
|
||||
}
|
||||
|
||||
if (playerEquipContainer is not null)
|
||||
{
|
||||
playerEquipContainer.OnEquip += OnEquip;
|
||||
playerEquipContainer.OnDeEquip += DeEquip;
|
||||
}
|
||||
}
|
||||
|
||||
private bool AddFactory(IBasicItem item)
|
||||
|
@@ -1,98 +1,99 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BITFALL.Player.Inventory;
|
||||
using UnityEngine;
|
||||
using BITKit;
|
||||
using BITKit.Entities;
|
||||
|
||||
namespace BITFALL
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 玩家装备容器
|
||||
/// 支持,护甲,头盔和背包等
|
||||
/// </summary>
|
||||
[CustomType(typeof(IPlayerEquipContainer))]
|
||||
public class PlayerEquipContainer : EntityComponent, IPlayerEquipContainer
|
||||
{
|
||||
private readonly Dictionary<IEquipmentSlot, IBasicItem> dictionary = new();
|
||||
[Inject]
|
||||
private IBasicItemContainer inventory;
|
||||
public override void OnAwake()
|
||||
{
|
||||
var health = entity.Get<IHealth>();
|
||||
health.OnSetAlive += OnSetAlive;
|
||||
}
|
||||
|
||||
private void OnSetAlive(bool obj)
|
||||
{
|
||||
if (obj) return;
|
||||
foreach (var x in dictionary.ToArray())
|
||||
{
|
||||
OnDeEquip?.Invoke(x.Key, x.Value);
|
||||
inventory.Add(x.Value);
|
||||
}
|
||||
dictionary.Clear();
|
||||
}
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
base.OnStart();
|
||||
inventory = entity.Get<IBasicItemContainer>();
|
||||
var playerInventory = entity.Get<IPlayerInventory>();
|
||||
playerInventory.OnUseItem += TryExecute;
|
||||
}
|
||||
|
||||
public Action<IEquipmentSlot, IBasicItem> OnEquip { get; set; }
|
||||
public Action<IEquipmentSlot, IBasicItem> OnDeEquip { get; set; }
|
||||
|
||||
public bool TryDeEquip<T>(T slot) where T : IEquipmentSlot
|
||||
{
|
||||
if (!dictionary.TryGetValue(slot, out var equipable)) return false;
|
||||
if (inventory.Add(equipable))
|
||||
{
|
||||
DeEquip(slot, equipable);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private bool Equip(IEquipmentSlot slot, IBasicItem item)
|
||||
{
|
||||
dictionary.Add(slot, item);
|
||||
OnEquip?.Invoke(slot, item);
|
||||
return true;
|
||||
}
|
||||
private bool DeEquip(IEquipmentSlot slot, IBasicItem item)
|
||||
{
|
||||
dictionary.Remove(slot);
|
||||
OnDeEquip?.Invoke(slot, item);
|
||||
return true;
|
||||
}
|
||||
public int Priority => 0;
|
||||
|
||||
public bool TryExecute(IBasicItem value)
|
||||
{
|
||||
var asset = value.GetAssetable();
|
||||
//尝试获取可装备信息
|
||||
if (!asset.TryGetProperty<EquipmentAsSlot>(out var equipable)) return false;
|
||||
//已装备物品
|
||||
if (dictionary.TryGetValue(equipable.slot, out var equipedItem))
|
||||
{
|
||||
//尝试将装配放回背包
|
||||
if (inventory.Add(equipedItem))
|
||||
{
|
||||
//移除已装备物品
|
||||
DeEquip(equipable.slot, value);
|
||||
}
|
||||
}
|
||||
//从库存中移除物品
|
||||
if (inventory.Remove(value))
|
||||
{
|
||||
//装配物品
|
||||
Equip(equipable.slot, value);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
// using System;
|
||||
// using System.Collections;
|
||||
// using System.Collections.Generic;
|
||||
// using System.Linq;
|
||||
// using BITFALL.Entities.Equipment;
|
||||
// using BITFALL.Player.Inventory;
|
||||
// using UnityEngine;
|
||||
// using BITKit;
|
||||
// using BITKit.Entities;
|
||||
//
|
||||
// namespace BITFALL
|
||||
// {
|
||||
//
|
||||
// /// <summary>
|
||||
// /// 玩家装备容器
|
||||
// /// 支持,护甲,头盔和背包等
|
||||
// /// </summary>
|
||||
// [CustomType(typeof(IEntityEquipmentContainer))]
|
||||
// public class PlayerEquipContainer : EntityComponent, IEntityEquipmentContainer
|
||||
// {
|
||||
// private Dictionary<IEquipmentSlot, IBasicItem> Equipment = new();
|
||||
// [Inject]
|
||||
// private IBasicItemContainer inventory;
|
||||
// public override void OnAwake()
|
||||
// {
|
||||
// var health = entity.Get<IHealth>();
|
||||
// health.OnSetAlive += OnSetAlive;
|
||||
// }
|
||||
//
|
||||
// private void OnSetAlive(bool obj)
|
||||
// {
|
||||
// if (obj) return;
|
||||
// foreach (var x in Equipment.ToArray())
|
||||
// {
|
||||
// OnDeEquip?.Invoke(x.Key, x.Value);
|
||||
// inventory.Add(x.Value);
|
||||
// }
|
||||
// Equipment.Clear();
|
||||
// }
|
||||
//
|
||||
// public override void OnStart()
|
||||
// {
|
||||
// base.OnStart();
|
||||
// inventory = entity.Get<IBasicItemContainer>();
|
||||
// var playerInventory = entity.Get<IPlayerInventory>();
|
||||
// playerInventory.OnUseItem += TryExecute;
|
||||
// }
|
||||
//
|
||||
// public Action<IEquipmentSlot, IBasicItem> OnEquip { get; set; }
|
||||
// public Action<IEquipmentSlot, IBasicItem> OnDeEquip { get; set; }
|
||||
//
|
||||
// public bool TryDeEquip<T>(T slot) where T : IEquipmentSlot
|
||||
// {
|
||||
// if (!Equipment.TryGetValue(slot, out var equipable)) return false;
|
||||
// if (inventory.Add(equipable))
|
||||
// {
|
||||
// DeEquip(slot, equipable);
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
// private bool Equip(IEquipmentSlot slot, IBasicItem item)
|
||||
// {
|
||||
// Equipment.Add(slot, item);
|
||||
// OnEquip?.Invoke(slot, item);
|
||||
// return true;
|
||||
// }
|
||||
// private bool DeEquip(IEquipmentSlot slot, IBasicItem item)
|
||||
// {
|
||||
// Equipment.Remove(slot);
|
||||
// OnDeEquip?.Invoke(slot, item);
|
||||
// return true;
|
||||
// }
|
||||
// public int Priority => 0;
|
||||
//
|
||||
// public bool TryExecute(IBasicItem value)
|
||||
// {
|
||||
// var asset = value.GetAssetable();
|
||||
// //尝试获取可装备信息
|
||||
// if (!asset.TryGetProperty<EquipmentAsSlot>(out var equipable)) return false;
|
||||
// //已装备物品
|
||||
// if (Equipment.TryGetValue(equipable.slot, out var equipedItem))
|
||||
// {
|
||||
// //尝试将装配放回背包
|
||||
// if (inventory.Add(equipedItem))
|
||||
// {
|
||||
// //移除已装备物品
|
||||
// DeEquip(equipable.slot, value);
|
||||
// }
|
||||
// }
|
||||
// //从库存中移除物品
|
||||
// if (inventory.Remove(value))
|
||||
// {
|
||||
// //装配物品
|
||||
// Equip(equipable.slot, value);
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
@@ -39,43 +39,24 @@ namespace BITFALL
|
||||
}
|
||||
private void OnActive(ISelectable obj)
|
||||
{
|
||||
if (obj.Transform.TryGetComponentAny<WorldableItem>(out var item))
|
||||
if (!obj.Transform.TryGetComponentAny<WorldItem>(out var item)) return;
|
||||
var _item = item.Pick();
|
||||
if(item.GetAssetable().TryGetProperty<Improvisable>(out _))
|
||||
{
|
||||
var _item = item.Pick();
|
||||
if(item.GetAssetable().TryGetProperty<Improvisable>(out _))
|
||||
if (_knockdown is not null && _knockdown.IsKnockdown)
|
||||
{
|
||||
if (_knockdown is not null && _knockdown.IsKnockdown)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (_improvisedService.TryEquipImprovisedItem(_item))
|
||||
{
|
||||
item.Picked();
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if (Add(_item))
|
||||
if (_improvisedService.TryEquipImprovisedItem(_item))
|
||||
{
|
||||
item.Picked();
|
||||
}
|
||||
}
|
||||
// else if(obj.Transform.TryGetComponentAny<IBasicItemContainer>(out _))
|
||||
// {
|
||||
//
|
||||
// }
|
||||
else if (Add(_item))
|
||||
{
|
||||
item.Picked();
|
||||
}
|
||||
}
|
||||
public bool TryUseItem(IBasicItem item)
|
||||
{
|
||||
if (OnUseItem is null) return false;
|
||||
return OnUseItem.CastAsFunc().Any(func => func.Invoke(item)) && UseItem(item);
|
||||
}
|
||||
|
||||
public event Func<IBasicItem, bool> OnUseItem;
|
||||
public bool TryUseItemCustom(IBasicItem item)
|
||||
{
|
||||
return OnUseItemCustom is not null && OnUseItemCustom.CastAsFunc().Any(func => func.Invoke(item));
|
||||
}
|
||||
|
||||
public event Func<IBasicItem, bool> OnUseItemCustom;
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[UnityEditor.CustomEditor(typeof(PlayerInventory))]
|
||||
|
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BITFALL.Entities.Inventory;
|
||||
using BITFALL.Items;
|
||||
using BITKit;
|
||||
using BITKit.Entities;
|
||||
using BITKit.Selection;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITFALL.Player.Inventory
|
||||
{
|
||||
[CustomType(typeof(IEntitySwapItem))]
|
||||
public class PlayerInventorySwap : EntityComponent,IEntitySwapItem
|
||||
{
|
||||
public bool TryGetCurrentContainer(out IBasicItemContainer container)
|
||||
{
|
||||
container = _currentContainer;
|
||||
return container is not null;
|
||||
}
|
||||
|
||||
public event Func<IBasicItemContainer, bool> OpenSwapFactory;
|
||||
public event Action<IBasicItemContainer> OnSwapOpened;
|
||||
public event Action<IBasicItemContainer> OnSwapClosed;
|
||||
|
||||
[Inject] private ISelector _selector;
|
||||
[Inject] private IHealth _health;
|
||||
[Inject] private IEntityInventory _inventory;
|
||||
|
||||
private IBasicItemContainer _currentContainer;
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
base.OnStart();
|
||||
_selector.OnActive += OnActive;
|
||||
_health.OnSetAlive += OnSetAlive;
|
||||
}
|
||||
|
||||
private void OnSetAlive(bool obj)
|
||||
{
|
||||
if (obj is false)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnActive(ISelectable obj)
|
||||
{
|
||||
if (_currentContainer is not null) return;
|
||||
if (obj.Transform.TryGetComponent<IBasicItemContainer>(out var container) is false) return;
|
||||
Open(container);
|
||||
}
|
||||
|
||||
public bool Add(IBasicItem item)
|
||||
{
|
||||
if (_currentContainer is null) return false;
|
||||
return _inventory.Add(item) && _currentContainer.Remove(item);
|
||||
}
|
||||
public bool Remove(IBasicItem item)
|
||||
{
|
||||
if (_currentContainer is null) return false;
|
||||
return _currentContainer.Add(item) && _inventory.Remove(item);
|
||||
}
|
||||
|
||||
public bool Open(IBasicItemContainer container)
|
||||
{
|
||||
if (_currentContainer is not null) return false;
|
||||
if (OpenSwapFactory.CastAsFunc().Any(x=>x.Invoke(container) is false)) return false;
|
||||
_currentContainer = container;
|
||||
OnSwapOpened?.Invoke(_currentContainer);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Close()
|
||||
{
|
||||
if (_currentContainer is null) return false;
|
||||
OnSwapClosed?.Invoke(_currentContainer);
|
||||
_currentContainer = null;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user