Exemplo n.º 1
0
    /// <summary>
    /// Using the materials in the passed inventory, crafts and returns the item the
    /// passed recipe is for.
    /// </summary>
    /// <param name="recipe"></param>
    /// <param name="inventory"></param>
    /// <returns>The recipe's result if the inventory has the mats. Null otherwise.</returns>
    public ItemInfo Craft(RecipeInfo recipe, PP_Inventory inventory)
    {
        ItemInfo itemToCraft = recipe.result;

        // check if the passed inventory has the materials for the recipe
        foreach (ItemInfo material in recipe.materials)
        {
            if (inventory.CountOf(material) < recipe.CountOf(material))
            {
                string messageFormat = "Could not craft {0}; the inventory doesn't have enough {1}s.";
                Debug.Log(string.Format(messageFormat, itemToCraft.name, material.name));
                return(null);
            }
        }

        // Use up the materials
        foreach (ItemInfo material in recipe.materials)
        {
            inventory.Remove(material);
        }

        // and voila! Item-crafting success!
        Debug.Log("Successfully crafted a(n) " + itemToCraft.name);
        return(itemToCraft.Copy());
    }