コード例 #1
0
        // equip from item behavior
        public void EquipItem(ItemBehavior itemBehavior, int equipSlot, Item sceneItem)
        {
            // if (equipSlot == -1) {
            //     equipSlot = mainEquipPointIndex;
            // }

            if (equipSlot == -1)
            {
                Debug.LogError("problem with equi slot not set, cant equip " + itemBehavior.itemName);
                return;
            }

            InventorySlot equippedInventorySlot = null;
            int           oldIndex = -1;

            // quick equipping
            if (sceneItem != null)
            {
                if (sceneItem.linkedInventory != null && sceneItem.linkedInventory != this)
                {
                    Debug.LogError("Scene item :: " + sceneItem.name + " is already quick equipped to " + sceneItem.linkedInventory.name + " cant quick equip to " + name);
                    return;
                }
                for (int i = 0; i < equippedSlots.Length; i++)
                {
                    if (equippedSlots[i] != null && equippedSlots[i].sceneItem == sceneItem)
                    {
                        //already equipped scene item here
                        if (i == equipSlot)
                        {
                            return;
                        }

                        // item is already quick equipped at another slot
                        if (equippedSlots[i].isQuickEquipped)
                        {
                            equippedInventorySlot = equippedSlots[i];
                            oldIndex = i;
                        }
                        else
                        {
                            // scene item is equipped as not quick equip, this shouldnt happen
                            Debug.LogError("Scene item :: " + sceneItem.name + " is already equipped to " + name + " normally, cant quick equip");
                            return;
                        }
                    }
                }

                if (equippedInventorySlot == null)
                {
                    equippedInventorySlot = BuildEquippedInventorySlot(sceneItem, true);
                }
                else
                {
                    equippedSlots[oldIndex] = null;
                }

                equipPoints[equipSlot].GetComponent <Interactor>().HoverLock(null);
            }
            else
            {
                //equipping , we need to get an available scene item
                for (int i = 0; i < equippedSlots.Length; i++)
                {
                    if (equippedSlots[i] != null && equippedSlots[i].item == itemBehavior)
                    {
                        //already equipped item here
                        if (i == equipSlot)
                        {
                            return;
                        }

                        // item is already equipped at another slot
                        // unequip it there
                        if (!equippedSlots[i].isQuickEquipped)
                        {
                            equippedInventorySlot = equippedSlots[i];
                            oldIndex = i;
                        }
                    }
                }

                if (equippedInventorySlot == null)
                {
                    equippedInventorySlot = BuildEquippedInventorySlot(Item.GetSceneItem(itemBehavior), false);
                }
                else
                {
                    equippedSlots[oldIndex] = null;
                }
            }


            //unequip our current equip slot
            if (equippedSlots[equipSlot] != null)
            {
                UnequipItem(equipSlot, equippedSlots[equipSlot].isQuickEquipped);
            }

            equippedSlots[equipSlot] = equippedInventorySlot;
            equippedSlots[equipSlot].sceneItem.linkedInventory = this;
            equippedSlots[equipSlot].sceneItem.myEquipPoint    = equipPoints[equipSlot];

            if (equippedSlots[equipSlot].item.equipType != EquipType.Static)
            {
                SnapItemToPosition(equipSlot);
            }

            equippedSlots[equipSlot].sceneItem.OnEquipped(this);

            if (onEquip != null)
            {
                onEquip(this, equippedSlots[equipSlot].sceneItem, equipSlot, equippedSlots[equipSlot].isQuickEquipped);
            }
        }
コード例 #2
0
        public void DropItem(ItemBehavior itemBehavior, int count, bool getScene, int inventoryIndex)
        {
            if (itemBehavior.permanentStash)
            {
                return;// false;
            }

            if (inventoryIndex < 0)
            {
                //check if it's already in inventory
                for (int i = 0; i < allInventory.Count; i++)
                {
                    if (allInventory[i].item == itemBehavior)
                    {
                        inventoryIndex = i;
                        // slotInInventory = allInventory[i];
                        break;
                    }
                }
            }



            InventorySlot slotInInventory = null;

            if (inventoryIndex >= 0)
            {
                slotInInventory = allInventory[inventoryIndex];
            }

            bool wasInInventory = slotInInventory != null;

            if (wasInInventory)
            {
                bool hasModel = false;
                if (ItemIsEquipped(-1, itemBehavior))
                {
                    UnequipItem(itemBehavior, getScene);
                    hasModel = true;
                }

                count = Mathf.Min(count, slotInInventory.count);

                slotInInventory.count -= count;
                if (slotInInventory.count <= 0)
                {
                    slotInInventory.count = 0;
                }

                // remove buffs
                if (itemBehavior.stashedItemBehavior != null)
                {
                    itemBehavior.stashedItemBehavior.OnItemDropped(this, itemBehavior, count);
                }


                if (slotInInventory.count == 0)
                {
                    allInventory.Remove(slotInInventory);
                }

                if (onDrop != null)
                {
                    onDrop(this, itemBehavior, count);
                }


                if (!hasModel)
                {
                    if (getScene)
                    {
                        Item sceneItem = Item.GetSceneItem(itemBehavior);
                        sceneItem.transform.position = transform.TransformPoint(dropLocalPoint);
                        sceneItem.transform.rotation = Quaternion.Euler(UnityEngine.Random.Range(0, 360), UnityEngine.Random.Range(0, 360), UnityEngine.Random.Range(0, 360));
                        sceneItem.gameObject.SetActive(true);
                    }
                }

                // return true;
            }
            // return false;
        }