using System; using System.Collections; using System.Collections.Generic; using BITFALL.Entities.Equipment; using BITFALL.Entities.Inventory; using BITFALL.Items.Armor; using BITKit; using BITKit.Entities; using UnityEditor; using UnityEngine; using UnityEngine.UIElements; namespace BITFALL.Entities.Armor { [CustomType(typeof(IArmor))] public class EntityArmor : EntityBehavior,IArmor { [SerializeField] private Optional initialArmor; [SerializeField,HideInInspector] internal int currentArmor; private int _armor; public int Armor { get => _armor; set=>OnArmorChanged?.Invoke(currentArmor = _armor = value); } public bool TryGetCurrentArmor(out IBasicItem item) { item = _currentArmor; return _currentArmor is not null; } public event Action OnArmorChanged; public event Action OnEquipArmor; public event Action OnUnEquipArmor; [Inject] private IHealth _health; [Inject(true)] private IPlayerEquipSelector _playerEquipSelector; [Inject(true)] private IEntityInventory _inventory; [Inject(true)] private IEntityEquipmentContainer _equipmentContainer; private IBasicItem _currentArmor; public override void OnAwake() { base.OnAwake(); _health.OnDamageFactory += OnDamageFactory; if (_inventory is not null) { _inventory.OnUsedItem += OnUsedItem; } if (_equipmentContainer is not null) { _equipmentContainer.OnEquip += OnEquip; _equipmentContainer.OnDeEquip += OnDeEquip; } if (_playerEquipSelector is not null) _playerEquipSelector.TryEquipFactory += OnTryEquip; } public override void OnStart() { base.OnStart(); if (initialArmor.Allow) { Armor = initialArmor.Value; } } private bool OnTryEquip(IBasicItem arg) { if (arg is null) return true; if (arg.GetAssetable().TryGetProperty(out _)) { if (_currentArmor is null) { return false; } if (Armor == _currentArmor.GetAssetable().As().MaxArmor) { return false; } } return true; } private void OnDeEquip(IEquipmentSlot arg1, IBasicItem arg2) { if (arg1 is not EquipmentAsArmor) return; _currentArmor = null; OnUnEquipArmor?.Invoke(arg2); } private void OnEquip(IEquipmentSlot arg1, IBasicItem arg2) { if (arg1 is not EquipmentAsArmor) return; _currentArmor = arg2; OnEquipArmor?.Invoke(arg2); } private void OnUsedItem(IBasicItem obj) { if (_currentArmor?.GetAssetable() is not AssetableArmor assetableArmor) return; if (obj.GetAssetable().TryGetProperty(out var addArmor)) { Armor = Mathf.Clamp(Armor + addArmor.Armor, 0, assetableArmor.MaxArmor); } } private int OnDamageFactory(DamageMessage arg1, int damage) { if (_currentArmor is null && initialArmor.Allow is false) return damage; if (Armor is 0) return damage; if (damage > Armor) { Armor = 0; return damage-Armor; } Armor -= damage; return 0; } } #if UNITY_EDITOR [CustomEditor(typeof(EntityArmor))] public sealed class EntityArmorInspector:BITInspector { public override VisualElement CreateInspectorGUI() { FillDefaultInspector(); var label = root.Create