324 lines
9.7 KiB
C#
324 lines
9.7 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
using UnityEngine.EventSystems;
|
|||
|
|
using TMPro;
|
|||
|
|
|
|||
|
|
public class InventoryManager : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
public static InventoryManager Instance { get; private set; }
|
|||
|
|
|
|||
|
|
private readonly Dictionary<int, Image> inventoryItemsDict = new Dictionary<int, Image>();
|
|||
|
|
private readonly Dictionary<int, Image> equipmentItemsDict = new Dictionary<int, Image>();
|
|||
|
|
private readonly Dictionary<int, Image> quickAccessItemsDict = new Dictionary<int, Image>();
|
|||
|
|
private readonly Dictionary<int, Image> bankVaultItemsDict = new Dictionary<int, Image>();
|
|||
|
|
private readonly Dictionary<int, Image> forgeItemsDict = new Dictionary<int, Image>();
|
|||
|
|
|
|||
|
|
[HideInInspector] public int[] inventoryItems = new int[24];
|
|||
|
|
public int[] inventoryItemCounts = new int[24];
|
|||
|
|
public string[] itemDescriptions = new string[24];
|
|||
|
|
private readonly Sprite[] inventory = new Sprite[24];
|
|||
|
|
|
|||
|
|
public int[] equipmentItems = new int[6];
|
|||
|
|
public int[] equipmentItemCounts = new int[6];
|
|||
|
|
private readonly Sprite[] equipment = new Sprite[6];
|
|||
|
|
|
|||
|
|
public int[] bankVaultItems = new int[25];
|
|||
|
|
public int[] bankVaultItemCounts = new int[25];
|
|||
|
|
private readonly Sprite[] bankVault = new Sprite[25];
|
|||
|
|
|
|||
|
|
public String[] point_ui_element_names = { "harold_text", "nero_text", "horde_text","spade_text" };
|
|||
|
|
public String[] point_ui_element_values = { "xHarold: 0", "xNero: 0", "xHorde: 0","NotFound" };
|
|||
|
|
|
|||
|
|
public void updatePointUIElement(string elementName, string value)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < point_ui_element_names.Length; i++)
|
|||
|
|
{
|
|||
|
|
if (point_ui_element_names[i].Equals(elementName))
|
|||
|
|
{
|
|||
|
|
Debug.Log("Inside Loop,spade_text detected");
|
|||
|
|
point_ui_element_values[i] = value;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
private void Awake()
|
|||
|
|
{
|
|||
|
|
Instance = this;
|
|||
|
|
InitializeDictionaries();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Start()
|
|||
|
|
{
|
|||
|
|
int[] m1 = new int[24];
|
|||
|
|
for (int i = 0; i < 24; i++)
|
|||
|
|
{
|
|||
|
|
m1[i] = -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int[] p0 = new int[24];
|
|||
|
|
for (int i = 0; i < 24; i++)
|
|||
|
|
{
|
|||
|
|
p0[i] = 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
updateInventory(m1, p0);
|
|||
|
|
|
|||
|
|
m1 = new int[6];
|
|||
|
|
for (int i = 0; i < 6; i++)
|
|||
|
|
{
|
|||
|
|
m1[i] = -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
p0 = new int[6];
|
|||
|
|
for (int i = 0; i < 6; i++)
|
|||
|
|
{
|
|||
|
|
p0[i] = 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
this.updateEquipment(m1, p0);
|
|||
|
|
|
|||
|
|
m1 = new int[25];
|
|||
|
|
for (int i = 0; i < 25; i++)
|
|||
|
|
m1[i] = -1;
|
|||
|
|
p0 = new int[25];
|
|||
|
|
for (int i = 0; i < 25; i++)
|
|||
|
|
p0[i] = 0;
|
|||
|
|
this.updateBankVault(m1, p0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public int GetForableItemCount(int id)
|
|||
|
|
{
|
|||
|
|
int count = 0;
|
|||
|
|
Weapon weapon = OnDemandLoader.Load<Weapon>("config/" + id);
|
|||
|
|
|
|||
|
|
int tier = (int)weapon.WeaponLevel;
|
|||
|
|
Debug.Log("tier is " + tier.ToString());
|
|||
|
|
Debug.Log(" ore count tier is " + ForgeController.oreCounts[tier].ToString());
|
|||
|
|
|
|||
|
|
if (ForgeController.oreCounts[tier] >= 100)
|
|||
|
|
{
|
|||
|
|
count++;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (ForgeController.siegePassTier >= 4)
|
|||
|
|
{
|
|||
|
|
count++;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
foreach (int itemID in inventoryItems)
|
|||
|
|
{
|
|||
|
|
if (weapon == null) continue;
|
|||
|
|
|
|||
|
|
bool isForgable = weapon.Forgeable;
|
|||
|
|
if (isForgable && itemID == id)
|
|||
|
|
{
|
|||
|
|
count += 1;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return count;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public int GetItemCount(int id)
|
|||
|
|
{
|
|||
|
|
int count = 0;
|
|||
|
|
foreach (int itemID in inventoryItems)
|
|||
|
|
{
|
|||
|
|
if (itemID == id)
|
|||
|
|
{
|
|||
|
|
count += 1;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return count;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void InitializeDictionaries()
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < 24; i++)
|
|||
|
|
inventoryItemsDict.Add(i, GameObject.Find("inventory_item_" + i).GetComponent<Image>());
|
|||
|
|
for (int i = 0; i < 6; i++)
|
|||
|
|
equipmentItemsDict.Add(i, GameObject.Find("equipment_item_" + i).GetComponent<Image>());
|
|||
|
|
for (int i = 18; i < 24; i++)
|
|||
|
|
quickAccessItemsDict.Add(i, GameObject.Find("qa_item_" + i).GetComponent<Image>());
|
|||
|
|
for (int i = 0; i < 25; i++)
|
|||
|
|
{
|
|||
|
|
GameObject bankSlot = GameObject.Find("Vault_item_" + i);
|
|||
|
|
bankVaultItemsDict.Add(i, bankSlot.GetComponent<Image>());
|
|||
|
|
|
|||
|
|
//when dragging items, the text gets dragged too.
|
|||
|
|
bankSlot.transform.GetComponentInChildren<TMP_Text>().text = "";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void updateEquipment(int[] equipmentIds, int[] equipmentCounts)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
for (int i = 0; i < 6; i++)
|
|||
|
|
{
|
|||
|
|
equipment[i] = null;
|
|||
|
|
this.equipmentItems[i] = equipmentIds[i];
|
|||
|
|
this.equipmentItemCounts[i] = equipmentCounts[i];
|
|||
|
|
equipment[i] = UIManager.Instance.RenderItem(equipmentIds[i]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//if inventory is open, update immediately.
|
|||
|
|
if (UIManager.Instance.Inventory.activeSelf)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < 6; i++)
|
|||
|
|
{
|
|||
|
|
equipmentItemsDict[i].sprite = equipment[i];
|
|||
|
|
equipmentItemsDict[i].color = equipment[i] != null ? Color.white : Color.clear;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void updateInventory(int[] itemIds, int[] count)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < 24; i++)
|
|||
|
|
{
|
|||
|
|
//clear inventory
|
|||
|
|
inventory[i] = null;
|
|||
|
|
|
|||
|
|
inventoryItems[i] = itemIds[i];
|
|||
|
|
inventoryItemCounts[i] = count[i];
|
|||
|
|
if (inventoryItems[i] != -1)
|
|||
|
|
{
|
|||
|
|
inventory[i] = UIManager.Instance.RenderItem(itemIds[i]);
|
|||
|
|
itemDescriptions[i] = ItemDef.GetInstance().getDescription(itemIds[i]);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
itemDescriptions[i] = "";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//if inventory is open, update immediately.
|
|||
|
|
if (UIManager.Instance.Inventory.activeInHierarchy)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < 24; i++)
|
|||
|
|
{
|
|||
|
|
inventoryItemsDict[i].sprite = inventory[i];
|
|||
|
|
inventoryItemsDict[i].color = inventory[i] != null ? Color.white : Color.clear;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for (int i = 18; i < 24; i++)
|
|||
|
|
{
|
|||
|
|
quickAccessItemsDict[i].sprite = inventory[i];
|
|||
|
|
quickAccessItemsDict[i].color = inventory[i] != null ? Color.white : Color.clear;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void bankVaultRefresh()
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < 25; i++)
|
|||
|
|
{
|
|||
|
|
bankVaultItemsDict[i].sprite = bankVault[i];
|
|||
|
|
bankVaultItemsDict[i].color = bankVault[i] != null ? Color.white : Color.clear;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void updateBankVault(int[] itemIds, int[] count)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < 25; i++)
|
|||
|
|
{
|
|||
|
|
bankVault[i] = null;
|
|||
|
|
bankVaultItems[i] = itemIds[i];
|
|||
|
|
bankVaultItemCounts[i] = count[i];
|
|||
|
|
TMP_Text stackCount = bankVaultItemsDict[i].GetComponentInChildren<TMP_Text>();
|
|||
|
|
stackCount.SetText("");
|
|||
|
|
|
|||
|
|
if (bankVaultItems[i] != -1)
|
|||
|
|
{
|
|||
|
|
InventoryItemStats item = OnDemandLoader.Load<GameObject>("items/" + itemIds[i])
|
|||
|
|
?.GetComponent<InventoryItemStats>();
|
|||
|
|
if (item != null)
|
|||
|
|
{
|
|||
|
|
bankVault[i] = UIManager.Instance.RenderItem(itemIds[i]);
|
|||
|
|
stackCount.SetText(count[i].ToString());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//if bank vault is open, update immediately.
|
|||
|
|
if (UIManager.Instance.getInterface<GameObject>("BankVaultCanvas").activeInHierarchy)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < 25; i++)
|
|||
|
|
{
|
|||
|
|
bankVaultItemsDict[i].sprite = bankVault[i];
|
|||
|
|
bankVaultItemsDict[i].color = bankVault[i] != null ? Color.white : Color.clear;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void showInventory()
|
|||
|
|
{
|
|||
|
|
Player.Instance.updatePlayer = false;
|
|||
|
|
UIManager.Instance.showInterface("Inventory");
|
|||
|
|
AudioManager.Instance.playSound("inventory_open");
|
|||
|
|
Player.Instance.OnlyCastPlayerShadows(false, "PlayerUICamera");
|
|||
|
|
EventSystem.current.SetSelectedGameObject(null);
|
|||
|
|
EventSystem.current.SetSelectedGameObject(GameObject.Find("inventory_item_18"));
|
|||
|
|
|
|||
|
|
for (int i = 0; i < 24; i++)
|
|||
|
|
{
|
|||
|
|
inventoryItemsDict[i].sprite = inventory[i];
|
|||
|
|
inventoryItemsDict[i].color = inventory[i] != null ? Color.white : Color.clear;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for (int i = 0; i < 6; i++)
|
|||
|
|
{
|
|||
|
|
equipmentItemsDict[i].sprite = equipment[i];
|
|||
|
|
equipmentItemsDict[i].color = equipment[i] != null ? Color.white : Color.clear;
|
|||
|
|
//Debug.Log("is EQUIPMENT NULL? " + equipment[i]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for (int i = 0; i < point_ui_element_names.Length; i++)
|
|||
|
|
{
|
|||
|
|
GameObject.Find(point_ui_element_names[i]).GetComponent<TMP_Text>().text = point_ui_element_values[i];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public void hideInventory()
|
|||
|
|
{
|
|||
|
|
UIManager.Instance.hideInterface("Inventory");
|
|||
|
|
Player.Instance.OnlyCastPlayerShadows(true);
|
|||
|
|
|
|||
|
|
Player.Instance.updatePlayer = Player.Instance.IsAlive();
|
|||
|
|
AudioManager.Instance.playSound("inventory_close");
|
|||
|
|
Player.Instance.OnlyCastPlayerShadows(Player.Instance.IsAlive(), "PlayerUI");
|
|||
|
|
|
|||
|
|
for (int i = 18; i < 24; i++)
|
|||
|
|
{
|
|||
|
|
quickAccessItemsDict[i].sprite = inventory[i];
|
|||
|
|
quickAccessItemsDict[i].color = inventory[i] != null ? Color.white : Color.clear;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void ToggleInventory()
|
|||
|
|
{
|
|||
|
|
if (!UIManager.Instance.BankVaultCanvas.activeInHierarchy)
|
|||
|
|
{
|
|||
|
|
if (UIManager.Instance.Inventory.activeInHierarchy)
|
|||
|
|
{
|
|||
|
|
hideInventory();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
showInventory();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|