/// <summary> /// Action for this option in the menu. /// </summary> /// <param name="item">Item.</param> public override void Action(InventoryItemScript item) { //ObjectManagerScript.Instance.CreateItemGameWorld(item); item.Drop(); //Selected item removed from inventory so close callout MenuUIManager.MenuUIInstance.CloseCallout(); }
// Use this for initialization void Start() { inventoryList = new List <InventoryItemScript>(); for (int i = 0; i < itemNames.Count; i++) { //Create a duplicate of the starter item GameObject inventoryItem = (GameObject)Instantiate(startItem); //UI items need to be parented by the canvas or an object within the canvas inventoryItem.transform.SetParent(parentPanel); //Original start item is disabled - so the duplicate must be enabled inventoryItem.SetActive(true); //Get InventoryItemScript component so we can set the data InventoryItemScript iis = inventoryItem.GetComponent <InventoryItemScript>(); iis.itemSprite.sprite = itemSprites[i]; iis.itemNameText.text = itemNames[i]; iis.itemName = itemNames[i]; iis.itemAmountText.text = itemAmounts[i].ToString(); iis.itemAmount = itemAmounts[i]; //Keep a list of the inventory items inventoryList.Add(iis); } DisplayListInOrder(); }
public void SelectionSortInventory() { //iterate through every item int he list except last for (int i = 0; i < inventoryList.Count - 1; i++) { int minIndex = i; //iterate through unsorted potion of list for (int j = i; j < inventoryList.Count; j++) { if (inventoryList[j].itemAmount < inventoryList[minIndex].itemAmount) { minIndex = j; } } //Swap the minimum item into position if (minIndex != i) { InventoryItemScript iis = inventoryList[i]; inventoryList[i] = inventoryList[minIndex]; inventoryList[minIndex] = iis; } } //Display the list in the correct order DisplayListInOrder(); }
// Use this for initialization void Start() { itemAmounts.Add(Grass); itemAmounts.Add(Dirt); itemAmounts.Add(Sand); itemAmounts.Add(Stone); inventoryList = new List <InventoryItemScript>(); for (int i = 0; i < itemNames.Count; i++) { GameObject inventoryItem = (GameObject)Instantiate(startItem); inventoryItem.transform.SetParent(parentPanel); inventoryItem.SetActive(true); InventoryItemScript iis = inventoryItem.GetComponent <InventoryItemScript>(); iis.itemNameText.text = itemNames[i]; iis.itemName = itemNames[i]; iis.itemAmountText.text = itemAmounts[i].ToString(); iis.itemAmount = itemAmounts[i]; inventoryList.Add(iis); DisplayListInOrder(); } }
public void RefreshInventory() { inventoryList = new List <InventoryItemScript>(); inventoryList.Clear(); foreach (GameObject invItem in GameObject.FindGameObjectsWithTag("Item")) //remove items from the scene, or we get duplicates { Destroy(invItem.gameObject); } for (int i = 0; i < itemNames.Count; i++) { // Create a duplicate of the starter item inventoryItem = (GameObject)Instantiate(startItem); // UI items need to parented by the canvas or an objec within the canvas inventoryItem.transform.SetParent(parentPanel); inventoryItem.tag = "Item"; // Original start item is disabled – so the duplicate must be enabled inventoryItem.SetActive(true); // Get InventoryItemScript component so we can set the data InventoryItemScript iis = inventoryItem.GetComponent <InventoryItemScript>(); iis.itemSprite.sprite = itemSprites[i]; iis.itemNameText.text = itemNames[i]; iis.itemName = itemNames[i]; iis.itemAmountText.text = itemAmounts[i].ToString(); iis.itemAmount = itemAmounts[i]; // Keep a list of the inventory items inventoryList.Add(iis); } DisplayListInOrder(); }
public bool DescendingName(InventoryItemScript a, InventoryItemScript b) { if (string.Compare(a.itemName, b.itemName) >= 0) { return(true); } return(false); }
public bool DescendingAmount(InventoryItemScript a, InventoryItemScript b) { if (a.itemAmount >= b.itemAmount) { return(true); } return(false); }
bool CompareName(InventoryItemScript a, InventoryItemScript b) { if (a.itemName.CompareTo(b.itemName) == -1 || a.itemName.CompareTo(b.itemName) == 0) { return(true); } return(false); }
bool CompareValue(InventoryItemScript a, InventoryItemScript b) { if (a.itemAmount <= b.itemAmount) { return(true); } return(false); }
public void PopulateInventory() { Debug.Log("Display Inventory called."); //Variables holding total items displayed so far and items on current page int totalItemsCount = 0, pageItemsCount = 0; RectTransform canvasRect = CanvasScript.CanvasScriptInstance.GetComponent <RectTransform>(); //Rect Y pos is calculated from bottom to top but we want to start from top so inverse float firstYPos = 0.0f; //Col pos variable holds the x position of the current column float colXPos = CanvasScript.CanvasScriptInstance.GetComponent <CanvasScaler>().referenceResolution.x * -1; //Create the initial page of the inventory List <GameObject> InventoryPage0 = new List <GameObject>(); //Add page to list of pages InventoryPages.Add(InventoryPage0); //Loop through each item in player's inventory and add it to a list for a page foreach (InventoryItem i in PlayerStateManager.Instance.PlayerInventory.Items.Values) { //Insantiate object GameObject obj = GameObject.Instantiate(ItemUIObj); //Initialize script to get refrences to display elements InventoryItemScript s = obj.GetComponent <InventoryItemScript>(); s.Init(); /*Set display Info*/ s.SetDisplayElements(i); /*Set index used for managing what item is currently selected*/ s.PageIndex = pageItemsCount; /*Make child of UI container*/ obj.transform.SetParent(this.MenuContainer.transform); //TODO: Add code to position correctly within window RectTransform itemRect = obj.GetComponent <RectTransform>(); int colNum = pageItemsCount / MaxItemsCol; itemRect.anchoredPosition = new Vector2(colXPos + (itemRect.sizeDelta.x * colNum), firstYPos - (itemRect.sizeDelta.y * (pageItemsCount % MaxItemsCol))); //Add newly created item Rect to list for this page InventoryPages[pageItemsCount / MaxItemsPage].Add(obj); //Increment item UI obj count for current page and total pageItemsCount++; totalItemsCount++; //Debug.Log("count/maxItemsCol = " + count / maxItemsCol); /*If the count of items for this current page exceeds page size * then we create list for an additional page */ if (pageItemsCount > MaxItemsPage) { //Create a new page for inventory which should be at index : totalItemsCount/MaxItemsPage InventoryPages.Add(new List <GameObject>()); //Reset page count pageItemsCount = 0; } //TODO: Add code to destroy these on close or otherwise avoid repeats } //All items have been added, so deactivate all pages except for the first }
// Update is called once per frame void Update() { // update blockAmounts array for (int i = 0; i < 4; i++) { inventoryItems[i] = new InventoryItemScript(); inventoryItems[i].itemCount = playerScript.blockCounts[i]; inventoryItems[i].itemName = blockName[i]; inventoryItems[i].itemImage = blockImage[i]; } // if the 0 key is pressed if (Input.GetKeyDown(KeyCode.Alpha0)) { if (invPanelOpen) { invPanelOpen = false; // if the inventory panel is open, close it inventoryPanel.SetActive(false); // freeze the cursor voxChunk.LockCursor(); } else { invPanelOpen = true; // if the inventory panel is closed, open it inventoryPanel.SetActive(true); SortByAmount(); searchBar.text = ""; // unfreeze the cursor voxChunk.UnlockCursor(); } } // if there are no panels open if (!invPanelOpen && !voxChunk.panelOpen) { // allow the player to move fpscript.m_RunSpeed = 10; fpscript.m_WalkSpeed = 5; } // if either panel is open else if (invPanelOpen || voxChunk.panelOpen) { // freeze the player fpscript.m_RunSpeed = 0; fpscript.m_WalkSpeed = 0; } if (invPanelOpen) { // search input search = searchBar.text; SearchByName(search.ToLower()); } }
InventoryItemScript[] Sort(InventoryItemScript[] amounts, SortType sortType) { // if there is only one number, don't try to split into two arrays if (amounts.Length <= 1) { return(amounts); } int arrayLength = amounts.Length / 2; // split into two arrays InventoryItemScript[] firstHalf = new InventoryItemScript[arrayLength]; InventoryItemScript[] secondHalf = new InventoryItemScript[arrayLength]; for (int i = 0; i < amounts.Length; i++) { if (i <= arrayLength - 1) { firstHalf[i] = amounts[i]; } else if (i > arrayLength - 1) { secondHalf[i - arrayLength] = amounts[i]; } } // call recursively firstHalf = Sort(firstHalf, sortType); secondHalf = Sort(secondHalf, sortType); if (!isSortedHighToLow) { // merge the two arrays, according to the sort type amounts = Merge(firstHalf, secondHalf, sortType); } else { // merge the two arrays, according to the sort type amounts = Merge(firstHalf, secondHalf, sortType); } // once the sorting is finished if (amounts.Length == 4) { for (int i = 0; i < amounts.Length; i++) { // reassign text and panel[i].GetComponent <Image>().sprite = amounts[i].itemImage; panel[i].GetComponentInChildren <Text>().text = amounts[i].itemName + ": " + amounts[i].itemCount; } } return(amounts); }
int SortLowToHigh(InventoryItemScript a, InventoryItemScript b) { if (a.itemCount < b.itemCount) { return(-1); } else { return(1); } }
int SortHighToLow(InventoryItemScript a, InventoryItemScript b) { if (a.itemCount > b.itemCount) { return(-1); } else { return(1); } }
public void InsertionSort() { for (int i = 1; i < inventoryList.Count; i++) { InventoryItemScript current = inventoryList[i]; int prec = i - 1; while (prec >= 0 && inventoryList[prec].itemAmount > current.itemAmount) { inventoryList[prec + 1] = inventoryList[prec]; prec -= 1; } inventoryList[prec + 1] = current; } DisplayListInOrder(); }
InventoryItemScript[] Merge(InventoryItemScript[] left, InventoryItemScript[] right, SortType sortType) { // create a new array for the merged items InventoryItemScript[] merged = new InventoryItemScript[left.Length + right.Length]; int i, j, m; i = j = m = 0; // while both i and j are less than the left and right array lengths while (i < left.Length && j < right.Length) { int result = sortType(left[i], right[j]); if (result == -1) { merged[m] = left[i]; i++; m++; } else { merged[m] = right[j]; j++; m++; } } if (i < left.Length) { // add the rest of the elements in a to the end of merged for (int k = i; k < left.Length; k++) { merged[m] = left[k]; m++; } } else { // add the rest of the elements in b to the end of merged for (int k = j; k < right.Length; k++) { merged[m] = right[k]; m++; } } return(merged); }
InventoryItemScript[] MergeHighToLow(InventoryItemScript[] a, InventoryItemScript[] b) { InventoryItemScript[] merged = new InventoryItemScript[a.Length + b.Length]; int i, j, m; i = j = m = 0; while (i < a.Length && j < b.Length) { if (a[i].itemCount >= b[j].itemCount) { merged[m] = a[i]; i++; m++; } else { merged[m] = b[j]; j++; m++; } } if (i < a.Length) { // add the rest of the elements in a to the end of merged for (int k = i; k < a.Length; k++) { merged[m] = a[k]; m++; } } else { // add the rest of the elements in b to the end of merged for (int k = j; k < b.Length; k++) { merged[m] = b[k]; m++; } } return(merged); }
public void BubbleSort() { //List<InventoryItemScript> tempHolder; bool keepGoing = true; while (keepGoing == true) { keepGoing = false; for (int i = 0; i < inventoryList.Count - 1; i++) { if (inventoryList [i + 1].itemAmount < inventoryList [i].itemAmount) { InventoryItemScript iis = inventoryList [i]; inventoryList [i] = inventoryList [i + 1]; inventoryList [i + 1] = iis; keepGoing = true; } } } DisplayListInOrder(); }
public void SortByNameZA() { for (int i = 0; i < inventoryList.Count - 1; i++) { int minIndex = i; for (int j = i; j < inventoryList.Count; j++) { if (string.Compare(inventoryList[j].itemName, inventoryList[minIndex].itemName) > 0) { minIndex = j; } } if (minIndex != i) { InventoryItemScript iis = inventoryList[i]; inventoryList[i] = inventoryList[minIndex]; inventoryList[minIndex] = iis; } } DisplayListInOrder(); }
public void SortInventory() { for (int i = 0; i < inventoryList.Count - 1; i++) { int minIndex = i; for (int j = i; j < inventoryList.Count; j++) { if (inventoryList[j].itemAmount < inventoryList[minIndex].itemAmount) { minIndex = j; } } if (minIndex != i) { InventoryItemScript iis = inventoryList[i]; inventoryList[i] = inventoryList[minIndex]; inventoryList[minIndex] = iis; } } DisplayListInOrder(); }
public void SearchSetSelectedItem(int itemIndex) { //Handle min/max page item check to make sure item asked for doesn't go out of bounds if (itemIndex < 0 || itemIndex > InventoryPages[CurrInventoryPage].Count - 1) { return; } //Set previously selected item as false if (InventoryPages[CurrInventoryPage][SelectedItemIndex] != null) { InventoryPages[CurrInventoryPage][SelectedItemIndex].GetComponent <InventoryItemScript>().SetSelected(false); } InventoryItemScript searchedForItem = null; //Find currently selected item foreach (GameObject obj in InventoryPages[CurrInventoryPage]) { InventoryItemScript thisItemScript = obj.GetComponent <InventoryItemScript>(); /*If the items set index matches the index we are looking for we set it to be selected*/ if (obj.GetComponent <InventoryItemScript>().PageIndex == itemIndex) { this.SelectedItemIndex = itemIndex; thisItemScript.SetSelected(true); //set variable to test if result was found successfully searchedForItem = thisItemScript; } } if (searchedForItem == null) { Debug.LogError("Selected item index not found in menu and inventory!"); } }
public override void Action(InventoryItemScript item) { }
public void CreateItemGameWorld(InventoryItemScript itemToCreate) { }
bool CompareName(InventoryItemScript a, InventoryItemScript b) { if (a.itemName.CompareTo(b.itemName) == -1 || a.itemName.CompareTo(b.itemName) == 0) { return true; } return false;
bool CompareValue(InventoryItemScript a, InventoryItemScript b) { if (a.itemAmount <= b.itemAmount) { return true; } return false;
public virtual void Action(InventoryItemScript item) { //Action method overridden by menu options that inherit from this class }
int SortAtoZ(InventoryItemScript a, InventoryItemScript b) { // compare a to b return(string.Compare(a.itemName, b.itemName)); }
public DudenEntry() { this.obj = null; this.positions = null; }
public DudenEntry(InventoryItemScript obj, List <DudenPosition> positions) { this.obj = obj; this.positions = positions; }
int SortZtoA(InventoryItemScript a, InventoryItemScript b) { // compare b to a return(string.Compare(b.itemName, a.itemName)); }