BITFALL/Assets/Artists/Scripts/Entities/Equipment/EntityEquipmentContainer.cs

146 lines
4.6 KiB
C#
Raw Normal View History

2023-10-20 22:46:14 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BITFALL.Entities.Equipment;
2023-10-24 23:37:59 +08:00
using BITFALL.Entities.Inventory;
2023-10-20 22:46:14 +08:00
using BITFALL.Player.Inventory;
using UnityEngine;
using BITKit;
using BITKit.Entities;
2024-03-31 23:34:22 +08:00
using Cysharp.Threading.Tasks;
2023-10-20 22:46:14 +08:00
namespace BITFALL
{
2023-10-24 23:37:59 +08:00
2023-10-20 22:46:14 +08:00
/// <summary>
/// 玩家装备容器
/// 支持,护甲,头盔和背包等
/// </summary>
[CustomType(typeof(IEntityEquipmentContainer))]
2023-10-30 01:25:53 +08:00
public class EntityEquipmentContainer : EntityBehavior, IEntityEquipmentContainer
2023-10-20 22:46:14 +08:00
{
2023-10-24 23:37:59 +08:00
public IDictionary<IEquipmentSlot, IBasicItem> Equipment { get; } =
new Dictionary<IEquipmentSlot, IBasicItem>();
[Inject] private IEntityInventory _inventory;
2023-10-20 22:46:14 +08:00
public override void OnAwake()
{
2023-10-30 01:25:53 +08:00
var health = UnityEntity.Get<IHealth>();
2023-10-20 22:46:14 +08:00
health.OnSetAlive += OnSetAlive;
2024-03-31 23:34:22 +08:00
_inventory.OnAdd += OnAdd;
2024-03-29 00:58:24 +08:00
_inventory.AllowUseItemFactory += OnAllowUse;
}
2024-03-31 23:34:22 +08:00
private async void OnAdd(IBasicItem obj)
{
if (obj is null) return;
var asset = obj.GetAssetable();
if (asset.TryGetProperty<EquipmentAsSlot>(out var equipmentAsSlot) is false) return;
if (Equipment.TryGetValue(equipmentAsSlot.slot, out _)) return;
await UniTask.NextFrame();
await UniTask.NextFrame();
await UniTask.NextFrame();
if(destroyCancellationToken.IsCancellationRequested) return;
TryExecute(obj);
}
2024-03-29 00:58:24 +08:00
private bool OnAllowUse(IBasicItem arg)
{
return Equipment.Values.Any(x => x.Id == arg?.Id) ;
2023-10-20 22:46:14 +08:00
}
private void OnSetAlive(bool obj)
{
2023-11-30 00:23:23 +08:00
if (Data.Get<bool>(BITConstant.Environment.sp_keepInventory)) return;
2023-10-20 22:46:14 +08:00
if (obj) return;
2023-10-24 23:37:59 +08:00
foreach (var x in Equipment.ToArray())
2023-10-20 22:46:14 +08:00
{
OnDeEquip?.Invoke(x.Key, x.Value);
2023-10-24 23:37:59 +08:00
_inventory.Add(x.Value);
2023-10-20 22:46:14 +08:00
}
2023-10-24 23:37:59 +08:00
Equipment.Clear();
2023-10-20 22:46:14 +08:00
}
public override void OnStart()
{
base.OnStart();
2023-10-24 23:37:59 +08:00
_inventory.TryUseItemFactory += TryExecute;
2023-10-20 22:46:14 +08:00
}
public Action<IEquipmentSlot, IBasicItem> OnEquip { get; set; }
public Action<IEquipmentSlot, IBasicItem> OnDeEquip { get; set; }
public bool TryDeEquip<T>(T slot) where T : IEquipmentSlot
{
2023-10-24 23:37:59 +08:00
if (!Equipment.TryGetAny(x => x.Key.GetType().IsInstanceOfType(slot), out var pair)) return false;
if (_inventory.Add(pair.Value))
2023-10-20 22:46:14 +08:00
{
2023-10-24 23:37:59 +08:00
DeEquip(slot, pair.Value);
2023-10-20 22:46:14 +08:00
}
2023-10-24 23:37:59 +08:00
2023-10-20 22:46:14 +08:00
return false;
}
2023-10-24 23:37:59 +08:00
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);
2024-03-29 00:58:24 +08:00
//自动补齐
2023-10-24 23:37:59 +08:00
if (_inventory.TryGetItem(x => x.AddressablePath == pair.Value.AddressablePath, out var item))
{
Equip(pair.Key, item);
_inventory.Remove(item);
}
return true;
}
2023-10-20 22:46:14 +08:00
private bool Equip(IEquipmentSlot slot, IBasicItem item)
{
2023-10-24 23:37:59 +08:00
if (Equipment.TryAdd(slot, item) is false) return false;
2023-10-20 22:46:14 +08:00
OnEquip?.Invoke(slot, item);
return true;
}
2023-10-24 23:37:59 +08:00
2023-10-20 22:46:14 +08:00
private bool DeEquip(IEquipmentSlot slot, IBasicItem item)
{
2023-10-24 23:37:59 +08:00
if (Equipment.TryRemove(slot) is false) return false;
2023-10-20 22:46:14 +08:00
OnDeEquip?.Invoke(slot, item);
return true;
}
2023-10-24 23:37:59 +08:00
2023-10-20 22:46:14 +08:00
public int Priority => 0;
public bool TryExecute(IBasicItem value)
{
var asset = value.GetAssetable();
//尝试获取可装备信息
2023-10-24 23:37:59 +08:00
if (!asset.TryGetProperty<EquipmentAsSlot>(out var equipmentAsSlot)) return false;
2023-10-20 22:46:14 +08:00
//已装备物品
2024-03-31 23:34:22 +08:00
if (Equipment.TryGetValue(equipmentAsSlot.slot, out var current))
2023-10-20 22:46:14 +08:00
{
2024-03-31 23:34:22 +08:00
if (_inventory.Add(current) is false)
return false;
Equipment.Remove(equipmentAsSlot.slot);
OnDeEquip?.Invoke(equipmentAsSlot.slot, current);
2023-10-20 22:46:14 +08:00
}
2023-10-24 23:37:59 +08:00
//装配物品
Equip(equipmentAsSlot.slot, value);
_inventory.Remove(value);
return true;
2023-10-20 22:46:14 +08:00
}
}
}