【发布时间】:2021-02-24 14:55:17
【问题描述】:
我需要一些关于我的游戏代码的帮助。这是一个关于制作菜肴和送货的游戏。您需要捕获一些在地下生成的成分,当您捕获它们时,它们会进入您的库存。
有 3 种类型的食谱,有 1、2 和 3 种成分。此外,不需要在您的库存中按顺序排列成分即可完成配方,它应该只是验证您是否拥有正确的物品。如果您有 3 种成分,则应在具有 3 种成分的食谱列表中验证是否有这些元素。
成分有名称和ID。
public abstract class IIngredient : IItem {
public string ingredientName;
public int ingredientId;
}
菜谱是一个IIngredient列表,菜谱有一个名字:
public abstract class IRecipe {
public List<IIngredient> ingredients;
public string recipe_name;
}
还有库存
public abstract class IInventory
{
public List<IIngredient> ingredient_inventory;
public abstract void AddIngredient(IIngredient ingredient);
}
玩家库存:
public class PlayerInventory : IInventory
{
public PlayerInventory()
{
this.ingredient_inventory = new List<IIngredient>();
}
public override void AddIngredient(IIngredient ingredient)
{
ingredient_inventory.Add(ingredient);
}
}
当我交付物品时,我想检查我的库存中有多少物品,以了解我应该比较哪些食谱,然后尝试将我的物品与所有成分相同的食谱进行匹配.如果有任何食谱匹配,我想知道它是谁。
我正在使用此代码,但它不起作用:
player_inventory_ingredients_id = new List<int>();
player_inventory_ingredients_name = new List<string>();
public override void CheckRecipe(PlayerInventory playerInventory)
{
foreach (IIngredient item_ingredient in playerInventory.ingredient_inventory)
{
player_inventory_ingredients_name.Add(item_ingredient.ingredientName);
Debug.Log("You have " + item_ingredient.ingredientName);
}
List<IRecipe> recipes_item_amount = recipes.FindAll
(i => i.ingredients.Count == playerInventory.ingredient_inventory.Count);
foreach (IRecipe recipe_name in recipes_item_amount)
{
Debug.Log("Possible recipes are " + recipe_name.recipe_name);
List<string> recipe_temp = new List<string>();
IEnumerable<string> test_the_names = player_inventory_ingredients_name.Except(recipe_temp);
foreach (IIngredient ingredients in recipe_name.ingredients)
{
Debug.Log(recipe_name + " ingredient is: " + ingredients);
recipe_temp.Add(ingredients.ingredientName);
}
}
}
最后一个代码块,我尝试验证并检查是否有 itens,但没有显示任何结果。我什至不知道我写的代码是否有意义嘿嘿
【问题讨论】:
-
最后一个代码块,我尝试验证并检查是否有 itens,但没有显示任何结果。我什至不知道我写的代码是否有意义嘿嘿
-
那么你需要三个嵌套的for循环1)用户2)配方名称3)成分
-
@GertArnold 成功了!谢谢!
-
@jdweng 你介意写吗?
-
球员名单在哪里。我看不出你是从哪里得到玩家名字的?
标签: c# list linq unity3d except