using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class EquipSystem : MonoBehaviour { public static EquipSystem Instance { get; set; } // -- UI -- // public GameObject quickSlotsPanel; public List quickSlotsList = new List(); public GameObject numbersHolder; public int selectedNumber = -1; public GameObject selectedItem; public GameObject toolHolder; public GameObject selectedItemModel; private void Awake() { if (Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } } private void Start() { PopulateSlotList(); } void Update() { if (Input.GetKeyDown(KeyCode.Alpha1)) { SelectQuickSlot(1); } else if (Input.GetKeyDown(KeyCode.Alpha2)) { SelectQuickSlot(2); } else if (Input.GetKeyDown(KeyCode.Alpha3)) { SelectQuickSlot(3); } else if (Input.GetKeyDown(KeyCode.Alpha4)) { SelectQuickSlot(4); } else if (Input.GetKeyDown(KeyCode.Alpha5)) { SelectQuickSlot(5); } else if (Input.GetKeyDown(KeyCode.Alpha6)) { SelectQuickSlot(6); } else if (Input.GetKeyDown(KeyCode.Alpha7)) { SelectQuickSlot(7); } } private void SelectQuickSlot(int number) { if (checkIfSlotIsFull(number) == true) { if (selectedNumber != number) { selectedNumber = number; // select item if (selectedItem != null) { selectedItem.GetComponent().isSelected = false; } selectedItem = getSelectedItem(number); selectedItem.GetComponent().isSelected = true; SetEquippedModel(selectedItem); // change color foreach (Transform child in numbersHolder.transform) { child.transform.Find("Text").GetComponent().color = Color.grey; } Text toBeChanged = numbersHolder.transform.Find("number" + number).transform.Find("Text").GetComponent(); toBeChanged.color = Color.white; } else { // unselect selectedNumber = -1; if (selectedItem != null) { selectedItem.GetComponent().isSelected = false; // change color foreach (Transform child in numbersHolder.transform) { child.transform.Find("Text").GetComponent().color = Color.grey; } selectedItem = null; } if (selectedItemModel != null) { DestroyImmediate(selectedItemModel.gameObject); selectedItemModel = null; } } } } private void SetEquippedModel(GameObject selectedItem) { if (selectedItemModel != null) { DestroyImmediate(selectedItemModel.gameObject); } string selectedItemName = selectedItem.name.Replace("(Clone)", ""); selectedItemModel = Instantiate(Resources.Load(selectedItemName + "_Model"), new Vector3(1.07f, 0.38f, 1.54f), Quaternion.Euler(0, 66.7f, 0f) ); selectedItemModel.transform.SetParent(toolHolder.transform, false); } private GameObject getSelectedItem(int number) { return quickSlotsList[number - 1].transform.GetChild(0).gameObject; } private bool checkIfSlotIsFull(int number) { if (quickSlotsList[number - 1].transform.childCount > 0) { return true; } else { return false; } } private void PopulateSlotList() { foreach (Transform child in quickSlotsPanel.transform) { if (child.CompareTag("QuickSlot")) { quickSlotsList.Add(child.gameObject); } } } public void AddToQuickSlots(GameObject itemToEquip) { // Find next free slot GameObject availableSlot = FindNextEmptySlot(); // Set transform of our object itemToEquip.transform.SetParent(availableSlot.transform, false); InventorySystem.Instance.ReCalculateList(); } private GameObject FindNextEmptySlot() { foreach (GameObject slot in quickSlotsList) { if (slot.transform.childCount == 0) { return slot; } } return new GameObject(); } public bool CheckIfFull() { int counter = 0; foreach (GameObject slot in quickSlotsList) { if (slot.transform.childCount > 0) { counter += 1; } } if (counter == 7) { return true; } else { return false; } } }