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 UnityEngine; namespace BITFALL.Entities.Armor { [CustomType(typeof(IArmor))] public class EntityArmor : EntityBehavior,IArmor { private int _armor; public int Armor { get => _armor; set=>OnArmorChanged?.Invoke(_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] private IPlayerEquipSelector _playerEquipSelector; [Inject] private IEntityInventory _inventory; [Inject] private IEntityEquipmentContainer _equipmentContainer; private IBasicItem _currentArmor; public override void OnAwake() { base.OnAwake(); _health.OnDamageFactory += OnDamageFactory; _inventory.OnUsedItem += OnUsedItem; _equipmentContainer.OnEquip += OnEquip; _equipmentContainer.OnDeEquip += OnDeEquip; _playerEquipSelector.TryEquipFactory += OnTryEquip; } 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) return damage; if (Armor is 0) return damage; if (damage > Armor) { Armor = 0; return damage-Armor; } Armor -= damage; return 0; } } }