//Adds an item type to the BackPack IEnumerator AddItemType(BackPackItemType t, ObjectBase item) { t.AddItem(item); while (transitInProgress) { yield return(new WaitForEndOfFrame()); } transitInProgress = true; float newTypePos; float addWidth; //If the BackPack is empty, calculate new position from the BackPack button if (items.Count == 0) { newTypePos = button.transform.position.x + (t.GetWidth() / 2) + (spaceBetweenItems); addWidth = t.GetWidth() + (spaceBetweenItems * 5); } //Else, calculate new position from the last item else { newTypePos = items[items.Count - 1].transform.position.x + (items[items.Count - 1].GetWidth() / 2) + spaceBetweenItems + (t.GetWidth() / 2); addWidth = t.GetWidth() + spaceBetweenItems; } Vector3 newPos = new Vector3(newTypePos, button.transform.position.y, t.transform.position.z); //Reset the type t.SetPosition(newPos); t.gameObject.SetActive(true); //Remove the type from the inactive list and add it to the active list inactiveItems.Remove(t); items.Add(t); ModifyBackPackValues(addWidth); yield return(new WaitForEndOfFrame()); //If the newly added item is the only item, open the BackPack if (items.Count == 1) { Open(); yield return(new WaitForSeconds(0.2f)); } //If the item can fit into the BackPack without scrolling, extend the BackPack else if (scrollAmmount == 0) { StartCoroutine(MoveHorizontalBy(strip, -(t.GetWidth() + spaceBetweenItems), 0.2f, false)); yield return(new WaitForSeconds(0.2f)); } transitInProgress = false; }
//Removes an item type from the BackPack IEnumerator RemoveItemType(BackPackItemType t) { while (transitInProgress) { yield return(new WaitForEndOfFrame()); } transitInProgress = true; //Get the index of the item and store the items to its right int index = GetIndex(t); List <BackPackItemType> right = GetItemsAfter(index); //Remove the item type from the items list items.Remove(t); inactiveItems.Add(t); t.gameObject.SetActive(false); //Place the right items to the temp, and move them to their new place MoveToTemp(right); StartCoroutine(MoveHorizontalBy(tempContainer, -(t.GetWidth() + spaceBetweenItems), 0.2f, false)); //Modify BackPack movement variables ModifyBackPackValues(-(t.GetWidth() + spaceBetweenItems)); //StartCoroutine("Test"); yield return(new WaitForSeconds(0.2f)); //Copy back the items from the temp to their original position ResetParents(); //If the items can fit into the whole BackPack if (totalWidth < maxBackPackMovement && items.Count > 0) { //Move the BackPack to the right float movementAmmount = button.transform.position.x - (3 * spaceBetweenItems) - (items[items.Count - 1].GetWidth() / 2); movementAmmount -= items[items.Count - 1].transform.position.x; StartCoroutine(MoveHorizontalBy(strip, movementAmmount, 0.2f, false)); yield return(new WaitForSeconds(0.2f)); } //Finalise item scrolling FinaliseScrolling(); transitInProgress = false; }
//Removes an item type from the BackPack IEnumerator RemoveItemType(BackPackItemType t) { while (transitInProgress) yield return new WaitForEndOfFrame(); transitInProgress = true; //Get the index of the item and store the items to its right int index = GetIndex(t); List<BackPackItemType> right = GetItemsAfter(index); //Remove the item type from the items list items.Remove(t); inactiveItems.Add(t); t.gameObject.SetActive(false); //Place the right items to the temp, and move them to their new place MoveToTemp(right); StartCoroutine(MoveHorizontalBy(tempContainer, -(t.GetWidth() + spaceBetweenItems), 0.2f, false)); //Modify BackPack movement variables ModifyBackPackValues(-(t.GetWidth() + spaceBetweenItems)); //StartCoroutine("Test"); yield return new WaitForSeconds(0.2f); //Copy back the items from the temp to their original position ResetParents(); //If the items can fit into the whole BackPack if (totalWidth < maxBackPackMovement && items.Count > 0) { //Move the BackPack to the right float movementAmmount = button.transform.position.x - (3 * spaceBetweenItems) - (items[items.Count - 1].GetWidth() / 2); movementAmmount -= items[items.Count - 1].transform.position.x; StartCoroutine(MoveHorizontalBy(strip, movementAmmount, 0.2f, false)); yield return new WaitForSeconds(0.2f); } //Finalise item scrolling FinaliseScrolling(); transitInProgress = false; }
//Adds an item type to the BackPack IEnumerator AddItemType(BackPackItemType t, ObjectBase item) { t.AddItem(item); while (transitInProgress) yield return new WaitForEndOfFrame(); transitInProgress = true; float newTypePos; float addWidth; //If the BackPack is empty, calculate new position from the BackPack button if (items.Count == 0) { newTypePos = button.transform.position.x + (t.GetWidth() / 2) + (spaceBetweenItems); addWidth = t.GetWidth() + (spaceBetweenItems * 5); } //Else, calculate new position from the last item else { newTypePos = items[items.Count - 1].transform.position.x + (items[items.Count - 1].GetWidth() / 2) + spaceBetweenItems + (t.GetWidth() / 2); addWidth = t.GetWidth() + spaceBetweenItems; } Vector3 newPos = new Vector3(newTypePos, button.transform.position.y, t.transform.position.z); //Reset the type t.SetPosition(newPos); t.gameObject.SetActive(true); //Remove the type from the inactive list and add it to the active list inactiveItems.Remove(t); items.Add(t); ModifyBackPackValues(addWidth); yield return new WaitForEndOfFrame(); //If the newly added item is the only item, open the BackPack if (items.Count == 1) { Open(); yield return new WaitForSeconds(0.2f); } //If the item can fit into the BackPack without scrolling, extend the BackPack else if (scrollAmmount == 0) { StartCoroutine(MoveHorizontalBy(strip, -(t.GetWidth() + spaceBetweenItems), 0.2f, false)); yield return new WaitForSeconds(0.2f); } transitInProgress = false; }