1
This commit is contained in:
@@ -3,14 +3,17 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BITFALL.Entities.Equipment;
|
||||
using BITFALL.Entities.Inventory;
|
||||
using BITFALL.Player.Inventory;
|
||||
using UnityEngine;
|
||||
using BITKit;
|
||||
using BITKit.Entities;
|
||||
using Unity.IO.LowLevel.Unsafe;
|
||||
using UnityEditor.Graphs;
|
||||
|
||||
namespace BITFALL
|
||||
{
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 玩家装备容器
|
||||
/// 支持,护甲,头盔和背包等
|
||||
@@ -18,9 +21,11 @@ namespace BITFALL
|
||||
[CustomType(typeof(IEntityEquipmentContainer))]
|
||||
public class EntityEquipmentContainer : EntityComponent, IEntityEquipmentContainer
|
||||
{
|
||||
private readonly Dictionary<IEquipmentSlot, IBasicItem> dictionary = new();
|
||||
[Inject]
|
||||
private IBasicItemContainer inventory;
|
||||
public IDictionary<IEquipmentSlot, IBasicItem> Equipment { get; } =
|
||||
new Dictionary<IEquipmentSlot, IBasicItem>();
|
||||
|
||||
[Inject] private IEntityInventory _inventory;
|
||||
|
||||
public override void OnAwake()
|
||||
{
|
||||
var health = entity.Get<IHealth>();
|
||||
@@ -30,20 +35,19 @@ namespace BITFALL
|
||||
private void OnSetAlive(bool obj)
|
||||
{
|
||||
if (obj) return;
|
||||
foreach (var x in dictionary.ToArray())
|
||||
foreach (var x in Equipment.ToArray())
|
||||
{
|
||||
OnDeEquip?.Invoke(x.Key, x.Value);
|
||||
inventory.Add(x.Value);
|
||||
_inventory.Add(x.Value);
|
||||
}
|
||||
dictionary.Clear();
|
||||
|
||||
Equipment.Clear();
|
||||
}
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
base.OnStart();
|
||||
inventory = entity.Get<IBasicItemContainer>();
|
||||
var playerInventory = entity.Get<IPlayerInventory>();
|
||||
playerInventory.OnUseItem += TryExecute;
|
||||
_inventory.TryUseItemFactory += TryExecute;
|
||||
}
|
||||
|
||||
public Action<IEquipmentSlot, IBasicItem> OnEquip { get; set; }
|
||||
@@ -51,49 +55,66 @@ namespace BITFALL
|
||||
|
||||
public bool TryDeEquip<T>(T slot) where T : IEquipmentSlot
|
||||
{
|
||||
if (!dictionary.TryGetValue(slot, out var equipable)) return false;
|
||||
if (inventory.Add(equipable))
|
||||
if (!Equipment.TryGetAny(x => x.Key.GetType().IsInstanceOfType(slot), out var pair)) return false;
|
||||
if (_inventory.Add(pair.Value))
|
||||
{
|
||||
DeEquip(slot, equipable);
|
||||
DeEquip(slot, pair.Value);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryUseEquip<T>(T slot) where T : IEquipmentSlot
|
||||
{
|
||||
return TryUseEquip(System.Activator.CreateInstance(typeof(T)) as IEquipmentSlot);
|
||||
}
|
||||
|
||||
public bool TryUseEquip(IEquipmentSlot slot)
|
||||
{
|
||||
if (!Equipment.TryGetAny(x => x.Key.GetType().IsInstanceOfType(slot), out var pair)) return false;
|
||||
if (!Equipment.TryRemove(pair.Key)) return false;
|
||||
OnDeEquip?.Invoke(pair.Key, pair.Value);
|
||||
_inventory.UseItem(pair.Value);
|
||||
|
||||
if (_inventory.TryGetItem(x => x.AddressablePath == pair.Value.AddressablePath, out var item))
|
||||
{
|
||||
Equip(pair.Key, item);
|
||||
_inventory.Remove(item);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool Equip(IEquipmentSlot slot, IBasicItem item)
|
||||
{
|
||||
dictionary.Add(slot, item);
|
||||
if (Equipment.TryAdd(slot, item) is false) return false;
|
||||
OnEquip?.Invoke(slot, item);
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool DeEquip(IEquipmentSlot slot, IBasicItem item)
|
||||
{
|
||||
dictionary.Remove(slot);
|
||||
if (Equipment.TryRemove(slot) is false) return false;
|
||||
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 (!asset.TryGetProperty<EquipmentAsSlot>(out var equipmentAsSlot)) return false;
|
||||
//已装备物品
|
||||
if (dictionary.TryGetValue(equipable.slot, out var equipedItem))
|
||||
if (Equipment.TryGetValue(equipmentAsSlot.slot, out _))
|
||||
{
|
||||
//尝试将装配放回背包
|
||||
if (inventory.Add(equipedItem))
|
||||
{
|
||||
//移除已装备物品
|
||||
DeEquip(equipable.slot, value);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//从库存中移除物品
|
||||
if (inventory.Remove(value))
|
||||
{
|
||||
//装配物品
|
||||
Equip(equipable.slot, value);
|
||||
}
|
||||
return false;
|
||||
//装配物品
|
||||
Equip(equipmentAsSlot.slot, value);
|
||||
_inventory.Remove(value);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user