public void OnPointerClick(PointerEventData eventData) { if (movableItemDrag.dragItem == true) { if (eventData.button == PointerEventData.InputButton.Left) { inventoryManager.DropItem(movableItemDrag.movableItemObject, movableItemDrag.amount); movableItemDrag.DragItem(false); } else if (eventData.button == PointerEventData.InputButton.Right) { if (movableItemDrag.movableItemObject.stackable) { if (movableItemDrag.amount == 1) { inventoryManager.DropItem(movableItemDrag.movableItemObject, movableItemDrag.amount); movableItemDrag.DragItem(false); } else { inventoryManager.DropItem(movableItemDrag.movableItemObject, 1); movableItemDrag.DragItem(true, movableItemDrag.movableItemObject, movableItemDrag.amount - 1); } } else { inventoryManager.DropItem(movableItemDrag.movableItemObject, movableItemDrag.amount); movableItemDrag.DragItem(false); } } } }
public bool AddingItem(ItemObject item, int amount = 1, int index = -2, bool draggedItem = false) { //SpecifiedSlot if (index != -2) { //Occupied if (items[index] != null) { if (item.stackable) { //Same item if (items[index] == item) { //Can pick up all items into the slot Debug.Log("spec slot, stack, same item, can pick up all"); if (amountData[index] + amount <= item.maxAmount) { amountData[index] += amount; if (inventoryChanged != null) { inventoryChanged.Invoke(index, item, amountData[index]); } return(true); } else { //Items were dragged if (draggedItem) { int toMouse = amountData[index] + amount - item.maxAmount; amountData[index] = item.maxAmount; if (inventoryChanged != null) { inventoryChanged.Invoke(index, item, item.maxAmount); } Debug.Log("Not enough space in slot, items dragged, " + item.name + toMouse + "to mouse"); movableItemDrag.DragItem(true, item, toMouse); return(false); } else { int toNext = amountData[index] + amount - item.maxAmount; amountData[index] = item.maxAmount; if (inventoryChanged != null) { inventoryChanged.Invoke(index, item, item.maxAmount); } Debug.Log("Not enough space in slot, items NOT dragged, " + item.name + toNext + "to nextSlot"); AddingItem(item, toNext); return(true); } } //Not the same item } else { if (draggedItem) { Debug.Log("switch items around with mouse"); //switch items ItemObject temporaryItem = items[index]; int temporaryAmount = amountData[index]; Inventory.instance.RemoveItem(index); Inventory.instance.AddingItem(movableItemDrag.movableItemObject, movableItemDrag.amount, index, true); movableItemDrag.DragItem(true, temporaryItem, temporaryAmount); return(true); } else { Debug.LogError("Inventory: something tried to put an item " + item.name + " " + amount + " into slot " + index + ", which was occupied by " + items[index] + " " + amountData[index]); return(false); } } //Not stackable item } else { if (draggedItem) { Debug.Log("Dragged item to slot, inSlot item to mouse"); ItemObject temporaryItem = items[index]; int temporaryAmount = 1; Inventory.instance.RemoveItem(index); Inventory.instance.AddingItem(movableItemDrag.movableItemObject, movableItemDrag.amount, index, true); movableItemDrag.DragItem(true, temporaryItem, temporaryAmount); return(true); } else { Debug.LogError("Inventory: something tried to put an item " + item.name + " " + amount + " into slot " + index + ", which was occupied by " + items[index] + " " + amountData[index]); return(false); } } //Not occupied } else { Debug.Log("spec slot, not occupied"); items[index] = item; amountData[index] = amount; if (inventoryChanged != null) { inventoryChanged.Invoke(index, item, amount); } return(true); } //no specified slot } else { if (item.stackable) { int availableSlot = FindSlot(item); if (availableSlot == -123) { Debug.Log("Inventory full"); return(false); } else { Debug.Log("no spec slot, stack"); AddingItem(item, amount, availableSlot); return(true); } } else { int availableSlot = NextFreeSlot(); if (availableSlot == -123) { Debug.Log("Inventory full"); return(false); } else { Debug.Log("no spec slot, nostack"); AddingItem(item, amount, availableSlot); return(true); } } } }
public void OnPointerClick(PointerEventData eventData) { if (eventData.button == PointerEventData.InputButton.Right) { switch (Inventory.instance.GetInventory()[itemSlotScript.index] != null) { case true when !movableItemDragScript.dragItem: //1 amount to mouse if (Inventory.instance.GetAmountData()[itemSlotScript.index] == 1) { movableItemDragScript.DragItem(true, itemSlotScript.myItemObject, 1); Inventory.instance.RemoveItem(itemSlotScript.index); } else { Inventory.instance.RemoveItem(itemSlotScript.index, 1); movableItemDragScript.DragItem(true, itemSlotScript.myItemObject, movableItemDragScript.amount + 1); } break; case false when movableItemDragScript.dragItem: //1 amount to slot if (movableItemDragScript.amount == 1) { Inventory.instance.AddingItem(movableItemDragScript.movableItemObject, 1, itemSlotScript.index, true); movableItemDragScript.DragItem(false); } else { movableItemDragScript.DragItem(true, movableItemDragScript.movableItemObject, movableItemDragScript.amount - 1); Inventory.instance.AddingItem(movableItemDragScript.movableItemObject, 1, itemSlotScript.index, true); } break; case true when movableItemDragScript.dragItem: //switch items Debug.Log("create a stacking situation for this"); ItemObject temporaryItem = itemSlotScript.myItemObject; int temporaryAmount = itemSlotScript.myAmount; Inventory.instance.RemoveItem(itemSlotScript.index); Inventory.instance.AddingItem(movableItemDragScript.movableItemObject, movableItemDragScript.amount, itemSlotScript.index, true); movableItemDragScript.DragItem(true, temporaryItem, temporaryAmount); break; } } if (eventData.button == PointerEventData.InputButton.Left) { switch (Inventory.instance.GetInventory()[itemSlotScript.index] != null) { case true when !movableItemDragScript.dragItem: //to mouse movableItemDragScript.DragItem(true, itemSlotScript.myItemObject, itemSlotScript.myAmount); Inventory.instance.RemoveItem(itemSlotScript.index); break; case false when movableItemDragScript.dragItem: //add dragged item to inv Inventory.instance.AddingItem(movableItemDragScript.movableItemObject, movableItemDragScript.amount, itemSlotScript.index, true); movableItemDragScript.DragItem(false); break; case true when movableItemDragScript.dragItem: //switch items ItemObject movItem = movableItemDragScript.movableItemObject; ItemObject invItem = Inventory.instance.GetInventory()[itemSlotScript.index]; //if they are different items if (movItem != invItem) { ItemObject temporaryItem = itemSlotScript.myItemObject; int temporaryAmount = itemSlotScript.myAmount; Inventory.instance.RemoveItem(itemSlotScript.index); Inventory.instance.AddingItem(movableItemDragScript.movableItemObject, movableItemDragScript.amount, itemSlotScript.index, true); movableItemDragScript.DragItem(true, temporaryItem, temporaryAmount); //same stackable item } else if (movItem.stackable) { if (Inventory.instance.AddingItem(movItem, movableItemDragScript.amount, itemSlotScript.index, true)) { movableItemDragScript.DragItem(false); } else { ItemObject temporaryItem = itemSlotScript.myItemObject; int temporaryAmount = itemSlotScript.myAmount; Inventory.instance.RemoveItem(itemSlotScript.index); Inventory.instance.AddingItem(movableItemDragScript.movableItemObject, movableItemDragScript.amount, itemSlotScript.index, true); movableItemDragScript.DragItem(true, temporaryItem, temporaryAmount); } } break; } } }