/// <summary> /// Returns whether or not it was possible to swap the two item stacks. /// </summary> /// <param name="from">the first clicked slot</param> bool SwapItems(InventorySlot from) { if (from.slotType != slotType && slotType != InventoryType.None) { return(false); } if (IsEmpty) { return(false); } if (from.item.GetName() != item.GetName() || from.items.Count + items.Count > item.GetStackSize()) { ObservableStack <Item> tmpFrom = new ObservableStack <Item>(from.items); from.items.Clear(); from.AddItems(items); items.Clear(); AddItems(tmpFrom); Debug.Log("Item Swap"); return(true); } Debug.Log("Swap failed"); return(false); }
public void MoveItem(GameObject selected) { if (moveFrom == null) { if (!selected.GetComponent <InventorySlot>().IsSlotEmpty()) { moveFrom = selected.GetComponent <InventorySlot>(); moveFrom.GetComponent <Image>().color = Color.grey; hoverItem = Instantiate(hoverItemPrefab); hoverItem.GetComponent <Image>().sprite = selected.GetComponent <Image>().sprite; hoverItem.name = "Hover"; RectTransform hoverPosition = hoverItem.GetComponent <RectTransform>(); RectTransform selectedPosition = selected.GetComponent <RectTransform>(); hoverPosition.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, selectedPosition.sizeDelta.x); hoverPosition.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, selectedPosition.sizeDelta.y); hoverItem.transform.SetParent(GameObject.Find("Canvas").transform, true); hoverItem.transform.localScale = moveFrom.gameObject.transform.localScale; } } else if (moveTo == null) { moveTo = selected.GetComponent <InventorySlot>(); Destroy(GameObject.Find("Hover")); } if (moveTo != null && moveFrom != null) { Stack <ItemSystem> itemsToMove = new Stack <ItemSystem>(moveTo.Items); moveTo.AddItems(moveFrom.Items); if (itemsToMove.Count == 0) { moveFrom.ClearSlot(); } else { moveFrom.AddItems(itemsToMove); } moveFrom.GetComponent <Image>().color = Color.white; moveTo = null; moveFrom = null; hoverItem = null; } }