You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
4.0 KiB
135 lines
4.0 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class InventoryManager : MonoBehaviour
|
|
{
|
|
static InventoryManager instance;
|
|
|
|
// !!
|
|
public Inventory myBag;
|
|
public GameObject slotGrid;
|
|
// public Slot slotPrefab;
|
|
public GameObject emptySlot;
|
|
public Text itemInfomation;
|
|
public int slotIndex;
|
|
public GameObject bagImage;
|
|
public GameObject SceneChange;
|
|
// public Character maincharacter;
|
|
|
|
public List<GameObject> slots = new List<GameObject>();
|
|
|
|
GameManager gameManager;
|
|
GameObject[] pieces;
|
|
Character maincharacter;
|
|
Character selected;
|
|
|
|
void Awake()
|
|
{
|
|
if (instance != null)
|
|
Destroy(this);
|
|
instance = this;
|
|
|
|
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
|
|
pieces = GameObject.FindGameObjectsWithTag("Pieces");
|
|
foreach (var piece in pieces)
|
|
{
|
|
if(piece.GetComponent<Character>().id == 0)
|
|
{
|
|
instance.maincharacter = piece.GetComponent<Character>();
|
|
break;
|
|
}
|
|
}
|
|
instance.selected = gameManager.GetSelected();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
instance.selected = gameManager.GetSelected();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
RefreshItem();
|
|
instance.itemInfomation.text = "这里是物品描述。";
|
|
}
|
|
|
|
public static void UpdateItemInfo(string itemDescription)
|
|
{
|
|
instance.itemInfomation.text = itemDescription;
|
|
}
|
|
|
|
public static void UpdateItemIndex(int index)
|
|
{
|
|
instance.slotIndex = index;
|
|
}
|
|
|
|
public static void RefreshItem()
|
|
{
|
|
// 循环删除slotGrid下的子集物品
|
|
for (int i = 0; i < instance.slotGrid.transform.childCount; i++)
|
|
{
|
|
if(instance.slotGrid.transform.childCount == 0)
|
|
break;
|
|
Destroy(instance.slotGrid.transform.GetChild(i).gameObject);
|
|
instance.slots.Clear(); // 清空slots列表
|
|
}
|
|
|
|
// 重新生成对应背包中物品的slot
|
|
for (int i = 0; i < instance.myBag.itemList.Count; i++)
|
|
{
|
|
instance.slots.Add(Instantiate(instance.emptySlot));
|
|
instance.slots[i].transform.SetParent(instance.slotGrid.transform);
|
|
instance.slots[i].GetComponent<Slot>().slotIndex = i;
|
|
instance.slots[i].GetComponent<Slot>().SetupSlot(instance.myBag.itemList[i]);
|
|
}
|
|
}
|
|
|
|
public static void UseItem()
|
|
{
|
|
// Debug.Log(instance.myBag.itemList[instance.slotIndex].itemName);
|
|
if(instance.myBag.itemList[instance.slotIndex].usable)
|
|
{
|
|
if(instance.myBag.itemList[instance.slotIndex].itemName == "Crystal")
|
|
{
|
|
if(instance.gameManager.GetExploreMode())
|
|
{
|
|
instance.SceneChange.SetActive(true);
|
|
instance.bagImage.SetActive(false);
|
|
}
|
|
}
|
|
|
|
if(instance.myBag.itemList[instance.slotIndex].itemName == "Health")
|
|
{
|
|
instance.selected.ChangeHealth(5);
|
|
|
|
if(instance.myBag.itemList[instance.slotIndex].itemHeld <= 1)
|
|
{
|
|
instance.myBag.itemList[instance.slotIndex] = null;
|
|
}
|
|
else
|
|
{
|
|
instance.myBag.itemList[instance.slotIndex].itemHeld--;
|
|
}
|
|
RefreshItem();
|
|
}
|
|
|
|
if(instance.myBag.itemList[instance.slotIndex].itemName == "Mana")
|
|
{
|
|
instance.selected.ChangeMana(3);
|
|
|
|
if(instance.myBag.itemList[instance.slotIndex].itemHeld <= 1)
|
|
{
|
|
instance.myBag.itemList[instance.slotIndex] = null;
|
|
}
|
|
else
|
|
{
|
|
instance.myBag.itemList[instance.slotIndex].itemHeld--;
|
|
}
|
|
RefreshItem();
|
|
}
|
|
}
|
|
}
|
|
}
|