public virtual bool MoveItemFromContainer(CRItemContainer srcContainer, int src, int dst) { #region ERROR_CHECKING //slot is occupied, swap should be used instead if (IsSlotOccupied(dst)) { Debug.LogWarning(gameObject.name + ":: Failed to move item: Destination slot is occupied, use SwapItemFromContainer(...) instead"); return false; } CRItem item = srcContainer.GetItem(src); if (item == null) { Debug.LogWarning(gameObject.name + ":: Failed to move item: Source slot is empty"); return false; } if (dst >= _size || dst < 0) { Debug.LogWarning(gameObject.name + ":: Failed to move item: Destination index is out of bounds"); return false; } if (ContainsItem(item)) { Debug.LogWarning(gameObject.name + ":: Failed to move item: Destination container already contains that item"); return false; } #endregion if (AddItem(item, dst)) { srcContainer.RemoveItem(src); return true; } else { return false; } }
/*********************** SWAP ITEMS FROM CONTAINER ***********************/ public virtual bool SwapItemFromContainer(CRItemContainer srcContainer, CRItem srcItem, int dst) { #region ERROR CHECKING //slot is occupied, swap should be used instead if (!IsSlotOccupied(dst)) { Debug.LogWarning(gameObject.name + ":: Failed to swap item: Destination slot is empty, use MoveItemFromContainer(...) instead"); return false; } if (srcItem == null) { Debug.LogWarning(gameObject.name + ":: Failed to swap item: Source item is null"); return false; } if (dst >= _size || dst < 0) { Debug.LogWarning(gameObject.name + ":: Failed to swap item: Destination index is out of bounds"); return false; } if (ContainsItem(srcItem)) { Debug.LogWarning(gameObject.name + ":: Failed to swap item: Destination container already contains that item"); return false; } CRItem dstItem = GetItem(dst); if (dstItem == null) { Debug.LogWarning(gameObject.name + ":: Failed to swap item: Destination item is null"); return false; } int src = srcContainer.GetIndex(srcItem); #endregion //try to remove the dst item from its slot if (!RemoveItem(dst)) { return false; } //try to remove the src item from its slot if (!srcContainer.RemoveItem(src)) { //failed //put the dst item back in its correct slot AddItem(dstItem,dst); return false; } //put the item removed from the src container into this container if (!AddItem(srcItem, dst)) { //failed //put the dst item back in its correct slot AddItem(dstItem, dst); //put the src item back in its correct slot srcContainer.AddItem(srcItem, src); return false; } //put the item removed from this container into the src container if (!srcContainer.AddItem(dstItem, src)) { //failed //remove the item added to this container RemoveItem(srcItem); //put the dst item back in its correct slot AddItem(dstItem, dst); //put the src item back in its correct slot srcContainer.AddItem(srcItem, src); return false; } return true; }
/*********************** MOVE ITEMS FROM CONTAINER ***********************/ public virtual bool MoveItemFromContainer(CRItemContainer srcContainer, CRItem item) { #region ERROR_CHECKING int src = srcContainer.GetIndex(item); if (src == -1) { Debug.LogWarning(gameObject.name + ":: Failed to move item: Source item not found"); return false; } if (ContainsItem(item)) { Debug.LogWarning(gameObject.name + ":: Failed to move item: Destination container already contains that item"); return false; } #endregion if (AddItem(item)) { srcContainer.RemoveItem(src); item.container = this; return true; } else { return false; } }