BITFALL/Assets/Artists/Scripts/Entities/EquipSelector/PlayerEquipSelector.cs

301 lines
9.8 KiB
C#
Raw Normal View History

2023-09-01 14:33:54 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BITKit;
using BITKit.Entities;
using UnityEngine.InputSystem;
2023-10-20 19:31:12 +08:00
2023-09-01 14:33:54 +08:00
using System.Diagnostics;
using System.Linq;
2023-10-20 19:31:12 +08:00
using System.Threading.Tasks;
using BITFALL.Entities;
using BITFALL.Entities.Improvised;
2023-10-02 23:24:56 +08:00
using BITFALL.Player.Inventory;
2023-09-01 14:33:54 +08:00
using BITKit.Entities.Player;
2023-10-20 19:31:12 +08:00
using Cysharp.Threading.Tasks;
using JetBrains.Annotations;
using UnityEditor;
2023-09-01 14:33:54 +08:00
using UnityEngine.InputSystem.Interactions;
using Debug = UnityEngine.Debug;
2023-10-20 19:31:12 +08:00
namespace BITFALL.Entities.Equipment
2023-09-01 14:33:54 +08:00
{
[CustomType(typeof(IPlayerEquipSelector))]
2023-10-20 19:31:12 +08:00
public class PlayerEquipSelector : EntityComponent,IPlayerEquipSelector
2023-09-01 14:33:54 +08:00
{
[Header(Constant.Header.InternalVariables)]
private readonly Dictionary<int, IBasicItem> equips=new();
2023-10-20 22:46:14 +08:00
public event Func<IBasicItem, bool> OnTryEquip;
2023-09-01 14:33:54 +08:00
public event Action<IDictionary<int, IBasicItem>> OnUpdateEquip;
2023-10-20 19:31:12 +08:00
[Inject(true)]
private IPlayerInventory _playerInventory;
[Inject]
private IBasicItemContainer _inventory;
[Inject(true)]
private IKnockdown _knockdown;
[Inject]
private IHealth _health;
2023-09-01 14:33:54 +08:00
private IBasicItem currentEquip;
2023-10-20 19:31:12 +08:00
[Inject(true)]
private IEntityEquipment _equipment;
[Inject(true)]
private ImprovisedServiceInterface _improvisedService;
private readonly DoubleBuffer<IBasicItem> _cachedItem=new();
private readonly List<int> _blockList=new();
2023-09-01 14:33:54 +08:00
public override void OnAwake()
{
2023-10-20 19:31:12 +08:00
_health.OnSetAlive += OnSetAlive;
if (_knockdown is not null)
{
_knockdown.OnKnockdown +=()=>
{
Equip(null);
};
}
if (_improvisedService is not null)
{
_improvisedService.OnEquipImprovisedItem += OnEquipImprovisedItem;
_improvisedService.OnUnEquipImprovisedItem += OnUnEquipImprovisedItem;
_improvisedService.OnTryEquipImprovisedItem += OnTryEquipImprovisedItem;
_improvisedService.OnTryUnEquipImprovisedItem += OnTryUnEquipImprovisedItem;
}
2023-09-01 14:33:54 +08:00
}
2023-10-20 19:31:12 +08:00
2023-09-01 14:33:54 +08:00
public override void OnStart()
{
base.OnStart();
2023-10-20 19:31:12 +08:00
if (_playerInventory is not null)
{
_playerInventory.OnUseItem += TryExecute;
_playerInventory.OnUseItemCustom += TryUseItemCustom;
_inventory.OnUsed += OnUsed;
_inventory.OnAdd += OnAdd;
_inventory.OnRemove += OnRemove;
}
}
2023-09-01 14:33:54 +08:00
2023-10-20 19:31:12 +08:00
private bool OnTryUnEquipImprovisedItem(IBasicItem arg)
{
return true;
}
private bool OnTryEquipImprovisedItem(IBasicItem arg)
{
return true;
2023-09-01 14:33:54 +08:00
}
2023-10-20 19:31:12 +08:00
private void OnUnEquipImprovisedItem(IBasicItem obj)
{
Equip(null);
if (_cachedItem.TryGetRelease(out var item))
{
Equip(item);
}
}
private void OnEquipImprovisedItem(IBasicItem obj)
{
if (currentEquip is not null)
{
_cachedItem.Release(currentEquip);
}
Equip(obj);
}
2023-09-01 14:33:54 +08:00
public void OnPrimary(InputAction.CallbackContext context)
{
if (context is not {interaction:PressInteraction ,performed:true}) return;
2023-10-20 19:31:12 +08:00
Equip(1);
2023-09-01 14:33:54 +08:00
}
public void OnSecondary(InputAction.CallbackContext context)
{
if (context is not {interaction:PressInteraction ,performed:true}) return;
Equip(2);
}
public void OnTertiary(InputAction.CallbackContext context)
{
if (context is not {interaction:PressInteraction ,performed:true}) return;
if (Equip(3) is false)
{
Equip(-1);
}
}
public void OnQuaternary(InputAction.CallbackContext context)
{
if (context is not {interaction:PressInteraction ,performed:true}) return;
Equip(4);
}
public void OnHolster(InputAction.CallbackContext context)
{
if (context is not {interaction:PressInteraction ,performed:true}) return;
Equip(-1);
}
private void OnSetAlive(bool alive)
{
if (alive) return;
foreach (var x in equips.ToArray())
{
2023-10-20 19:31:12 +08:00
_inventory.Add(x.Value);
2023-09-01 14:33:54 +08:00
}
equips.Clear();
UpdateEquip();
Equip(-1);
}
2023-10-02 23:24:56 +08:00
private bool TryExecute(IBasicItem value)
2023-09-01 14:33:54 +08:00
{
2023-10-20 19:31:12 +08:00
if (_knockdown is not null && _knockdown.IsKnockdown) return false;
2023-09-01 14:33:54 +08:00
var asset = value.GetAssetable();
2023-10-20 19:31:12 +08:00
if (_equipment.IsSupportItem(value) is false) return false;
2023-09-01 14:33:54 +08:00
switch (asset)
{
case var _ when asset.TryGetProperty<EquipmentAsWeapon>(out _):
if (equips.TryAdd(1, value) || equips.TryAdd(2,value))
{
2023-10-20 19:31:12 +08:00
_equipment.EntryEquip(value);
2023-09-01 14:33:54 +08:00
UpdateEquip();
2023-10-20 19:31:12 +08:00
_improvisedService?.TryUnEquipImprovised(out _);
currentEquip = value;
2023-09-01 14:33:54 +08:00
return true;
}
break;
}
return false;
}
2023-10-20 19:31:12 +08:00
private bool TryUseItemCustom(IBasicItem value)
2023-09-01 14:33:54 +08:00
{
2023-10-20 19:31:12 +08:00
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 _):
2023-10-20 22:46:14 +08:00
if (OnTryEquip?.CastAsFunc().Any(x => x.Invoke(value)) is false) return false;
2023-10-20 19:31:12 +08:00
_equipment.EntryEquip(value);
_improvisedService?.TryUnEquipImprovised(out _);
2023-10-20 22:46:14 +08:00
if (currentEquip is not null)
{
_cachedItem.Release(currentEquip);
}
2023-10-20 19:31:12 +08:00
currentEquip = value;
return true;
}
return false;
}
public async void OnAdd(IBasicItem item)
{
if (_blockList.Contains(item.Id)) return;
if(currentEquip is not null && currentEquip.AddressablePath == item.AddressablePath)
{
if(equips.TryGetAny(x=>x.Value.AddressablePath == item.AddressablePath,out var pair))
{
equips.Remove(pair.Key);
UpdateEquip();
}
currentEquip = null;
_equipment.EntryEquip((IBasicItem)null);
}
else
{
try
{
for (var i = 0; i < 8; i++)
{
await UniTask.NextFrame();
}
if (_equipment.IsSupportItem(item))
{
_playerInventory.TryUseItem(item);
}
}
catch(OperationCanceledException){}
}
2023-09-01 14:33:54 +08:00
}
2023-10-20 19:31:12 +08:00
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))
{
if (_inventory.DropOrSpawn(currentEquip))
{
equips.Remove(pair.Key);
_equipment.EntryEquip((IBasicItem)null);
currentEquip = null;
UpdateEquip();
}
}
}
2023-09-01 14:33:54 +08:00
public void OnRemove(IBasicItem item)
{
2023-10-20 19:31:12 +08:00
if (_equipment.IsSupportItem(item) is false)
2023-09-01 14:33:54 +08:00
{
UpdateEquip();
}
2023-10-20 19:31:12 +08:00
_blockList.Add(item.Id);
2023-09-01 14:33:54 +08:00
}
2023-10-20 19:31:12 +08:00
private void OnUsed(IBasicItem obj)
2023-09-01 14:33:54 +08:00
{
2023-10-20 19:31:12 +08:00
_blockList.Remove(obj.Id);
2023-09-01 14:33:54 +08:00
}
private void UpdateEquip()
{
OnUpdateEquip?.Invoke(new Dictionary<int, IBasicItem>(equips));
2023-10-20 19:31:12 +08:00
_cachedItem.Clear();
2023-09-01 14:33:54 +08:00
}
public bool TryDeEquip(IBasicItem item)
{
if (item is null) return false;
if (equips.Any(x => x.Value.AddressablePath == item.AddressablePath) is false) return false;
var index = equips.Single(x=>x.Value.AddressablePath==item.AddressablePath).Key;
if (equips.TryRemove(index) is false) return false;
2023-10-20 19:31:12 +08:00
if (!_inventory.Add(item)) return false;
2023-09-01 14:33:54 +08:00
UpdateEquip();
return true;
}
2023-10-20 22:46:14 +08:00
public bool Cancel()
{
if (currentEquip is null) return false;
if (_cachedItem.TryGetRelease(out var item))
{
Equip(item);
return true;
}
Equip(null);
return false;
}
2023-09-01 14:33:54 +08:00
private void Equip(IBasicItem item)
{
2023-10-20 19:31:12 +08:00
_equipment.EntryEquip(item);
currentEquip = item;
2023-09-01 14:33:54 +08:00
}
private bool Equip(int index)
{
if (!equips.TryGetValue(index, out var x) && index is not -1) return false;
2023-10-20 19:31:12 +08:00
if (_knockdown is not null && _knockdown.IsKnockdown) return false;
currentEquip = x;
_improvisedService?.TryUnEquipImprovised(out _);
_equipment.EntryEquip(x);
2023-09-01 14:33:54 +08:00
return true;
}
}
}