BITFALL/Assets/BITKit/Unity/Scripts/Entity/EquipSelector/PlayerEquipSelector.cs

164 lines
5.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BITKit;
using BITKit.Entities;
using UnityEngine.InputSystem;
using static UnityEditor.Progress;
using System.Diagnostics;
using System.Linq;
using BITKit.Entities.Player;
using UnityEngine.InputSystem.Interactions;
using Debug = UnityEngine.Debug;
namespace BITFALL
{
[CustomType(typeof(IPlayerEquipSelector))]
public class PlayerEquipSelector : EntityComponent,TaskSubscriber<IBasicItem>,IEntityInventoryCallback,IPlayerEquipSelector
{
[Header(Constant.Header.Components)]
public EntityEquipment equipment;
[Header(Constant.Header.InternalVariables)]
private readonly Dictionary<int, IBasicItem> equips=new();
private IBasicItemContainer inventory;
public event Action<IBasicItem> OnEquip;
public event Action<IBasicItem> OnDeEquip;
public event Action<IDictionary<int, IBasicItem>> OnUpdateEquip;
private IBasicItem currentEquip;
public override void OnAwake()
{
var health = entity.Get<IHealth>();
health.OnSetAlive += OnSetAlive;
OnDeEquip += DeEquip;
OnEquip += Equip;
}
public override void OnStart()
{
base.OnStart();
entity.RegisterCallback<TaskSubscriber<IBasicItem>>(this);
inventory = entity.Get<IBasicItemContainer>();
}
public void OnPrimary(InputAction.CallbackContext context)
{
if (context is not {interaction:PressInteraction ,performed:true}) return;
Equip(1);
}
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())
{
inventory.Add(x.Value);
}
equips.Clear();
UpdateEquip();
Equip(-1);
}
int TaskSubscriber<IBasicItem>.Priority => 0;
bool TaskSubscriber<IBasicItem>.TryExecute(IBasicItem value)
{
var asset = value.GetAssetable();
if (IsSupportItem(value) is false) return false;
switch (asset)
{
case var _ when asset.TryGetProperty<EquipmentAsWeapon>(out _):
if (equips.TryAdd(1, value) || equips.TryAdd(2,value))
{
OnEquip?.Invoke(value);
UpdateEquip();
return true;
}
break;
}
return false;
}
public void OnAdd(IBasicItem item)
{
}
public void OnRemove(IBasicItem item)
{
if (IsSupportItem(item) is false)
{
UpdateEquip();
}
}
private bool IsSupportItem(IBasicItem item)
{
return equipment.equips.list.Any(x => x.AddressablePath == item.AddressablePath);
}
private void UpdateEquip()
{
OnUpdateEquip?.Invoke(new Dictionary<int, IBasicItem>(equips));
}
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;
if (!inventory.Add(item)) return false;
OnDeEquip?.Invoke(item);
UpdateEquip();
return true;
}
private void Equip(IBasicItem item)
{
if (item is null)
{
equipment.equips.Entry(-1);
}
else
{
equipment.equips.Entry(x=>x.AddressablePath == item.AddressablePath);
}
}
private bool Equip(int index)
{
if (!equips.TryGetValue(index, out var x) && index is not -1) return false;
if (index is -1)
{
OnEquip?.Invoke(x);
}
return true;
}
private void DeEquip(IBasicItem item)
{
equipment.equips.Entry(-1);
}
}
}