275 lines
8.8 KiB
C#
275 lines
8.8 KiB
C#
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;
|
|
/// <summary>
|
|
/// to set image properly for disabled
|
|
/// </summary>
|
|
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<bEquipArea>();
|
|
bool thisSlot = false;
|
|
for (int i=0; i<equipArea.equipSlots.Count; i++)
|
|
{
|
|
if (equipArea.equipSlots[i] == equipSlot)
|
|
{
|
|
currentIndex = i;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
faith = Player.Instance.GetAttribute("Faith");
|
|
}
|
|
|
|
bool MatchesType(bItemType type)
|
|
{
|
|
return itemType == type || itemType2 == type;
|
|
}
|
|
//this 2 methods are only for gems - to check if button should be visible for gemable weapon
|
|
private void OnEquipItem(bEquipArea area, bItem item)
|
|
{
|
|
if (!MatchesType(item.type))
|
|
{
|
|
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
|
|
//check if this slot was equiped
|
|
|
|
if (area.currentEquippedItem == equipSlot.item)
|
|
{
|
|
//m_weaponPowerAttribute = item.GetItemAttribute(bItemAttributes.Power);
|
|
//m_currentWeaponGemable = m_weaponPowerAttribute !=null;
|
|
SetButton(item);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
private void OnUnequipItem(bEquipArea area, bItem item)
|
|
{
|
|
if (!MatchesType(item.type))
|
|
{
|
|
return;
|
|
}
|
|
//Debug.Log("On Unequiped weapon in area: "+area.name+" weapon: "+weapon.name);
|
|
if (m_currentWeaponGemable && item.GetItemAttribute(bItemAttributes.Power) !=null)
|
|
{
|
|
m_currentWeaponGemable = false;
|
|
m_weaponPowerAttribute = null;
|
|
}
|
|
if (item == m_currentItem)
|
|
{
|
|
SetButton(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<Sprite>(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<Sprite>(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;
|
|
}
|
|
}
|
|
} |