public void OnItemConsumed(Inventory inventory, ItemBehavior item, int count, int equipSlot)
 {
     InitializeListeners();
     for (int i = 0; i < listeners.Count; i++)
     {
         listeners[i].OnItemConsumed(inventory, item, count, equipSlot);
     }
 }
 public void OnItemStashed(Inventory inventory, ItemBehavior item, int count)
 {
     InitializeListeners();
     for (int i = 0; i < listeners.Count; i++)
     {
         listeners[i].OnItemStashed(inventory, item, count);
     }
 }
 public bool CanStashItem(ItemBehavior item)
 {
     if (item.allowMultipleStashed)
     {
         return(true);
     }
     //check if it's already in inventory
     return(!ItemIsInInventory(item, out _, out _));
 }
Exemple #4
0
 public void OnItemConsumed(Inventory inventory, ItemBehavior item, int count, int equipSlot)
 {
     Debug.LogError("Equipping on consume");
     if (inventory.ItemIsEquipped(equipSlot, item))
     {
         inventory.UnequipItem(equipSlot, false);
     }
     else
     {
         inventory.EquipItem(item, equipSlot, null);
     }
 }
        public void StashItem(Item item)
        {
            ItemBehavior itemBehavior = item.itemBehavior;

            if (CanStashItem(itemBehavior))
            {
                StashItem(itemBehavior, itemBehavior.stackable ? item.itemCount : 1);

                //disable the scene item (frees it up for pool)
                item.gameObject.SetActive(false);
            }
        }
 public void UnequipItem(ItemBehavior item, bool showScene)
 {
     for (int i = 0; i < equippedSlots.Length; i++)
     {
         if (equippedSlots[i] != null)
         {
             if (equippedSlots[i].item == item)
             {
                 UnequipItem(i, showScene);
                 return;
             }
         }
     }
 }
        void SnapItemToPosition(int equipSlot)
        {
            Transform    itemTransform = equippedSlots[equipSlot].sceneItem.transform;
            ItemBehavior item          = equippedSlots[equipSlot].item;

            Transform oldParent = itemTransform.parent;

            TransformBehavior.AdjustTransform(itemTransform, equipPoints[equipSlot].transform, item.equipTransform, equipSlot);

            if (item.equipType != EquipType.Normal)
            {
                itemTransform.parent = oldParent;
            }
        }
        public bool TransferItemTo(ItemBehavior item, int count, Inventory otherInventory)
        {
            int itemIndex;

            if (CanDropItem(item, out itemIndex, out _, false))
            {
                if (otherInventory.CanStashItem(item))
                {
                    DropItem(item, count, false, itemIndex);
                    otherInventory.StashItem(item, count);
                    return(true);
                }
            }
            return(false);
        }
 bool ItemIsInInventory(ItemBehavior item, out int atIndex, out InventorySlot slot)
 {
     slot    = null;
     atIndex = -1;
     for (int i = 0; i < allInventory.Count; i++)
     {
         if (allInventory[i].item == item)
         {
             atIndex = i;
             slot    = allInventory[i];
             return(true);
         }
     }
     return(false);
 }
        public void StashItem(ItemBehavior itemBehavior, int count)
        {
            //check if it's already in inventory
            InventorySlot slotInInventory = null;

            if (itemBehavior.keepOnStash)
            {
                for (int i = 0; i < allInventory.Count; i++)
                {
                    if (allInventory[i].item == itemBehavior)
                    {
                        slotInInventory = allInventory[i];
                        break;
                    }
                }
            }
            bool wasInInventory = slotInInventory != null;

            if (wasInInventory)
            {
                if (!itemBehavior.allowMultipleStashed)
                {
                    return;// false;
                }
                slotInInventory.count += count;
            }
            else
            {
                slotInInventory = new InventorySlot(itemBehavior, count);

                if (itemBehavior.keepOnStash)
                {
                    allInventory.Add(slotInInventory);
                }
            }

            if (itemBehavior.stashedItemBehavior != null)
            {
                itemBehavior.stashedItemBehavior.OnItemStashed(this, itemBehavior, count);
            }

            if (onStash != null)
            {
                onStash(this, itemBehavior, count);
            }
        }
        public bool CanDropItem(ItemBehavior item, out int atIndex, out InventorySlot slot, bool checkInventory)
        {
            slot    = null;
            atIndex = -1;
            if (item.permanentStash)
            {
                return(false);
            }

            //check if it's already in inventory
            if (checkInventory)
            {
                return(ItemIsInInventory(item, out atIndex, out slot));
            }

            return(true);
        }
 public bool ItemIsEquipped(int slotIndex, ItemBehavior item)
 {
     if (slotIndex < 0)
     {
         for (int i = 0; i < equippedSlots.Length; i++)
         {
             if (equippedSlots[i] != null && equippedSlots[i].item == item)
             {
                 return(true);
             }
         }
         return(false);
     }
     else
     {
         return(equippedSlots[slotIndex] != null && equippedSlots[slotIndex].item == item);
     }
 }
Exemple #13
0
        public static Item GetSceneItem(ItemBehavior itemBehavior)  // prefab) {// Inventory quickEquipInventory){// bool dropOnUseEnd) {
        {
            Item prefab = itemBehavior.scenePrefabVariations[Random.Range(0, itemBehavior.scenePrefabVariations.Length)];

            int instanceID = prefab.GetInstanceID();

            Item sceneItem = null;
            bool hasKey    = false;

            if (itemPoolsPerPrefab.ContainsKey(instanceID))
            {
                hasKey = true;

                foreach (var it in itemPoolsPerPrefab[instanceID])
                {
                    if (!it.gameObject.activeInHierarchy)
                    {
                        sceneItem = it;
                        break;
                    }
                }
            }

            if (sceneItem == null)
            {
                sceneItem = GameObject.Instantiate(prefab);

                if (hasKey)
                {
                    itemPoolsPerPrefab[instanceID].Add(sceneItem);
                }
                else
                {
                    itemPoolsPerPrefab.Add(instanceID, new HashSet <Item>()
                    {
                        sceneItem
                    });
                }
            }
            return(sceneItem);
        }
        public bool TransferInventoryContentsTo(Inventory otherInventory)
        {
            bool didAnyTransfer = false;

            for (int i = allInventory.Count - 1; i >= 0; i--)
            {
                ItemBehavior item  = allInventory[i].item;
                int          count = allInventory[i].count;

                if (CanDropItem(item, out _, out _, false))
                {
                    if (otherInventory.CanStashItem(item))
                    {
                        DropItem(item, count, false, i);
                        otherInventory.StashItem(item, count);
                        didAnyTransfer = true;
                    }
                }
            }
            return(didAnyTransfer);
        }
        void FixedUpdate()
        {
            if (equipID < 0 || equipID >= baseInventory.equippedSlots.Length)
            {
                Debug.LogError("Equip slot " + equipID + " is out of range on inventory " + baseInventory);
                return;
            }

            Inventory.InventorySlot slot = baseInventory.equippedSlots[equipID];
            if (slot != null)
            {
                ItemBehavior item = slot.item;

                if (item.equipType == Inventory.EquipType.Physics)
                {
                    UpdateAttachedVelocity(slot);
                }
                else if (item.equipType == Inventory.EquipType.Normal)
                {
                    TransformBehavior.AdjustTransform(slot.sceneItem.transform, transform, item.equipTransform, equipID);
                }
            }
        }
        // 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);
            }
        }
 public InventorySlot(ItemBehavior item, int count)
 {
     this.item  = item;
     this.count = count;
 }
Exemple #18
0
 public void OnItemStashed(Inventory inventory, ItemBehavior item, int count)
 {
 }
        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;
        }
Exemple #20
0
 public void OnItemDropped(Inventory inventory, ItemBehavior item, int count)
 {
 }