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 Sirenix.OdinInspector.Editor; namespace BITFALL { public interface IEquipSelectorCallback { void OnEquip(IBasicItem item); void OnDeEquip(IBasicItem item); void OnUpdateEquiped(IDictionary maps); } public class PlayerEquipSelector : EntityInputComponent, IHealthCallback,IExcutor,IEntityInventoryCallback { [Header(Constant.Header.Input)] public InputActionReference primaryAction; public InputActionReference secondaryAction; public InputActionReference tertiaryAction; public InputActionReference quaternaryAction; public InputActionReference holsterAction; public InputActionGroup inputActionGroup; [Header(Constant.Header.Components)] public EntityEquipment equipment; [Header(Constant.Header.InternalVariables)] private readonly Dictionary equips=new(); private IBasicItemContainer inventory; public override void OnStart() { base.OnStart(); inputActionGroup.RegisterCallback(primaryAction, OnPrimary); inputActionGroup.RegisterCallback(secondaryAction, OnSecondary); inputActionGroup.RegisterCallback(tertiaryAction, OnMelee); inputActionGroup.RegisterCallback(quaternaryAction, OnQuaternary); inputActionGroup.RegisterCallback(holsterAction, OnHolster); entity.RegisterCallback>(this); inventory = entity.Get(); } public override void OnPrimary(InputAction.CallbackContext context) { if (!context.started) return; TryEquip(1); } public override void OnSecondary(InputAction.CallbackContext context) { if (!context.started) return; TryEquip(2); } public override void OnMelee(InputAction.CallbackContext context) { if (TryEquip(3)) { } else { TryEquip(-1); } } public override void OnTertiary(InputAction.CallbackContext context) { TryEquip(4); } void OnHolster(InputAction.CallbackContext context) { TryEquip(-1); } public override void OnSpawn() { base.OnSpawn(); if (isLocalPlayer) { inputActionGroup.allowInput.SetElements(this, true); } else { inputActionGroup.allowInput.SetDisableElements(this, true); } } public override void OnDespawn() { base.OnDespawn(); if (isLocalPlayer) { inputActionGroup.allowInput.SetElements(this, false); } else { inputActionGroup.allowInput.SetDisableElements(this, false); } } void IHealthCallback.OnSetAlive(bool alive) { inputActionGroup.allowInput.SetElements(nameof(IHealthCallback), alive); if (alive is false) { foreach (var x in equips) { } equips.Clear(); TryEquip(-1); } } void IHealthCallback.OnSetHP(int hp) { } public bool TryExcute(IBasicItem value) { var asset = value.GetAssetable(); if (IsSupportItem(value) is false) return false; var i = 1; var length = 4; if (value.TryGetProperty(out _)) { i = 1; length = 2; } else if (value.TryGetProperty(out _)) { i = 3; length = 3; }else if (value.TryGetProperty(out _)) { i = 4; length = 4; } for (; i <=length; i++) { if (equips.ContainsKey(i) is true) continue; equips.Add(i, value); if (equips.ContainsKey(0) is true) return true; Equip(value); UpdateEquiped(); return true; } return false; } private bool TryEquip(int index) { if (equips.TryGetValue(index, out var equip)) { Equip(equip); return true; } else if (index is -1) { equips.TryRemove(0); Equip(null); return true; } else { return false; } } private void Equip(IBasicItem item) { var index = equipment.equips.list.FindIndex(x => x?.AddressablePath == item?.AdressablePath); if (equips.TryGetValue(0, out var equipd)) { entity.GetCallbacks().ForEach(x => x.OnDeEquip(equipd)); } if (index is -1) { equips.TryRemove(0); equipment.equips.Entry(index); } else { entity.GetCallbacks().ForEach(x => x.OnEquip(item)); equips.Insert(0,item); equipment.equips.Entry(index); } UpdateEquiped(); } public void OnAdd(IBasicItem item) { } public void OnRemove(IBasicItem item) { if (IsSupportItem(item) is false) { UpdateEquiped(); } } private bool IsSupportItem(IBasicItem item) { return equipment.equips.list.FindIndex(x => x.AddressablePath == item.AdressablePath) is not -1; } void UpdateEquiped() { foreach (var x in entity.GetCallbacks()) { x.OnUpdateEquiped(new Dictionary(equips)); } } } }