Esempio n. 1
0
    void MoveToMouse()
    {
        Item item = inventory.InventoryList [slotID];

        // Taking from slot
        if (inventoryUI.GetItemAtMouse().itemName == "" && item.itemName != "")
        {
            inventoryUI.SetAtMouse(item, slotID, gameObject, GetComponent <InventorySlotData>());
            inventory.RemoveAtIndex(slotID);
            inventoryUI.GetSlotAtMouse().GetComponent <InventorySlotData> ().Empty();

            if (slotChangedEvent != null)
            {
                slotChangedEvent();
            }
        }

        // Putting in slot
        else
        {
            // Slot is empty
            if (inventory.InventoryList [slotID].itemName == "")
            {
                inventory.AddItemToInventory(inventoryUI.GetItemAtMouse(), slotID);
                InventorySlotData slotData = GetComponent <InventorySlotData> ();
                slotData.SetItem(inventory.InventoryList [slotID]);
                slotData.currentAmmo = inventoryUI.GetWeaponAmmoAtMouse();
                slotData.itemHealth  = inventoryUI.GetHealthAtMouse();
                slotData.SetItemHealth();
            }

            // Swap places
            else
            {
                InventorySlotData thisSlot  = GetComponent <InventorySlotData> ();
                InventorySlotData otherSlot = inventoryUI.GetSlotAtMouse().GetComponent <InventorySlotData> ();

                int heldAmmo = inventoryUI.GetWeaponAmmoAtMouse();  // the ammo of the item being moved
                int newAmmo  = thisSlot.currentAmmo;                // the ammo of this slot

                float heldHealth = inventoryUI.GetHealthAtMouse();  // the health of the item being moved
                float newHealth  = thisSlot.itemHealth;             // the health of this slot

                // Swap the slots
                inventory.AddItemToInventory(item, inventoryUI.GetIndexAtMouse());
                inventory.AddItemToInventory(inventoryUI.GetItemAtMouse(), slotID);

                // Replace the values
                thisSlot.currentAmmo  = heldAmmo;
                thisSlot.itemHealth   = heldHealth;
                otherSlot.currentAmmo = newAmmo;
                otherSlot.itemHealth  = newHealth;
            }

            inventoryUI.EmptyAtMouse();
        }
    }