This commit is contained in:
CortexCore
2023-10-24 23:37:59 +08:00
parent 325f63d6bc
commit 3e39e627bc
388 changed files with 29043 additions and 889 deletions

View File

@@ -11,6 +11,7 @@ using System.Linq;
using System.Threading.Tasks;
using BITFALL.Entities;
using BITFALL.Entities.Improvised;
using BITFALL.Entities.Inventory;
using BITFALL.Player.Inventory;
using BITKit.Entities.Player;
using Cysharp.Threading.Tasks;
@@ -27,13 +28,11 @@ namespace BITFALL.Entities.Equipment
[Header(Constant.Header.InternalVariables)]
private readonly Dictionary<int, IBasicItem> equips=new();
public event Func<IBasicItem, bool> OnTryEquip;
public event Func<IBasicItem, bool> TryEquipFactory;
public event Action<IDictionary<int, IBasicItem>> OnUpdateEquip;
[Inject(true)]
private IPlayerInventory _playerInventory;
[Inject]
private IBasicItemContainer _inventory;
private IEntityInventory _inventory;
[Inject(true)]
private IKnockdown _knockdown;
[Inject]
@@ -44,6 +43,8 @@ namespace BITFALL.Entities.Equipment
private IEntityEquipment _equipment;
[Inject(true)]
private ImprovisedServiceInterface _improvisedService;
[Inject]
private IEntityEquipmentContainer _equipmentContainer;
private readonly DoubleBuffer<IBasicItem> _cachedItem=new();
@@ -68,22 +69,17 @@ namespace BITFALL.Entities.Equipment
_improvisedService.OnTryUnEquipImprovisedItem += OnTryUnEquipImprovisedItem;
}
}
public override void OnStart()
{
base.OnStart();
if (_playerInventory is not null)
{
_playerInventory.OnUseItem += TryExecute;
_playerInventory.OnUseItemCustom += TryUseItemCustom;
_inventory.OnUsed += OnUsed;
_inventory.OnAdd += OnAdd;
_inventory.OnRemove += OnRemove;
}
_inventory.TryUseItemFactory += TryEquip;
_inventory.OnUsedItem += OnEquip;
_inventory.OnAdd += OnAdd;
_inventory.OnRemove += OnRemove;
}
private bool OnTryUnEquipImprovisedItem(IBasicItem arg)
{
@@ -135,7 +131,7 @@ namespace BITFALL.Entities.Equipment
public void OnQuaternary(InputAction.CallbackContext context)
{
if (context is not {interaction:PressInteraction ,performed:true}) return;
Equip(4);
Equip<EquipmentAsArmorPlate>();
}
public void OnHolster(InputAction.CallbackContext context)
{
@@ -153,7 +149,7 @@ namespace BITFALL.Entities.Equipment
UpdateEquip();
Equip(-1);
}
private bool TryExecute(IBasicItem value)
private bool TryEquip(IBasicItem value)
{
if (_knockdown is not null && _knockdown.IsKnockdown) return false;
var asset = value.GetAssetable();
@@ -167,31 +163,21 @@ namespace BITFALL.Entities.Equipment
UpdateEquip();
_improvisedService?.TryUnEquipImprovised(out _);
currentEquip = value;
_inventory.UseItem(value);
return true;
}
break;
}
return false;
}
private bool TryUseItemCustom(IBasicItem value)
{
if (_knockdown is not null && _knockdown.IsKnockdown) return false;
var asset = value.GetAssetable();
if (_equipment.IsSupportItem(value) is false) return false;
switch (asset)
{
case var _ when asset.TryGetProperty<EquipmentUseItem>(out _):
if (OnTryEquip?.CastAsFunc().Any(x => x.Invoke(value)) is false) return false;
_equipment.EntryEquip(value);
_improvisedService?.TryUnEquipImprovised(out _);
if (currentEquip is not null)
{
_cachedItem.Release(currentEquip);
}
currentEquip = value;
return true;
if (TryEquipFactory?.CastAsFunc().Any(x => x.Invoke(value)) is false) return false;
_equipment.EntryEquip(value);
_improvisedService?.TryUnEquipImprovised(out _);
if (currentEquip is not null)
{
_cachedItem.Release(currentEquip);
}
currentEquip = value;
return true;
}
return false;
}
public async void OnAdd(IBasicItem item)
@@ -216,9 +202,9 @@ namespace BITFALL.Entities.Equipment
{
await UniTask.NextFrame();
}
if (_equipment.IsSupportItem(item))
if (_equipment.IsSupportItem(item) && item.GetAssetable().TryGetProperty<EquipmentAsWeapon>(out _))
{
_playerInventory.TryUseItem(item);
_inventory.TryUseItem(item);
}
}
catch(OperationCanceledException){}
@@ -227,17 +213,25 @@ namespace BITFALL.Entities.Equipment
public void Throw(InputAction.CallbackContext context)
{
if (context is not { interaction: HoldInteraction, performed: true }) return;
if(currentEquip is null) return;
if(equips.TryGetAny(x=>x.Value.AddressablePath == currentEquip.AddressablePath,out var pair))
switch (context)
{
if (_inventory.DropOrSpawn(currentEquip))
{
equips.Remove(pair.Key);
_equipment.EntryEquip((IBasicItem)null);
currentEquip = null;
UpdateEquip();
}
case { interaction: HoldInteraction, performed: true }:
if (currentEquip is null) return;
if (equips.TryGetAny(x => x.Value.AddressablePath == currentEquip.AddressablePath, out var pair))
{
if (_inventory.DropOrSpawn(currentEquip))
{
equips.Remove(pair.Key);
_equipment.EntryEquip((IBasicItem)null);
currentEquip = null;
UpdateEquip();
}
}
break;
case { interaction: TapInteraction, performed: true }:
Equip<EquipmentAsThrow>();
break;
}
}
public void OnRemove(IBasicItem item)
@@ -249,7 +243,7 @@ namespace BITFALL.Entities.Equipment
_blockList.Add(item.Id);
}
private void OnUsed(IBasicItem obj)
private void OnEquip(IBasicItem obj)
{
_blockList.Remove(obj.Id);
}
@@ -297,5 +291,13 @@ namespace BITFALL.Entities.Equipment
_equipment.EntryEquip(x);
return true;
}
private bool Equip<T>() where T : IEquipmentSlot
{
if (!_equipmentContainer.Equipment.TryGetAny(x => x.Key is T, out var item)) return false;
//if (!_inventory.TryUseItem(item.Value)) return false;
Equip(item.Value);
return true;
}
}
}