using System; using System.Collections; using Language.Lua; using PixelCrushers.DialogueSystem; using Sirenix.Utilities; using UnityEngine; using UnityEngine.UI; using UnityStandardAssets.CrossPlatformInput; namespace Beyond { public class ItemButtonController : MonoBehaviour { public bEquipSlot equipSlot; private bEquipArea equipArea; public Image image; public GameObject imageParent; private int currentIndex = 0; private string imagePath = ""; public string InputNameToPress = "M"; private bool isLocked = false; private float lockDuration = 0.5f; [SerializeField] private bItemType itemType = 0; [SerializeField] private bItemType itemType2 = 0; [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_currentItem = null; private bItemAttribute m_weaponPowerAttribute; private PlayerAttribute faith; // Start is called before the first frame update private void Awake() { //SetConsumableButton(); equipSlot.onAddItem.AddListener(SetButton); equipSlot.onRemoveItem.AddListener(SetButton); Player.Instance.ItemManager.onEquipItem.AddListener(OnEquipItem); Player.Instance.ItemManager.onUnequipItem.AddListener(OnUnequipItem); //start with 0, end on 2. On consume, equipped recheck binding here equipArea = equipSlot.GetComponentInParent(); bool thisSlot = false; for (int i=0; i 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 (equipSlot) { equipSlot.onAddItem.RemoveListener(SetButton); equipSlot.onRemoveItem.RemoveListener(SetButton); Player.Instance.ItemManager.onEquipItem.RemoveListener(OnEquipItem); Player.Instance.ItemManager.onEquipItem.RemoveListener(OnUnequipItem); } } public void SetButton(bItem item = null) { imagePath = ""; if (item != null) { TryToUnloadOldImage(); imagePath = item.iconPath; image.sprite = Resources.Load(imagePath); } //if current weapon is geamable, then show button only if button type is gemstone if (item == null) { imageParent.SetActive(false); } else if (item.type == bItemType.Gemstones) { imageParent.SetActive(m_currentWeaponGemable); } else { imageParent.SetActive(true); } m_currentItem = item; onEnabled = null; } private void TryToUnloadOldImage() { if (image.sprite != null) { Resources.UnloadAsset(image.sprite); //Resources.UnloadUnusedAssets(); } } public void ConsumeItem() { if (isLocked) { return; } if (m_currentItem.type == bItemType.QuantaPower || m_currentItem.type == bItemType.PowerScroll) { // --- FIX STARTS HERE --- // Old logic: Player.Instance.Magic.SelectPowerBasedOnArea(currentIndex); // New Logic: Directly tell the equipment area to select this slot. // MagicAttacks automatically looks at whatever is equipped in this area. if (equipArea != null) { equipArea.SetEquipSlot(currentIndex); } // --- FIX ENDS HERE --- if (!InputNameToPress.IsNullOrWhitespace()) { CrossPlatformInputManager.SetButtonDown(InputNameToPress); CrossPlatformInputManager.SetButtonUp(InputNameToPress); } } else { equipArea.UseItem(equipArea.equipSlots[currentIndex]); } //short button lock would be nice here if (equipSlot.item == null) { SetButton(null); } else { isLocked = true; StartCoroutine(UnlockButtonCoroutine()); } } private IEnumerator UnlockButtonCoroutine() { yield return new WaitForSeconds(lockDuration); isLocked = false; } } }