public void swapWith(MenuSlot releaseTo) { MenuSlotItem meTemp = this.getItem(); MenuSlotItem youTemp = releaseTo.getItem(); bool changedInventory = false; //If the two items can't be combined if (!combineSuccess(releaseTo)) { //Maybe add/remove the items to the home inventories //tip to future William: only use one | so the evaluation doesn't short curcuit changedInventory = changedInventory | maybeAddToInventory(releaseTo); changedInventory = changedInventory | maybeRemoveFromInventory(releaseTo); changedInventory = changedInventory | releaseTo.maybeAddToInventory(this); changedInventory = changedInventory | releaseTo.maybeRemoveFromInventory(this); //Swap them this.setItem(youTemp); releaseTo.setItem(meTemp); } if (changedInventory && releaseTo.type != SlotType.DisplaySlot) { HomeBaseUIController hbuic = GetComponentInParent <HomeBaseUIController>(); hbuic.updateInventoryItemCount(); } }
private bool combineSuccess(MenuSlot releaseTo) { MenuSlotItem meTemp = this.getItem(); MenuSlotItem youTemp = releaseTo.getItem(); //If the two items should be merged if (this.getItem() != null && releaseTo.getItem() != null && meTemp.id == youTemp.id && meTemp.isFlower == youTemp.isFlower) { HomeBaseUIController hbuic = GetComponentInParent <HomeBaseUIController>(); if (releaseTo.type == SlotType.PlayerInventory)//if moving to inventory { hbuic.addHomeInventoryItem(releaseTo.getItem().id, -releaseTo.getItem().count, releaseTo.getItem().isFlower); } else if (this.type == SlotType.PlayerInventory)//if moving out of inventory { hbuic.addHomeInventoryItem(this.getItem().id, this.getItem().count, this.getItem().isFlower); } //Set the other item's count int oldCount = releaseTo.getItem().count; //Destroy the item in the other slot because the grabbed item will be stored GameObject.Destroy(releaseTo.getItem().gameObject); //Set the other slot to the grabbed item releaseTo.setItem(this.getItem()); releaseTo.setCount(oldCount + this.getItem().count); //Clear the item in the grabbed slot this.setItem(null); //It was successful so return true return(true); } return(false); }