Пример #1
0
        /// <summary>
        /// Initializes the categories which belong to the ItemCollection.
        /// </summary>
        /// <param name="itemCollection">The ItemCollection that contain the categories.</param>
        public void Initialize(ItemCollection itemCollection)
        {
            if (CategoryIDs == null || CategoryIDs.Length == 0)
            {
                return;
            }

            if (CategoryIDs.Length > 1)
            {
                // Multiple categories are specified. Create a new runtime category that has multiple parents.
                var categoryParents = new Category[CategoryIDs.Length];
                for (int i = 0; i < CategoryIDs.Length; ++i)
                {
                    categoryParents[i] = itemCollection.GetCategory(CategoryIDs[i]);
                }
                var category = ScriptableObject.CreateInstance <Category>();
                category.hideFlags = HideFlags.HideAndDontSave;
                category.Parents   = categoryParents;
                m_Category         = category;
            }
            else
            {
                m_Category = itemCollection.GetCategory(CategoryIDs[0]);
            }
        }
Пример #2
0
 /// <summary>
 /// CategoryItemSet constructor with a single parameter.
 /// </summary>
 /// <param name="categoryID">The ID of the category.</param>
 /// <param name="categoryName">The name of the category.</param>
 /// <param name="itemCategory">The category that the ItemSet belongs to.</param>
 public CategoryItemSet(uint categoryID, string categoryName, IItemCategoryIdentifier itemCategory)
 {
     CategoryID     = categoryID;
     m_CategoryName = categoryName;
     m_ItemCategory = itemCategory;
     m_ItemSetList  = new List <ItemSet>();
 }
Пример #3
0
        /// <summary>
        /// Initializes the ItemSet categories.
        /// </summary>
        /// <returns>True if the categories were initialized.</returns>
        protected override bool InitializeCategories()
        {
            var itemSetManager = m_ItemSetManager as ItemSetManager;
            var itemCollection = EditorGUILayout.ObjectField("Item Collection", itemSetManager.ItemCollection, typeof(ItemCollection), false) as ItemCollection;

            if (itemCollection == null)
            {
                EditorGUILayout.HelpBox("An ItemCollection reference is required.", MessageType.Error);
                return(false);
            }
            else if (itemSetManager.ItemCollection != itemCollection)
            {
                itemSetManager.ItemCollection = itemCollection;
                m_ItemSetReorderableList      = null;
            }
            m_ItemCollection = itemCollection;

            var categoryIdentifiers = new IItemCategoryIdentifier[m_ItemCollection.Categories.Length];

            for (int i = 0; i < categoryIdentifiers.Length; ++i)
            {
                categoryIdentifiers[i] = m_ItemCollection.Categories[i];
            }
            CheckCategories(categoryIdentifiers);
            CheckItemSetAbilities(categoryIdentifiers);
            return(true);
        }
Пример #4
0
        /// <summary>
        /// Returns true if the CategoryIdentifier belongs to the category with the specified index.
        /// </summary>
        /// <param name="itemCategory">The CategoryIdentifier to determine if it belongs to the category.</param>
        /// <param name="categoryIndex">The index of the category which the ItemIdentifier may belong to.</param>
        /// <returns>True if the ItemIdentifier belongs to the category with the specified index.</returns>
        private bool IsCategoryMember(IItemCategoryIdentifier itemCategory, int categoryIndex)
        {
            if (categoryIndex >= m_CategoryItemSets.Length || categoryIndex < 0)
            {
                return(false);
            }

            if (itemCategory == m_CategoryItemSets[categoryIndex].ItemCategory)
            {
                return(true);
            }

            // Recursively search the parents.
            var categoryParents = itemCategory.GetDirectParents();

            if (categoryParents == null)
            {
                return(false);
            }
            for (int i = 0; i < categoryParents.Count; ++i)
            {
                if (IsCategoryMember(categoryParents[i], categoryIndex))
                {
                    return(true);
                }
            }

            return(false);
        }
        /// <summary>
        /// Adds the ItemSet if it doesn't already exist.
        /// </summary>
        /// <param name="itemSet">The ItemSet to add.</param>
        /// <param name="defaultItemSet">Is the ItemSet the default ItemSet within the category?</param>
        /// <param name="category">The category that the ItemSet is trying to be added to.</param>
        /// <param name="addParents">Should ItemSets be added to the category parents?</param>
        public void AddItemSet(ItemSet itemSet, bool defaultItemSet, IItemCategoryIdentifier category, bool addParents)
        {
            // The ItemSet must have at least one slot filled.
            var validItemSet = false;

            for (int i = 0; i < itemSet.Slots.Length; ++i)
            {
                if (itemSet.Slots[i] != null)
                {
                    validItemSet = true;
                    break;
                }
            }
            if (!validItemSet)
            {
                return;
            }

            // The category can have multiple parents. Keep trying to add the ItemSet to all of the possible categories.
            if (addParents)
            {
                var categoryParents = category.GetDirectParents();
                for (int i = 0; i < categoryParents.Count; ++i)
                {
                    AddItemSet(itemSet, defaultItemSet, categoryParents[i], addParents);
                }
            }

            // The category may not have been added to the ItemSetManager.
            if (!m_CategoryIndexMap.TryGetValue(category, out var categoryIndex))
            {
                return;
            }

            // Ensure the ItemSet is unique.
            var itemSetList = m_CategoryItemSets[categoryIndex].ItemSetList;

            for (int i = 0; i < itemSetList.Count; ++i)
            {
                var uniqueItemSet = false;
                for (int j = 0; j < itemSetList[i].Slots.Length; ++j)
                {
                    if (itemSet.Slots[j] != itemSetList[i].Slots[j])
                    {
                        uniqueItemSet = true;
                        break;
                    }
                }
                if (!uniqueItemSet)
                {
                    return;
                }
            }

            // The ItemSet is unique. Add it to the list.
            AddItemSet(itemSet, defaultItemSet, categoryIndex);
        }
Пример #6
0
        /// <summary>
        /// Returns the corresponding category index which maps to the category.
        /// </summary>
        /// <param name="category">The interested category.</param>
        /// <returns>The corresponding category index which maps to the category.</returns>
        public int CategoryToIndex(IItemCategoryIdentifier category)
        {
            if (category == null)
            {
                return(-1);
            }

            if (m_CategoryIndexMap.TryGetValue(category, out var index))
            {
                return(index);
            }

            return(-1);
        }
        /// <summary>
        /// Adds the item with the specified ItemCategoryIdentifier.
        /// </summary>
        /// <param name="item">The item that should be added.</param>
        /// <param name="category">The category that the item should be aded to.</param>
        private void AddItem(Item item, IItemCategoryIdentifier category)
        {
            AddItemSet(item, category);
            // The category can have multiple parents. Keep trying to add the item to all of the possible ItemSets.
            var categoryParents = category.GetDirectParents();

            if (categoryParents != null)
            {
                for (int i = 0; i < categoryParents.Count; ++i)
                {
                    AddItem(item, categoryParents[i]);
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Returns true if the ItemCategory represents the default ItemCategory.
        /// </summary>
        /// <param name="itemDefinition">The ItemDefinition to determine if it is the default ItemCategory.</param>
        /// <param name="itemCategory">The ItemCategory to determine if it is the default ItemCategory.</param>
        /// <returns>True if the ItemCategory represents the default ItemCategory.</returns>
        private bool IsDefaultItemCategory(ItemDefinitionBase itemDefinition, IItemCategoryIdentifier itemCategory)
        {
            var categoryParents = itemCategory.GetDirectParents();

            if (categoryParents != null)
            {
                for (int i = 0; i < categoryParents.Count; ++i)
                {
                    if (IsDefaultItemCategory(itemDefinition, categoryParents[i]))
                    {
                        return(true);
                    }
                }
            }

            var index = CategoryToIndex(itemCategory);

            if (index == -1)
            {
                return(false);
            }

            // The default category does not match the active category. Return false.
            if (m_CategoryItemSets[index].DefaultItemSetIndex != m_ActiveItemSetIndex[index])
            {
                return(false);
            }

            // The default category is active. Ensure the ItemDefinition is in that ItemSet.
            var hasItemDefinition = false;
            var itemSetList       = m_CategoryItemSets[index].ItemSetList[m_ActiveItemSetIndex[index]];

            for (int i = 0; i < itemSetList.Slots.Length; ++i)
            {
                if (IsChildOf(itemDefinition, itemSetList.Slots[i]))
                {
                    hasItemDefinition = true;
                    break;
                }
            }
            if (!hasItemDefinition)
            {
                return(false);
            }

            return(true);
        }
Пример #9
0
        /// <summary>
        /// Adds the ItemSet for the specified item if it doesn't already exist.
        /// </summary>
        /// <param name="item">The item to add the ItemSet for.</param>
        /// <param name="itemSet">The ItemSet to add.</param>
        /// <param name="defaultItemSet">Is the ItemSet the default ItemSet within the category?</param>
        /// <param name="category">The category that the ItemSet is trying to be added to.</param>
        private void AddItemSet(Item item, ItemSet itemSet, bool defaultItemSet, IItemCategoryIdentifier category)
        {
            // The category can have multiple parents. Keep trying to add the ItemSet to all of the possible categories.
            var categoryParents = category.GetDirectParents();

            if (categoryParents != null)
            {
                for (int i = 0; i < categoryParents.Count; ++i)
                {
                    AddItemSet(item, itemSet, defaultItemSet, categoryParents[i]);
                }
            }

            // The category may not have been added to the ItemSetManager.
            if (!m_CategoryIndexMap.TryGetValue(category, out var categoryIndex))
            {
                return;
            }

            var addItemSet  = true;
            var itemSetList = m_CategoryItemSets[categoryIndex].ItemSetList;

            for (int i = 0; i < itemSetList.Count; ++i)
            {
                var slots     = itemSetList[i].Slots;
                var slotMatch = true;
                for (int j = 0; j < slots.Length; ++j)
                {
                    if (slots[j] != itemSet.Slots[j])
                    {
                        slotMatch = false;
                        break;
                    }
                }
                if (slotMatch)
                {
                    addItemSet = false;
                    break;
                }
            }
            // If the ItemSet doesn't exist then add it to the list.
            if (addItemSet)
            {
                AddItemSet(itemSet, defaultItemSet, categoryIndex);
            }
        }
Пример #10
0
        /// <summary>
        /// Adds the item with the specified ItemCategoryIdentifier.
        /// </summary>
        /// <param name="item">The item that should be added.</param>
        /// <param name="category">The category that the item should be aded to.</param>
        private void AddItem(Item item, IItemCategoryIdentifier category)
        {
            if (category == null)
            {
                Debug.LogError("Error: No category has been specified. Ensure the ItemIdentifier has been added to a category.");
                return;
            }

            AddItemSet(item, category);
            // The category can have multiple parents. Keep trying to add the item to all of the possible ItemSets.
            var categoryParents = category.GetDirectParents();

            if (categoryParents != null)
            {
                for (int i = 0; i < categoryParents.Count; ++i)
                {
                    AddItem(item, categoryParents[i]);
                }
            }
        }
 /// <summary>
 /// Is the category an ItemSet category?
 /// </summary>
 /// <param name="category">The category that may be an ItemSet category.</param>
 /// <returns>True if the category is an ItemSet category.</returns>
 protected virtual bool IsItemSetCategory(IItemCategoryIdentifier category)
 {
     return(true);
 }
Пример #12
0
 /// <summary>
 /// Future method.
 /// </summary>
 bool IItemCategoryIdentifier.InherentlyContains(IItemCategoryIdentifier other, bool includeThis)
 {
     return(false); // Intentional, structure for future work.
 }
Пример #13
0
        /// <summary>
        /// Adds a new ItemSet for the specified item if it doesn't already exist.
        /// </summary>
        /// <param name="item">The item to add the ItemSet for.</param>
        /// <param name="category">The category that the item should be aded to.</param>
        private void AddItemSet(Item item, IItemCategoryIdentifier category)
        {
            // The category may not have been added to the ItemSetManager.
            if (category == null || !m_CategoryIndexMap.TryGetValue(category, out var categoryIndex))
            {
                return;
            }

            var            addItemSet = item.UniqueItemSet;
            var            stateName  = string.Empty;
            List <ItemSet> itemSetList;
            var            addedDedicatedItemDefinition = false;

            for (int i = 0; i < m_CategoryItemSets.Length; ++i)
            {
                itemSetList = m_CategoryItemSets[i].ItemSetList;
                addedDedicatedItemDefinition = false;
                for (int j = 0; j < itemSetList.Count; ++j)
                {
                    // If the item instance has already been added then no new item set needs to be created.
                    if (itemSetList[j].ItemIdentifiers[item.SlotID] == item.ItemIdentifier)
                    {
                        addItemSet = false;
                        break;
                    }
                    // The ItemDefinition exists but the ItemIdentifier does not. Populate the ItemIdentifier with the current item and then stop searching.
                    if (itemSetList[j].ItemIdentifiers[item.SlotID] == null && itemSetList[j].Slots[item.SlotID] == item.ItemDefinition)
                    {
                        // The item definition matches. Newly added items need to have an ItemSet to themselves.
                        var dedicatedItemDefinition = true;
                        for (int k = 0; k < itemSetList[j].Slots.Length; ++k)
                        {
                            if (k == item.SlotID)
                            {
                                continue;
                            }

                            if (itemSetList[j].Slots[k] != null)
                            {
                                dedicatedItemDefinition = false;
                                break;
                            }
                        }
                        if (dedicatedItemDefinition)
                        {
                            // Multiple of the same ItemDefinition items may be added. Do not keep assigning the ItemIdentifier to dedicated ItemSets
                            // to allow other items with the same ItemDefinition to have an open ItemSet.
                            if (!addedDedicatedItemDefinition)
                            {
                                itemSetList[j].ItemIdentifiers[item.SlotID] = item.ItemIdentifier;
                            }
                            addedDedicatedItemDefinition = true;
                            addItemSet = false; // Do not break within the loop because multiple definitions may exist for the same identifier.
                        }
                        else
                        {
                            itemSetList[j].ItemIdentifiers[item.SlotID] = item.ItemIdentifier;
                        }
                        // Any new child item definitions should use the same state name as the parent item definitions.
                    }
                    else if (IsChildOf(item.ItemDefinition, itemSetList[j].Slots[item.SlotID]) && !string.IsNullOrEmpty(itemSetList[j].State) && string.IsNullOrEmpty(stateName))
                    {
                        var dedicatedItemDefinition = true;
                        for (int k = 0; k < itemSetList[j].Slots.Length; ++k)
                        {
                            if (k == item.SlotID)
                            {
                                continue;
                            }

                            if (itemSetList[j].Slots[k] != null)
                            {
                                dedicatedItemDefinition = false;
                                break;
                            }
                        }
                        if (dedicatedItemDefinition)
                        {
                            stateName = itemSetList[j].State;
                        }
                    }
                }
            }

            // If no ItemSet exists with the added Item then add a new ItemSet.
            itemSetList = m_CategoryItemSets[categoryIndex].ItemSetList;
            if (addItemSet)
            {
                var itemSet = new ItemSet(m_Inventory.SlotCount, item.SlotID, item.ItemDefinition, item.ItemIdentifier, stateName);
                // If the parent ItemIdentifier is not null then the new ItemSet should be added after the ItemSets with the same parent.
                if (item.ItemDefinition.GetParent() != null)
                {
                    var insertIndex = -1;
                    for (int i = 0; i < itemSetList.Count; ++i)
                    {
                        if (IsChildOf(item.ItemDefinition, itemSetList[i].Slots[item.SlotID]))
                        {
                            // The other slot elements must be empty.
                            var canInsert = true;
                            for (int j = 0; j < itemSetList[i].Slots.Length; ++j)
                            {
                                if (j == item.SlotID)
                                {
                                    continue;
                                }

                                if (itemSetList[i].Slots[j] != null)
                                {
                                    canInsert = false;
                                    break;
                                }
                            }
                            if (canInsert)
                            {
                                insertIndex = i + 1;
                            }
                        }
                        else if (insertIndex != -1)
                        {
                            // The ItemSet should be inserted after the last ItemSet with the same parent.
                            break;
                        }
                    }
                    // Insert the ItemSet with the child ItemDefinition.
                    insertIndex = insertIndex != -1 ? insertIndex : itemSetList.Count;
                    itemSetList.Insert(insertIndex, itemSet);

                    // If the ItemSet was inserted before the default or active index then the index needs to update to stay accurate.
                    if (insertIndex <= m_CategoryItemSets[categoryIndex].DefaultItemSetIndex)
                    {
                        m_CategoryItemSets[categoryIndex].DefaultItemSetIndex += 1;
                    }
                    if (insertIndex <= m_ActiveItemSetIndex[categoryIndex])
                    {
                        m_ActiveItemSetIndex[categoryIndex] += 1;
                        EventHandler.ExecuteEvent(m_GameObject, "OnItemSetIndexChange", categoryIndex, m_ActiveItemSetIndex[categoryIndex]);
                    }

                    // The ItemSet must be duplicated for any ItemDefinition that has a similar parent.
                    for (int i = itemSetList.Count - 1; i >= 0; --i)
                    {
                        if (IsChildOf(item.ItemDefinition, itemSetList[i].Slots[item.SlotID]))
                        {
                            // Another ItemDefinition must exist in order for the ItemSet to be duplicated.
                            for (int j = 0; j < itemSetList[i].Slots.Length; ++j)
                            {
                                if (j == item.SlotID)
                                {
                                    continue;
                                }
                                // The ItemSet is unique. Duplicate it.
                                if (itemSetList[i].Slots[j] != null)
                                {
                                    DuplicateItemSet(item, categoryIndex, i);
                                    break;
                                }
                            }
                        }
                    }
                }
                else
                {
                    // The ItemDefinition doesn't have a parent. Add the ItemSet to the end of the list.
                    itemSet.Initialize(m_GameObject, this, m_CategoryItemSets[categoryIndex].CategoryID, categoryIndex, itemSetList.Count);
                    // The ItemDefinition may already exists within the ItemSetList. Use the same state name.
                    for (int i = 0; i < itemSetList.Count; ++i)
                    {
                        if (IsChildOf(item.ItemDefinition, itemSetList[i].Slots[item.SlotID]) && !string.IsNullOrEmpty(itemSetList[i].State))
                        {
                            var dedicatedItemSet = true;
                            for (int j = 0; j < itemSetList[i].Slots.Length; ++j)
                            {
                                if (j == item.SlotID)
                                {
                                    continue;
                                }
                                if (itemSetList[i].Slots[j] != null)
                                {
                                    dedicatedItemSet = false;
                                    break;
                                }
                            }
                            if (dedicatedItemSet)
                            {
                                itemSet.State = itemSetList[i].State;
                                break;
                            }
                        }
                    }
                    itemSetList.Add(itemSet);
                }
            }
            else if (!item.UniqueItemSet)
            {
                // The individual ItemSet doesn't need to be added, but it may need to duplicate an existing ItemSet.
                for (int i = 0; i < itemSetList.Count; ++i)
                {
                    if (itemSetList[i].Slots[item.SlotID] != item.ItemDefinition || itemSetList[i].ItemIdentifiers[item.SlotID] == item.ItemIdentifier)
                    {
                        continue;
                    }

                    for (int j = 0; j < itemSetList[i].Slots.Length; ++j)
                    {
                        if (j == item.SlotID)
                        {
                            continue;
                        }
                        if (itemSetList[i].Slots[j] != null)
                        {
                            DuplicateItemSet(item, categoryIndex, i);
                            break;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Adds a new ItemSet for the specified item if it doesn't already exist.
        /// </summary>
        /// <param name="item">The item to add the ItemSet for.</param>
        /// <param name="category">The category that the item should be aded to.</param>
        private void AddItemSet(Item item, IItemCategoryIdentifier category)
        {
            // The category may not have been added to the ItemSetManager.
            if (category == null || !m_CategoryIndexMap.TryGetValue(category, out var categoryIndex))
            {
                return;
            }

            var            addItemSet = item.UniqueItemSet;
            List <ItemSet> itemSetList;

            for (int i = 0; i < m_CategoryItemSets.Length; ++i)
            {
                itemSetList = m_CategoryItemSets[i].ItemSetList;
                for (int j = 0; j < itemSetList.Count; ++j)
                {
                    // If the item instance has already been added then no new item set needs to be created.
                    if (itemSetList[j].ItemIdentifiers[item.SlotID] == item.ItemIdentifier)
                    {
                        addItemSet = false;
                        break;
                    }
                    // The ItemDefinition exists but the ItemIdentifier does not. Populate the ItemIdentifier with the current item and then stop searching.
                    if (itemSetList[j].ItemIdentifiers[item.SlotID] == null && itemSetList[j].Slots[item.SlotID] == item.ItemDefinition)
                    {
                        itemSetList[j].ItemIdentifiers[item.SlotID] = item.ItemIdentifier;

                        // The item definition matches. Newly added items need to have an ItemSet to themselves.
                        var dedicatedItemDefinition = true;
                        for (int k = 0; k < itemSetList[j].Slots.Length; ++k)
                        {
                            if (k == item.SlotID)
                            {
                                continue;
                            }

                            if (itemSetList[j].Slots[k] != null)
                            {
                                dedicatedItemDefinition = false;
                                break;
                            }
                        }
                        if (dedicatedItemDefinition)
                        {
                            addItemSet = false; // Do not break within the loop because multiple definitions may exist for the same identifier.
                        }
                    }
                }
            }

            // If no ItemSet exists with the added Item then add a new ItemSet.
            if (addItemSet)
            {
                itemSetList = m_CategoryItemSets[categoryIndex].ItemSetList;
                var itemSet = new ItemSet(m_Inventory.SlotCount, item.SlotID, item.ItemDefinition, item.ItemIdentifier, string.Empty);
                // If the parent ItemIdentifier is not null then the new ItemSet should be added after the ItemSets with the same parent.
                if (item.ItemDefinition.GetParent() != null)
                {
                    var insertIndex = -1;
                    for (int i = 0; i < itemSetList.Count; ++i)
                    {
                        if (IsChildOf(item.ItemDefinition, itemSetList[i].Slots[item.SlotID]))
                        {
                            // The other slot elements must be empty.
                            var canInsert = true;
                            for (int j = 0; j < itemSetList[i].Slots.Length; ++j)
                            {
                                if (j == item.SlotID)
                                {
                                    continue;
                                }

                                if (itemSetList[i].Slots[j] != null)
                                {
                                    canInsert = false;
                                    break;
                                }
                            }
                            if (canInsert)
                            {
                                insertIndex = i + 1;
                            }
                        }
                        else if (insertIndex != -1)
                        {
                            // The ItemSet should be inserted after the last ItemSet with the same parent.
                            break;
                        }
                    }
                    // Insert the ItemSet with the child ItemIdentifier.
                    insertIndex = insertIndex != -1 ? insertIndex : itemSetList.Count;
                    itemSetList.Insert(insertIndex, itemSet);

                    // If the ItemSet was inserted before the active index then the active index needs to update to stay accurate.
                    if (insertIndex <= m_ActiveItemSetIndex[categoryIndex])
                    {
                        m_ActiveItemSetIndex[categoryIndex] += 1;
                        EventHandler.ExecuteEvent(m_GameObject, "OnItemSetIndexChange", categoryIndex, m_ActiveItemSetIndex[categoryIndex]);
                    }

                    // The ItemSet must be duplicated for any ItemIdentifier that has a similar parent.
                    for (int i = itemSetList.Count - 1; i >= 0; --i)
                    {
                        var insertItemSet = false;
                        if (IsChildOf(item.ItemDefinition, itemSetList[i].Slots[item.SlotID]))
                        {
                            // Another ItemIdentifier must exist in order for the ItemSet to be duplicated.
                            for (int j = 0; j < itemSetList[i].Slots.Length; ++j)
                            {
                                if (j == item.SlotID)
                                {
                                    continue;
                                }
                                if (itemSetList[i].Slots[j] != null)
                                {
                                    insertItemSet = true;
                                    break;
                                }
                            }
                        }

                        if (insertItemSet)
                        {
                            var duplicatedItemSet = new ItemSet(itemSetList[i]);
                            duplicatedItemSet.Slots[item.SlotID]           = item.ItemDefinition;
                            duplicatedItemSet.ItemIdentifiers[item.SlotID] = item.ItemIdentifier;
                            duplicatedItemSet.Initialize(m_GameObject, this, m_CategoryItemSets[categoryIndex].CategoryID, categoryIndex, i + 1);
                            itemSetList.Insert(i + 1, duplicatedItemSet);

                            // All of the subsequent ItemSets need to update their index.
                            for (int j = i + 2; j < itemSetList.Count; ++j)
                            {
                                itemSetList[j].Index = j;
                            }

                            // If the ItemSet was inserted before the active index then the active index needs to update to stay accurate.
                            if (i + 1 <= m_ActiveItemSetIndex[categoryIndex])
                            {
                                m_ActiveItemSetIndex[categoryIndex] += 1;
                                EventHandler.ExecuteEvent(m_GameObject, "OnItemSetIndexChange", categoryIndex, m_ActiveItemSetIndex[categoryIndex]);
                            }
                        }
                    }
                }
                else
                {
                    // The ItemIdentifier doesn't have a parent. Add the ItemSet to the end of the list.
                    itemSet.Initialize(m_GameObject, this, m_CategoryItemSets[categoryIndex].CategoryID, categoryIndex, itemSetList.Count);
                    itemSetList.Add(itemSet);
                }
            }
        }
Пример #15
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);
            }
        }