using System; using System.Collections; using System.Collections.Generic; using Invector.vMelee; using UnityEngine; using UnityEngine.UI; namespace Beyond { public class ConsumablesButtonController : MonoBehaviour { public bEquipArea equipArea; public Image image; public GameObject imageParent; private int currentIndex = 0; private string imagePath = ""; private bool isLocked = false; private float lockDuration = 0.5f; [SerializeField] private bItemType itemType; [SerializeField] private ImagePulser imagePulser; [SerializeField] private bool pulseImage = true; //override private float pulseSpeed = 10f; /// /// to set image properly for disabled /// private event Action onEnabled; private bool m_currentWeaponGemable = false; private bItem m_currentWeapon = null; private bItemAttribute m_weaponPowerAttribute; private PlayerAttribute faith; // Start is called before the first frame update private void Awake() { //SetConsumableButton(); equipArea.equipSlots.ForEach(slot => slot.onAddItem.AddListener(SetConsumableButton)); equipArea.equipSlots.ForEach(slot => slot.onRemoveItem.AddListener(SetConsumableButton)); Player.Instance.ItemManager.onEquipItem.AddListener(OnEquipWeapon); Player.Instance.ItemManager.onUnequipItem.AddListener(OnUnequipWeapon); //start with 0, end on 2. On consume, equipped recheck binding here } void Start() { if (itemType == bItemType.ConsumablesFaith) { faith = Player.Instance.GetAttribute("Faith"); } } //this 2 methods are only for gems - to check if button should be visible for gemable weapon private void OnEquipWeapon(bEquipArea area, bItem weapon) { if (itemType != bItemType.Gemstones || weapon.type == bItemType.Gemstones) { return; } //Debug.Log("On Unequiped weapon in area: "+area.name+" weapon: "+weapon.name); //if current button type is gemstone, check if button should be active m_currentWeapon = area.currentEquippedItem; m_weaponPowerAttribute = m_currentWeapon.GetItemAttribute(bItemAttributes.Power); m_currentWeaponGemable = m_weaponPowerAttribute !=null; SetConsumableButton(); } private void OnUnequipWeapon(bEquipArea area, bItem weapon) { if (itemType != bItemType.Gemstones || weapon.type == bItemType.Gemstones) { return; } //Debug.Log("On Unequiped weapon in area: "+area.name+" weapon: "+weapon.name); if (m_currentWeaponGemable && weapon.GetItemAttribute(bItemAttributes.Power) !=null) { m_currentWeaponGemable = false; m_weaponPowerAttribute = null; } } public void EnablePulsing(bool e) { if (!imagePulser) return; pulseImage = e; } void Update() { if (imagePulser == null || !pulseImage) return; float value; imagePulser.speed = pulseSpeed; imagePulser.scaleAmount = 1.2f; switch (itemType) { case bItemType.Consumable: value = Player.Instance.CurrentHealth / Player.Instance.MaxHealth; if (value > 0.25) imagePulser.StopPulsing(); else imagePulser.StartPulsing(); break; case bItemType.ConsumablesFaith: value = faith.AttributeCurrentValue / faith.AttributeMaxValue; if (value > 0.25) imagePulser.StopPulsing(); else imagePulser.StartPulsing(); break; case bItemType.Gemstones: if (m_weaponPowerAttribute != null) { if (m_weaponPowerAttribute.value > 1) { imagePulser.StopPulsing(); } else { imagePulser.StartPulsing(); } } break; } } private void OnEnable() { //because unloaded in menu image needs to be reloaded again if (imagePath.Length > 1) { image.sprite = Resources.Load(imagePath); } isLocked = false; } private void OnDestroy() { if (equipArea) { equipArea.equipSlots.ForEach(slot => slot.onAddItem.RemoveListener(SetConsumableButton)); equipArea.equipSlots.ForEach(slot => slot.onRemoveItem.RemoveListener(SetConsumableButton)); Player.Instance.ItemManager.onEquipItem.RemoveListener(OnEquipWeapon); Player.Instance.ItemManager.onEquipItem.RemoveListener(OnUnequipWeapon); } } public void SetConsumableButton(bItem itemDefault = null) { // Debug.LogError("setting here"); bool hasFoundConsumable = false; int count = equipArea.equipSlots.Count; imagePath = ""; for (int i = 0; i < count; i++) { bItem item = equipArea.equipSlots[i].item; if (item != null && item.type == itemType) { TryToUnloadOldImage(); imagePath = item.iconPath; image.sprite = Resources.Load(imagePath); currentIndex = i; i = count; hasFoundConsumable = true; } } if (!hasFoundConsumable) { imageParent.SetActive(false); } //if current weapon is geamable, then show button only if button type is gemstone else if (itemType == bItemType.Gemstones) { imageParent.SetActive(m_currentWeaponGemable); } else { imageParent.SetActive(true); } onEnabled = null; } private void TryToUnloadOldImage() { if (image.sprite != null) { Resources.UnloadAsset(image.sprite); //Resources.UnloadUnusedAssets(); } } public void ConsumeItem() { if (isLocked) { return; } //short button lock would be nice here equipArea.UseItem(equipArea.equipSlots[currentIndex]); SetConsumableButton(); isLocked = true; StartCoroutine(UnlockButtonCoroutine()); } private IEnumerator UnlockButtonCoroutine() { yield return new WaitForSeconds(lockDuration); isLocked = false; } } }