Ejemplo n.º 1
0
    public GameObject InventoryDrop(int index, bool dontDrop = false)
    {
        if (inventoryItems.Count <= index)
        {
            return(null);
        }

        InventoryItemType item = inventoryItems[index];

        if (item == null)
        {
            return(null);
        }

        GameObject droppedItem = item.instance;

        if (droppedItem == null)
        {
            return(null);
        }

        {
            droppedItem.SetActive(true);
            droppedItem.transform.position = transform.position + (visualObject.transform.forward * 0.5f);

            BaseInventoryItem itemComponent = droppedItem.GetComponent <BaseInventoryItem>();
            if (itemComponent != null && itemComponent.onDrop != null)
            {
                itemComponent.onDrop.Invoke();
            }
        }

        if (dontDrop || item.deleteOnDrop)
        {
            Destroy(droppedItem);
        }

        inventoryItems.RemoveAt(index);

        if (onInventoryChanged != null)
        {
            onInventoryChanged();
        }

        return(droppedItem);
    }
Ejemplo n.º 2
0
        /// <summary>
        /// Maps all the common properties in an inventory item's base class to an inventory model's base class.
        /// </summary>
        /// <param name="sourceItem">The inventory item we are mapping to a model.</param>
        /// <param name="targetModel">A reference to the model we are mapping the base inventory item's properties to.</param>
        private static void MapCommonModel(BaseInventoryItem sourceItem, ref BaseInventoryModel targetModel)
        {
            // Throw exceptions if any parameters are null.
            if (sourceItem == null)
            {
                throw new ArgumentNullException(nameof(sourceItem));
            }

            if (targetModel == null)
            {
                throw new ArgumentNullException(nameof(targetModel));
            }

            // Set all the common properties of the inventory model from the source inventory item.
            targetModel.Title       = sourceItem.Title;
            targetModel.Cost        = sourceItem.Cost;
            targetModel.Genre       = sourceItem.Genre;
            targetModel.Platform    = sourceItem.Platform;
            targetModel.ReleaseYear = sourceItem.ReleaseYear;
        }
        /// <summary>
        /// Reads in a text file and creates a list of inventory objects based on the input.
        /// The input format is as follows: 'itemType,title,cost,genre,platform,releaseYear,typeSpecificInfo1,typeSpecificInfo2'
        /// </summary>
        /// <param name="path">The file's location.</param>
        /// <returns>A read-only list of inventory item objects.</returns>
        public IReadOnlyList <IInventoryItem> LoadInventory(string path)
        {
            // Make sure that the file exists and throw an exception if it doesn't
            if (!File.Exists(path))
            {
                throw new FileNotFoundException($"File in path \"{path}\" was not found");
            }

            // Read in the entire text inventory file.
            string[] inventoryText = File.ReadAllLines(path);

            // Create a new empty list of inventory items to return.
            List <IInventoryItem> inventory = new List <IInventoryItem>();

            // Iterate over each line in the file.
            foreach (string line in inventoryText)
            {
                // A try-catch block to check if input line can be transformed into a valid inventory item object.
                try
                {
                    // Get the inventory item from text and add it to the inventory if it is not null.
                    BaseInventoryItem item = InventoryItemFactory.CreateInventoryItem(line);
                    if (item != null)
                    {
                        inventory.Add(item);
                    }
                }
                catch
                {
                    // Skip any invalid entries.
                }
            }

            // Return the loaded inventory as a read-only list of inventory item objects.
            return(inventory);
        }