293 lines
14 KiB
C#
293 lines
14 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class InventorySlotController : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler {
|
|
[SerializeField] private Sprite icon;
|
|
[SerializeField] private GameObject countText;
|
|
|
|
[SerializeField] public Transform inventoryPanel;
|
|
[SerializeField] private Transform equipPanel;
|
|
|
|
[SerializeField] public int index = -1;
|
|
[SerializeField] public bool isIndexedInventoryItem = true; // False means equip slot
|
|
[SerializeField] public ItemType equipType = ItemType.NONE;
|
|
|
|
[SerializeField] public GameObject itemPanelObject;
|
|
[SerializeField] public GameObject successEffect;
|
|
private ItemPanelController itemPanelController = null;
|
|
|
|
private Item itemInfo = null;
|
|
private ItemSpriteCollectionController itemSpriteCollectionController;
|
|
|
|
[SerializeField] private Vector2 inventoryPanelOffset;
|
|
[SerializeField] private Vector2 equipPanelOffset;
|
|
|
|
[SerializeField] private bool isClickable = true;
|
|
|
|
private GameAudioController gameAudioController;
|
|
|
|
private PlayerAnimationController playerAnimationController;
|
|
|
|
private void Awake() {
|
|
itemSpriteCollectionController = GameObject.FindGameObjectWithTag("ItemSpriteCollection").GetComponent<ItemSpriteCollectionController>();
|
|
gameAudioController = GameObject.FindGameObjectWithTag("GameAudio")
|
|
.GetComponent<GameAudioController>();
|
|
playerAnimationController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerAnimationController>();
|
|
}
|
|
|
|
public void Refresh(int index, ItemType type = ItemType.EQUIP_BOW, bool isIndexed = true) {
|
|
isIndexedInventoryItem = isIndexed;
|
|
if (isIndexed) {
|
|
this.index = index;
|
|
itemInfo = State.state.inventory.items[index];
|
|
} else if (Item.equippableItems.Contains(type) && State.state.inventory.equipped.ContainsKey(type)) {
|
|
// Equip
|
|
itemInfo = (Item)State.state.inventory.equipped[type];
|
|
equipType = type;
|
|
}
|
|
Image image = transform.GetChild(0).gameObject.GetComponent<Image>();
|
|
|
|
if (itemInfo == null) {
|
|
image.color = new Color(1f, 1f, 1f, 0f);
|
|
icon = null;
|
|
} else {
|
|
icon = itemSpriteCollectionController.itemSprites[((int)itemInfo.subType) - 1];
|
|
image.sprite = icon;
|
|
image.color = new Color(1f, 1f, 1f, 1f);
|
|
}
|
|
if (itemInfo != null && itemInfo.count > 1) {
|
|
countText.GetComponent<TextMeshProUGUI>().SetText(itemInfo.count.ToString());
|
|
} else {
|
|
countText.GetComponent<TextMeshProUGUI>().SetText("");
|
|
}
|
|
}
|
|
|
|
public void Refresh(Item item) {
|
|
// Override for slots outside of your inventory
|
|
isClickable = false;
|
|
|
|
itemInfo = item;
|
|
Image image = transform.GetChild(0).gameObject.GetComponent<Image>();
|
|
|
|
if (itemInfo == null) {
|
|
image.color = new Color(1f, 1f, 1f, 0f);
|
|
icon = null;
|
|
} else {
|
|
icon = itemSpriteCollectionController.itemSprites[((int)itemInfo.subType) - 1];
|
|
image.sprite = icon;
|
|
image.color = new Color(1f, 1f, 1f, 1f);
|
|
}
|
|
if (itemInfo != null && itemInfo.count > 1) {
|
|
countText.GetComponent<TextMeshProUGUI>().SetText(itemInfo.count.ToString());
|
|
} else {
|
|
countText.GetComponent<TextMeshProUGUI>().SetText("");
|
|
}
|
|
}
|
|
|
|
public string GetStatsWriteup() {
|
|
string writeup = "";
|
|
if (Item.equippableItems.Contains(itemInfo.data.itemType)) {
|
|
EquipStats equipStats = itemInfo.data.equipStats;
|
|
writeup =
|
|
"Required Level: " + equipStats.requiredLevel.ToString() + "\n";
|
|
|
|
if (equipStats.attack != 0) writeup += "\nAttack: " + equipStats.attack.ToString();
|
|
|
|
if (equipStats.pwr != 0) writeup += "\n Pwr: " + equipStats.pwr.ToString();
|
|
if (equipStats.dex != 0) writeup += "\n Dex: " + equipStats.dex.ToString();
|
|
if (equipStats.stb != 0) writeup += "\n Stb: " + equipStats.stb.ToString();
|
|
if (equipStats.def != 0) writeup += "\n Def: " + equipStats.def.ToString();
|
|
|
|
if (equipStats.maxHP != 0) writeup += "\n maxHP: " + equipStats.maxHP.ToString();
|
|
|
|
if (equipStats.attackPercentage != 0) writeup += "\n Attack%: " + equipStats.attackPercentage.ToString();
|
|
if (equipStats.pwrPercentage != 0) writeup += "\n Pwr%: " + equipStats.pwrPercentage.ToString();
|
|
if (equipStats.dexPercentage != 0) writeup += "\n Dex%: " + equipStats.dexPercentage.ToString();
|
|
if (equipStats.stbPercentage != 0) writeup += "\n Stb%: " + equipStats.stbPercentage.ToString();
|
|
if (equipStats.defPercentage != 0) writeup += "\n Def%: " + equipStats.defPercentage.ToString();
|
|
|
|
if (equipStats.maxHPPercentage != 0) writeup += "\n maxHP%: " + equipStats.maxHPPercentage.ToString();
|
|
|
|
writeup += "\n Enhancements: " + equipStats.numEnhancements.ToString() + " / " + (equipStats.numEnhancements + equipStats.remainingEnhancements).ToString();
|
|
} else if (itemInfo.data.itemType == ItemType.USE) {
|
|
UseStats useStats = itemInfo.data.useStats;
|
|
if (useStats.hp != 0) writeup += "\n HP: " + useStats.hp.ToString();
|
|
if (useStats.exp != 0) writeup += "\n EXP: " + useStats.exp.ToString();
|
|
|
|
if (useStats.hpPercentage != 0) writeup += "\n HP%: " + useStats.hpPercentage.ToString();
|
|
if (useStats.expPercentage != 0) writeup += "\n EXP%: " + useStats.expPercentage.ToString();
|
|
|
|
if (useStats.availableStatPoints != 0) writeup += "\n Stat Points: " + useStats.availableStatPoints.ToString();
|
|
|
|
} else if (itemInfo.data.itemType == ItemType.ETC) {
|
|
// Hah.
|
|
}
|
|
return writeup;
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData) {
|
|
if (eventData.pointerCurrentRaycast.gameObject != null &&
|
|
eventData.pointerCurrentRaycast.gameObject.CompareTag("InventoryIcon")) {
|
|
Debug.Log("Enter");
|
|
}
|
|
if (itemInfo != null) {
|
|
gameAudioController.audioSource.PlayOneShot(
|
|
gameAudioController.UI_Inv_ItemHover);
|
|
itemPanelController = Instantiate(itemPanelObject).GetComponent<ItemPanelController>();
|
|
if (isIndexedInventoryItem) {
|
|
itemPanelController.transform.SetParent(inventoryPanel);
|
|
itemPanelController.transform.position = new Vector3(
|
|
inventoryPanel.transform.position.x + inventoryPanelOffset.x,
|
|
inventoryPanel.transform.position.y + inventoryPanelOffset.y,
|
|
itemPanelController.transform.position.z);
|
|
} else {
|
|
itemPanelController.transform.SetParent(equipPanel);
|
|
itemPanelController.transform.position = new Vector3(
|
|
equipPanel.transform.position.x + equipPanelOffset.x,
|
|
equipPanel.transform.position.y + equipPanelOffset.y,
|
|
itemPanelController.transform.position.z);
|
|
}
|
|
itemPanelController.transform.localScale = Vector3.one;
|
|
itemPanelController.itemNameText.SetText(itemInfo.data.name);
|
|
itemPanelController.itemDescriptionText.SetText(itemInfo.data.description);
|
|
itemPanelController.itemStatsText.SetText(GetStatsWriteup());
|
|
}
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData) {
|
|
Debug.Log("Exit");
|
|
if (itemPanelController != null) {
|
|
Destroy(itemPanelController.gameObject);
|
|
itemPanelController = null;
|
|
}
|
|
}
|
|
|
|
public void SpawnSuccessEffect() {
|
|
GameObject effect = Instantiate(successEffect);
|
|
effect.transform.SetParent(transform);
|
|
effect.transform.position = new Vector2(transform.position.x + 8, transform.position.y - 8);
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData data) {
|
|
if (!isClickable) return;
|
|
if (data.button == PointerEventData.InputButton.Left) {
|
|
// Drop if non-equip.
|
|
// Equip if non-equipped equip. Dequip if equipped equip.
|
|
if (isIndexedInventoryItem) {
|
|
Item item = State.state.inventory.items[index];
|
|
if (item != null) {
|
|
if (Item.equippableItems.Contains(item.data.itemType)) {
|
|
if (State.state.stats.level >= item.data.equipStats.requiredLevel || true) {
|
|
if (!playerAnimationController.isIdling()) {
|
|
Debug.Log("Can't equip, stop moving!");
|
|
return;
|
|
}
|
|
// If this is an indexed slot with an equip in it, try to equip.
|
|
State.state.inventory.EquipItem(index);
|
|
playerAnimationController.ChangeEquip(item.data.itemType, item.subType);
|
|
Destroy(itemPanelController.gameObject);
|
|
itemPanelController = null;
|
|
gameAudioController.audioSource.PlayOneShot(
|
|
gameAudioController.UI_Inv_Equip);
|
|
SpawnSuccessEffect();
|
|
} else {
|
|
Debug.LogError("You don't meet the level requirement to equip that.");
|
|
gameAudioController.audioSource.PlayOneShot(
|
|
gameAudioController.UI_Inv_EquipFail);
|
|
}
|
|
} else if (item.data.itemType == ItemType.USE) {
|
|
State.state.inventory.UseItem(index);
|
|
SpawnSuccessEffect();
|
|
} else if (itemInfo != null) {
|
|
// Drop item.
|
|
State.state.inventory.DropItem(index, 1);
|
|
gameAudioController.audioSource.PlayOneShot(
|
|
gameAudioController.UI_Inv_Drop);
|
|
SpawnSuccessEffect();
|
|
if (item == null ||
|
|
item.count == 0) {
|
|
Debug.Log("All gone!");
|
|
Destroy(itemPanelController.gameObject);
|
|
itemPanelController = null;
|
|
} else {
|
|
Debug.Log("Not all gone: " + item.count.ToString());
|
|
}
|
|
}
|
|
}
|
|
} else if (itemInfo != null) {
|
|
// If this is an equip slot, try to dequip.
|
|
if (State.state.inventory.DequipItem(equipType)) {
|
|
Destroy(itemPanelController.gameObject);
|
|
playerAnimationController.ChangeEquip(equipType, ItemSubType.NONE);
|
|
itemPanelController = null;
|
|
gameAudioController.audioSource.PlayOneShot(
|
|
gameAudioController.UI_Inv_Dequip);
|
|
SpawnSuccessEffect();
|
|
} else {
|
|
// TODO: Add visual effect
|
|
gameAudioController.audioSource.PlayOneShot(
|
|
gameAudioController.UI_Inv_DequipFail);
|
|
}
|
|
}
|
|
} else if (data.button == PointerEventData.InputButton.Right) {
|
|
// Drop-all if not an equipped equip.
|
|
if (isIndexedInventoryItem) {
|
|
Item item = State.state.inventory.items[index];
|
|
if (item != null && itemInfo != null) {
|
|
SpawnSuccessEffect();
|
|
State.state.inventory.DropItem(index, item.count);
|
|
if (item == null ||
|
|
item.count == 0) {
|
|
Debug.Log("All gone!");
|
|
Destroy(itemPanelController.gameObject);
|
|
itemPanelController = null;
|
|
} else {
|
|
Debug.Log("Not all gone: " + item.count.ToString());
|
|
}
|
|
}
|
|
}
|
|
} else if (data.button == PointerEventData.InputButton.Middle) {
|
|
if (itemInfo != null) {
|
|
Item item = State.state.inventory.items[index];
|
|
if (item.Enhance()) {
|
|
// TODO: Fix enhancing all items in inv
|
|
State.state.inventory.items[index] = item;
|
|
SpawnSuccessEffect();
|
|
switch (item.data.enhancementStats.tier) {
|
|
case 1:
|
|
gameAudioController.audioSource.PlayOneShot(
|
|
gameAudioController.UI_Inv_EnhanceTier1);
|
|
break;
|
|
case 2:
|
|
gameAudioController.audioSource.PlayOneShot(
|
|
gameAudioController.UI_Inv_EnhanceTier2);
|
|
break;
|
|
case 3:
|
|
gameAudioController.audioSource.PlayOneShot(
|
|
gameAudioController.UI_Inv_EnhanceTier3);
|
|
break;
|
|
default: break;
|
|
}
|
|
} else {
|
|
gameAudioController.audioSource.PlayOneShot(
|
|
gameAudioController.UI_Inv_EnhanceFail);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Trigger a RefreshAll.
|
|
GameObject inventoryPanelObject = GameObject.FindGameObjectWithTag("InventoryPanel");
|
|
if (inventoryPanelObject && inventoryPanelObject.activeSelf) {
|
|
inventoryPanelObject.GetComponent<InventoryPanelController>().RefreshAll();
|
|
}
|
|
GameObject equipPanelObject = GameObject.FindGameObjectWithTag("EquipPanel");
|
|
if (equipPanelObject && equipPanelObject.activeSelf) {
|
|
equipPanelObject.GetComponent<EquipPanelController>().RefreshAll();
|
|
}
|
|
}
|
|
}
|