Пример #1
0
        /// <summary>
        /// Picks up the item.
        /// </summary>
        /// <param name="character">The character that should pick up the item.</param>
        /// <param name="inventory">The inventory belonging to the character.</param>
        /// <param name="slotID">The slot ID that picked up the item. A -1 value will indicate no specified slot.</param>
        /// <param name="immediatePickup">Should the item be picked up immediately?</param>
        /// <param name="pickupItemIdentifier">Should the ItemIdentifier be picked up? This should be false if the ItemIdentifier will later be picked up.</param>
        public void DoItemPickup(GameObject character, InventoryBase inventory, int slotID, bool immediatePickup, bool pickupItemIdentifier)
        {
            // Add any items to the character.
            if (m_ItemPickupSet != null && m_ItemPickupSet.Length > 0)
            {
                // Spawn the item under the character's ItemPlacement GameObject.
                var itemPlacement = character.GetComponentInChildren <ItemPlacement>(true);
                if (itemPlacement == null)
                {
                    Debug.LogError($"Error: ItemPlacement doesn't exist under the character {character.name}.");
                    return;
                }
                for (int i = 0; i < m_ItemPickupSet.Length; ++i)
                {
                    // If the Item is null then only the ItemSet should be added.
                    if (m_ItemPickupSet[i].Item == null)
                    {
                        var itemSetManager = character.GetCachedComponent <ItemSetManagerBase>();
                        if (itemSetManager == null)
                        {
                            continue;
                        }

                        IItemCategoryIdentifier category = null;
                        var addItemSetParents            = true;
                        // If no item is specified then the category should be retrieved from the Item Definition.
                        if (m_ItemPickupSet[i].CategoryID == 0)
                        {
                            for (int j = 0; j < m_ItemPickupSet[i].ItemSet.Slots.Length; ++j)
                            {
                                var itemDefinition = m_ItemPickupSet[i].ItemSet.Slots[j];
                                if (itemDefinition != null)
                                {
                                    category = itemDefinition.GetItemCategory();
                                }
                            }
                        }
                        else
                        {
                            // A specific category was specified.
                            if (itemSetManager.CategoryItemSets != null)
                            {
                                for (int j = 0; j < itemSetManager.CategoryItemSets.Length; ++j)
                                {
                                    if (itemSetManager.CategoryItemSets[j].CategoryID == m_ItemPickupSet[i].CategoryID)
                                    {
                                        category          = itemSetManager.CategoryItemSets[j].ItemCategory;
                                        addItemSetParents = false;
                                        break;
                                    }
                                }
                            }
                        }

                        if (category != null)
                        {
                            itemSetManager.AddItemSet(m_ItemPickupSet[i].ItemSet, m_ItemPickupSet[i].Default, category, addItemSetParents);
                        }

                        continue;
                    }

                    if (slotID != -1 && (slotID >= m_ItemPickupSet[i].ItemSet.Slots.Length || m_ItemPickupSet[i].ItemSet.Slots[slotID] == null))
                    {
                        continue;
                    }

                    var item = m_ItemPickupSet[i].Item.GetCachedComponent <Item>();
                    if (inventory.HasItem(item))
                    {
                        continue;
                    }

                    // Instantiate the item that will be added to the character.
                    item = Item.SpawnItem(character, item);

                    // Add the ItemSet before the item so the item can use the added ItemSet.
                    if (m_ItemPickupSet[i].ItemSet != null)
                    {
                        var itemSetManager = character.GetCachedComponent <ItemSetManager>();
                        if (itemSetManager != null)
                        {
                            m_ItemPickupSet[i].ItemSet.ItemIdentifiers = new Shared.Inventory.IItemIdentifier[m_ItemPickupSet[i].ItemSet.Slots.Length];
                            if (item.SlotID >= m_ItemPickupSet[i].ItemSet.ItemIdentifiers.Length)
                            {
                                Debug.LogError($"Error: Unable to assign the ItemIdentifier at slot {item.SlotID}. The Slot Count should be increased on the item pickup.");
                                continue;
                            }
                            m_ItemPickupSet[i].ItemSet.ItemIdentifiers[item.SlotID] = item.ItemIdentifier;
                            itemSetManager.AddItemSet(item, m_ItemPickupSet[i].ItemSet, m_ItemPickupSet[i].Default);
                        }
                    }

                    // All of the setup is complete - add the item to the inventory.
                    inventory.AddItem(item, false, false);
                }
            }

            m_PickedUp = m_AlwaysPickup;
            if (pickupItemIdentifier)
            {
                // Even if the ItemIdentifier doesn't have space it may be equipped by the inventory. The object should be considered as picked up in this situation.
                EventHandler.RegisterEvent <Item, int>(character, "OnAbilityWillEquipItem", OnWillEquipItem);
                if (DoItemIdentifierPickup(character, inventory, slotID, immediatePickup, true))
                {
                    m_PickedUp = true;
                }
                EventHandler.UnregisterEvent <Item, int>(character, "OnAbilityWillEquipItem", OnWillEquipItem);
            }
            else
            {
                // If pickup ItemIdentifier is false then the PickupItem ability will pick up the ItemIdentifier.
                m_PickedUp = true;
            }

            if (m_PickedUp)
            {
                ObjectPickedUp(character);
            }
        }
        /// <summary>
        /// Picks up the item.
        /// </summary>
        /// <param name="character">The character that should pick up the item.</param>
        /// <param name="inventory">The inventory belonging to the character.</param>
        /// <param name="slotID">The slot ID that picked up the item. A -1 value will indicate no specified slot.</param>
        /// <param name="immediatePickup">Should the item be picked up immediately?</param>
        /// <param name="pickupItemType">Should the ItemType be picked up? This should be false if the ItemType will later be picked up.</param>
        public void DoItemPickup(GameObject character, InventoryBase inventory, int slotID, bool immediatePickup, bool pickupItemType)
        {
            // Add any items to the character.
            if (m_ItemPickupSet != null && m_ItemPickupSet.Length > 0)
            {
                // Spawn the item under the character's ItemPlacement GameObject.
                var itemPlacement = character.GetComponentInChildren <ItemPlacement>(true);
                if (itemPlacement == null)
                {
                    Debug.LogError("Error: ItemPlacement doesn't exist under the character " + character.name);
                    return;
                }
                for (int i = 0; i < m_ItemPickupSet.Length; ++i)
                {
                    // The Item must exist.
                    if (m_ItemPickupSet[i].Item == null || (slotID != -1 && (slotID >= m_ItemPickupSet[i].ItemSet.Slots.Length || m_ItemPickupSet[i].ItemSet.Slots[slotID] == null)))
                    {
                        continue;
                    }

                    var item = m_ItemPickupSet[i].Item.GetCachedComponent <Item>();

                    // Add the ItemSet before the item so the item can use the added ItemSet.
                    if (m_ItemPickupSet[i].ItemSet != null)
                    {
                        var itemSetManager = character.GetCachedComponent <ItemSetManager>();
                        if (itemSetManager != null)
                        {
                            itemSetManager.AddItemSet(item, m_ItemPickupSet[i].ItemSet, m_ItemPickupSet[i].Default);
                        }
                    }

                    // Instantiate and add the item to the character.
                    if (!inventory.HasItem(item))
                    {
                        var itemGameObject = ObjectPool.Instantiate(m_ItemPickupSet[i].Item, Vector3.zero, Quaternion.identity, itemPlacement.transform);
                        itemGameObject.name = m_ItemPickupSet[i].Item.name;
                        itemGameObject.transform.localPosition = Vector3.zero;
                        itemGameObject.transform.localRotation = Quaternion.identity;
                        item = itemGameObject.GetComponent <Item>();
                        inventory.AddItem(item, false);
                    }
                }
            }

            m_PickedUp = m_AlwaysPickup;
            if (pickupItemType)
            {
                // Even if the ItemType doesn't have space it may be equipped by the inventory. The object should be considered as picked up in this situation.
                EventHandler.RegisterEvent <Item, int>(character, "OnAbilityWillEquipItem", OnWillEquipItem);
                if (DoItemTypePickup(character, inventory, slotID, immediatePickup, true))
                {
                    m_PickedUp = true;
                }
                EventHandler.UnregisterEvent <Item, int>(character, "OnAbilityWillEquipItem", OnWillEquipItem);
            }
            else
            {
                // If pickup ItemType is false then the PickupItem ability will pick up the ItemType.
                m_PickedUp = true;
            }

            if (m_PickedUp)
            {
                ObjectPickedUp(character);
            }
        }