public void ItemDropper(ItemPickups drop, int dropAmount) { GameObject droppedItem = Instantiate(Resources.Load("ItemDropPrefabs/" + drop.path + "Pickup", typeof(GameObject))) as GameObject; droppedItem.GetComponent <ItemPickup>().newPickup.stackAmount = dropAmount; droppedItem.transform.position = transform.position + transform.forward * 3f; }
ItemPickups CloneAnItemPickup(ItemPickups insertedItem) { ItemPickups clone = new ItemPickups(); clone.storedSprite = insertedItem.storedSprite; clone.path = insertedItem.path; clone.isStackable = insertedItem.isStackable; clone.stackAmount = 1; return(clone); }
public void DropMenuBarItems() { if (index != -1) { ItemPickups dropper = player.GetComponent <PlayerInventoryManager>().itemsStored[index]; if (player.GetComponent <PlayerInventoryManager>().itemsStored[index] != null && player.GetComponent <PlayerInventoryManager>().itemsStored[index].stackAmount > 0) { player.GetComponent <PlayerInventoryManager>().itemsStored[index].stackAmount--; player.GetComponent <PlayerInteractor>().ItemDropper(dropper, 1); if (player.GetComponent <PlayerInventoryManager>().itemsStored[index].stackAmount == 0) { player.GetComponent <PlayerInventoryManager>().itemsStored[index] = null; } } DisplayMenuItems(); } }
public void AddItemToInventory(ItemPickups pickup) { // If the item is stackable run this loop if (pickup.isStackable == IsStackable.Stackable) { int emptySpot = -1; // Store an empty slot incase there is already a full stack, or no stacks at all for (int i = 0; i < itemsStored.Length; i++) { if (emptySpot == -1 && itemsStored[i] == null) { emptySpot = i; } // Ignore empty slots from this point on else if (itemsStored[i] == null) { } // If the item in the slot is the same as the pickup and it's not at a full stack, add another to the stack else if (itemsStored[i].path == pickup.path && itemsStored[i].stackAmount + pickup.stackAmount <= 99) { itemsStored[i].stackAmount += pickup.stackAmount; emptySpot = -1; break; } } if (emptySpot != -1) { itemsStored[emptySpot] = pickup; } } else { // Find the empty slot, and store it for (int i = 0; i < itemsStored.Length; i++) { if (itemsStored[i] == null) { itemsStored[i] = pickup; break; } } } // Tell the inventory to update when you pickup an item canvas.GetComponent <PlayerInventoryUI>().UpdateTheUI(); }
// Use this for initialization void Awake() { stackCount = 1; newPickup = new ItemPickups(); newPickup.storedSprite = inventorySprite; newPickup.path = itemPickupPath; newPickup.isStackable = stackablity; newPickup.stackAmount = stackCount; if (stackablity == IsStackable.Stackable) { newPickup.maxStack = 99; } else { newPickup.maxStack = 1; } }
protected virtual void OnWaitingForPlayers() { ItemIds.Clear(); ItemPickups.Clear(); }
void MoveItemsInInventory() { // If the stack you're holding ever reaches 0, set it to null if (inventoryScript.temporaryItemStored != null && inventoryScript.temporaryItemStored.stackAmount <= 0) { inventoryScript.temporaryItemStored = null; UpdateTheUI(); } // Left click behavior if the inventory is active if (inventory.activeInHierarchy && Input.GetMouseButtonDown(0)) { GameObject selectedInventorySlot; selectedInventorySlot = EventSystem.current.currentSelectedGameObject; // If we don't select a game object and we aren't dragging anything break out of this function if (EventSystem.current.currentSelectedGameObject == null && inventoryScript.temporaryItemStored == null) { return; } // Find the index of the slot that the inventory and store it to access the same slot in the item manager for (int i = 0; i < ItemDisplay.Length; i++) { if (ItemDisplay[i] == selectedInventorySlot) { slotIndex = i; break; } } // If the player left clicks off the inventory, drop all of the items if (selectedInventorySlot == null && inventoryScript.temporaryItemStored != null) { player.GetComponent <PlayerInteractor>().ItemDropper(inventoryScript.temporaryItemStored, inventoryScript.temporaryItemStored.stackAmount); inventoryScript.temporaryItemStored = null; } // If the slot index hasn't changed break out of this function, because they aren't selecting an inventory slot else if (slotIndex == -1) { return; } // Only trigger this if you aren't carrying an item, and the space you're selecting has an item else if (inventoryScript.temporaryItemStored == null && inventoryScript.itemsStored[slotIndex] != null) { print(inventoryScript.itemsStored[slotIndex].stackAmount); // Set your currently dragged item to your mouses dragging item, and put the item into temporary storage inventoryScript.temporaryItemStored = inventoryScript.itemsStored[slotIndex]; inventoryScript.itemsStored[slotIndex] = null; } // This puts items into empty places else if (inventoryScript.temporaryItemStored != null && inventoryScript.itemsStored[slotIndex] == null) { inventoryScript.itemsStored[slotIndex] = inventoryScript.temporaryItemStored; inventoryScript.temporaryItemStored = null; } // If the two items are the same, just add all the items you can to it, until it's at the stack amount else if (inventoryScript.temporaryItemStored != null && inventoryScript.temporaryItemStored.path == inventoryScript.itemsStored[slotIndex].path) { if (inventoryScript.temporaryItemStored.stackAmount + inventoryScript.itemsStored[slotIndex].stackAmount <= 99) { inventoryScript.itemsStored[slotIndex].stackAmount += inventoryScript.temporaryItemStored.stackAmount; inventoryScript.temporaryItemStored.stackAmount = 0; } // Figure how to add the amount that gets the stored stack to 99, but not past that, and sets the temp stack to the right amount if (inventoryScript.temporaryItemStored.stackAmount + inventoryScript.itemsStored[slotIndex].stackAmount > 99) { inventoryScript.temporaryItemStored.stackAmount = 99 - inventoryScript.temporaryItemStored.stackAmount; inventoryScript.itemsStored[slotIndex].stackAmount = 99; } } // This function swaps two items else if (inventoryScript.temporaryItemStored != null && inventoryScript.itemsStored[slotIndex] != null) { // Put the item in temporary storage into swapping storage inventoryScript.swappingStorage = inventoryScript.temporaryItemStored; // Make the temporary item into the item you're swapping with inventoryScript.temporaryItemStored = inventoryScript.itemsStored[slotIndex]; // Make the item in that index the item you were storing prior inventoryScript.itemsStored[slotIndex] = inventoryScript.swappingStorage; // nullify the swapping storage inventoryScript.swappingStorage = null; } // No matter what happens, update the UI UpdateTheUI(); } // How to handle right clicking else if (inventory.activeInHierarchy && Input.GetMouseButtonDown(1)) { // ----------------- RIGHT CLICK HANDLER -------------------------- // GameObject selectedInventorySlot; selectedInventorySlot = EventSystem.current.currentSelectedGameObject; PointerEventData cursor = new PointerEventData(EventSystem.current); cursor.position = Input.mousePosition; List <RaycastResult> objectsHit = new List <RaycastResult>(); EventSystem.current.RaycastAll(cursor, objectsHit); // ----------------- RIGHT CLICK HANDLER -------------------------- // // ------------------------ FINDING THE INVENTORY SLOT -------------- // print(objectsHit.Count); if (objectsHit.Count == 0) { // If there are no objects in the list, that means that the list is empty, and they didn't select any game objects. // In this case objects should be dropped on the ground if (inventoryScript.temporaryItemStored != null) { player.GetComponent <PlayerInteractor>().ItemDropper(inventoryScript.temporaryItemStored, 1); inventoryScript.temporaryItemStored.stackAmount--; UpdateTheUI(); return; } else { return; } } for (int i = 0; i < objectsHit.Count; i++) { //If it's an inventory button set it to the object we want to select and break out of the loop if (objectsHit[i].gameObject.GetComponent <Button>()) { selectedInventorySlot = objectsHit[i].gameObject; break; } } // Find the index of the slot that the inventory and store it to access the same slot in the item manager for (int i = 0; i < ItemDisplay.Length; i++) { if (ItemDisplay[i] == selectedInventorySlot) { slotIndex = i; break; } } // ------------------------FINDING THE INVENTORY SLOT-------------- // // Right click should only have behavior if you're holding something if (inventoryScript.temporaryItemStored != null) { // If the inventory slot is empty, place only one of the objects into that slot if (inventoryScript.itemsStored[slotIndex] == null) { // Remove one from the item you're holding inventoryScript.temporaryItemStored.stackAmount--; inventoryScript.itemsStored[slotIndex] = CloneAnItemPickup(inventoryScript.temporaryItemStored); inventoryScript.itemsStored[slotIndex].stackAmount = 1; } else if (inventoryScript.itemsStored[slotIndex].path == inventoryScript.temporaryItemStored.path) { inventoryScript.itemsStored[slotIndex].stackAmount++; inventoryScript.temporaryItemStored.stackAmount--; } // If there's nothing left in the temp storage set temp storage to null if (inventoryScript.temporaryItemStored.stackAmount <= 0) { inventoryScript.temporaryItemStored = null; } } // If you right click on a slot with an item, while you're not holding an item else if (inventoryScript.temporaryItemStored == null && inventoryScript.itemsStored[slotIndex] != null) { ItemPickups createdItem = CloneAnItemPickup(inventoryScript.itemsStored[slotIndex]); // If it evenly divides by zero, just cut the amount in half if (inventoryScript.itemsStored[slotIndex].stackAmount % 2 == 0) { inventoryScript.itemsStored[slotIndex].stackAmount = inventoryScript.itemsStored[slotIndex].stackAmount / 2; inventoryScript.temporaryItemStored = createdItem; inventoryScript.temporaryItemStored.stackAmount = inventoryScript.itemsStored[slotIndex].stackAmount; } else { inventoryScript.itemsStored[slotIndex].stackAmount = inventoryScript.itemsStored[slotIndex].stackAmount / 2; inventoryScript.temporaryItemStored = createdItem; inventoryScript.temporaryItemStored.stackAmount = inventoryScript.itemsStored[slotIndex].stackAmount; inventoryScript.temporaryItemStored.stackAmount++; } } UpdateTheUI(); } }