/// <summary> /// Initializes the ItemCollection reference. This is useful if the ItemCollection is assigned after Awake is called. /// </summary> /// <param name="itemCollection">The ItemCollection to initialize.</param> private void InitializeItemCollection(ItemCollection itemCollection) { // The ItemCollection has already been initialized. if (m_Inventory != null) { return; } if (itemCollection == null) { return; } m_ItemCollection = itemCollection; m_GameObject = gameObject; m_Inventory = m_GameObject.GetCachedComponent <InventoryBase>(); if (m_CategoryItemSets.Length == 0 && m_ItemCollection.Categories.Length > 0) { m_CategoryItemSets = new CategoryItemSet[m_ItemCollection.Categories.Length]; } m_ActiveItemSetIndex = new int[m_CategoryItemSets.Length]; m_NextItemSetIndex = new int[m_ActiveItemSetIndex.Length]; for (int i = 0; i < m_CategoryItemSets.Length; ++i) { m_ActiveItemSetIndex[i] = -1; m_NextItemSetIndex[i] = -1; if (m_CategoryItemSets[i] == null) { m_CategoryItemSets[i] = new CategoryItemSet(m_ItemCollection.Categories[i].ID); } } // Store the category index value within the ItemType to prevent the index from having to be retrieved every time the ID is used. var itemTypes = m_ItemCollection.ItemTypes; for (int i = 0; i < itemTypes.Length; ++i) { var indices = new int[itemTypes[i].CategoryIDs.Length]; for (int j = 0; j < itemTypes[i].CategoryIDs.Length; ++j) { var categoryID = itemTypes[i].CategoryIDs[j]; for (int k = 0; k < m_CategoryItemSets.Length; ++k) { if (m_CategoryItemSets[k].CategoryID == categoryID) { indices[j] = k; break; } } } itemTypes[i].CategoryIndices = indices; } if (Application.isPlaying) { EventHandler.RegisterEvent <Item>(gameObject, "OnInventoryAddItem", OnAddItem); } }
/// <summary> /// Initializes the ItemSetManager. /// </summary> /// <param name="force">Should the ItemSet be force initialized?</param> public override void Initialize(bool force) { if (m_Initialized && !force) { return; } m_Initialized = true; if (m_ItemCollection == null) { m_CategoryItemSets = null; return; } // The ItemTypes get their categories from the ItemCollection. for (int i = 0; i < m_ItemCollection.ItemTypes.Length; ++i) { m_ItemCollection.ItemTypes[i].Initialize(m_ItemCollection); } // Initialize the categories. if ((m_CategoryItemSets == null || m_CategoryItemSets.Length == 0)) { if (m_ItemCollection.Categories.Length > 0) { m_CategoryItemSets = new CategoryItemSet[m_ItemCollection.Categories.Length]; } } else if (m_CategoryItemSets.Length != m_ItemCollection.Categories.Length) { System.Array.Resize(ref m_CategoryItemSets, m_ItemCollection.Categories.Length); } if (m_CategoryIndexMap == null) { m_CategoryIndexMap = new Dictionary <IItemCategoryIdentifier, int>(); } else { m_CategoryIndexMap.Clear(); } m_ActiveItemSetIndex = new int[m_CategoryItemSets.Length]; m_NextItemSetIndex = new int[m_ActiveItemSetIndex.Length]; for (int i = 0; i < m_CategoryItemSets.Length; ++i) { m_ActiveItemSetIndex[i] = -1; m_NextItemSetIndex[i] = -1; if (m_CategoryItemSets[i] == null) { m_CategoryItemSets[i] = new CategoryItemSet(m_ItemCollection.Categories[i].ID, m_ItemCollection.Categories[i].name, m_ItemCollection.Categories[i]); } else { m_CategoryItemSets[i].CategoryID = m_ItemCollection.Categories[i].ID; m_CategoryItemSets[i].CategoryName = m_ItemCollection.Categories[i].name; m_CategoryItemSets[i].ItemCategory = m_ItemCollection.Categories[i]; } // Create a mapping between the category and index. var category = m_ItemCollection.GetCategory(m_CategoryItemSets[i].CategoryID); m_CategoryIndexMap.Add(category, i); // The ItemSet must be initialized. for (int j = 0; j < m_CategoryItemSets[i].ItemSetList.Count; ++j) { m_CategoryItemSets[i].ItemSetList[j].Initialize(gameObject, this, m_CategoryItemSets[i].CategoryID, i, j); } } }