Untitled
unknown
csharp
2 years ago
4.1 kB
3
Indexable
Never
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; //[System.Serializable] public class InventorySystem : MonoBehaviour { public static InventorySystem Instance; [System.Serializable] public class Combinable { public int dragID, mergeID; public Item productItem; } public GameObject inventoryUI; [Tooltip("Updates inventory in UI")] public Content content; [Space(5)] public GameObject holdedItemObject; public Transform itemObjectHolder; [Space(10)] public List<Item> Inventory; public List<Combinable> Combinables; public int dragIndex = -1; private void Awake() { if(Instance == null) { Instance = this; } if(content == null) { content = GameObject.FindObjectOfType<Content>(); } } public void Add(Item item, int amountToAdd) { //If item already exists in inventory add it to stack. if (Inventory.Contains(item)) { if (item.RoomLeftInStack(amountToAdd)) { item.AddToStack(amountToAdd); } else { Debug.Log("I think I have enough of " + item.data.name); } } //Add new item to list else { Inventory.Add(item); item.AddToStack(amountToAdd); } UpdateUI(); } public void Remove(Item item) { item.RemoveFromStack(1); if (item.stackSize < 1) { Inventory.Remove(item); } UpdateUI(); } public void Combine(Item dragItem, Item mergeItem) { //Item dragItem = Inventory[drag]; // Item mergeItem = Inventory[merge]; foreach (Combinable c in Combinables) { if (c.dragID == dragItem.data.ID && c.mergeID == mergeItem.data.ID || c.dragID == mergeItem.data.ID && c.mergeID == dragItem.data.ID) { if (Inventory.Contains(c.productItem)) { int product = Inventory.IndexOf(c.productItem); if (Inventory[product].RoomLeftInStack(1)) { Inventory[product].AddToStack(1); Remove(mergeItem); Remove(dragItem); } else { Debug.Log("I have enough of " + c.productItem.data.name); } } else { Add(c.productItem, 1); Remove(mergeItem); Remove(dragItem); } } } } public void Event(String s, int index) { Debug.Log(s); /* if (s == "DragBegin") { dragIndex = index; Inventory[index].isDragged = true; } if(s == "DragEnd") { Inventory[index].isDragged = false; } if(s == "Drop") { if(dragIndex != index) { Combine(Inventory[dragIndex], Inventory[index]); } } if(s == "Select") { bool tempBool = Inventory[index].isSelected; foreach(Item i in Inventory) //Clear other items bools { i.isSelected = false; } Inventory[index].isSelected = !tempBool; } */ UpdateUI(); } public void UpdateUI() { if(inventoryUI.activeInHierarchy) content.OnUpdateInventory(); } private void Update() { if (Input.GetKeyDown(KeyCode.I)) { inventoryUI.SetActive(!inventoryUI.activeSelf); UpdateUI(); } } }