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,IEntityInventoryCallback,IPlayerEquipSelector { [Header(Constant.Header.Components)] public EntityEquipment equipment; [Header(Constant.Header.InternalVariables)] private readonly Dictionary equips=new(); private IBasicItemContainer inventory; public event Action OnEquip; public event Action OnDeEquip; public event Action> OnUpdateEquip; private IBasicItem currentEquip; public override void OnAwake() { var health = entity.Get(); health.OnSetAlive += OnSetAlive; OnDeEquip += DeEquip; OnEquip += Equip; } public override void OnStart() { base.OnStart(); entity.RegisterCallback>(this); inventory = entity.Get(); } 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.Priority => 0; bool TaskSubscriber.TryExecute(IBasicItem value) { var asset = value.GetAssetable(); if (IsSupportItem(value) is false) return false; switch (asset) { case var _ when asset.TryGetProperty(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(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); } } }