【问题标题】:C# Crafting LogicC# 制作逻辑
【发布时间】:2014-04-22 00:06:01
【问题描述】:

有很多类似的问题,但没有一个有明确的答案。我正在使用 Unity3D 引擎创建游戏。我已经建立了一个库存系统,其中包含添加项目、删除项目和检查库存是否包含一定数量的项目的方法。

我想要做的是在每次更新时,循环浏览物品,如果玩家有制作配方所需的物品,显示添加一个按钮,玩家可以点击该按钮来制作新物品。

我想我可以使用字典来存储所需的项目和数量。像这样的:

    public static Dictionary<string, int> sword = new Dictionary<string, int>() {
        {"stone", 2},
        {"Wood", 1}
    };

    public static Dictionary<string, int> plank = new Dictionary<string, int>() {
        {"Wood", 5}
    };

但我还需要一种方法来存储所有食谱并循环访问它们。就像字典词典一样。所以我可以遍历所有的制作项目名称。我可以做类似的事情吗?

public static Dictionary<string, Dictionary> recipes = new Dictionary<string, int>() {
    {"Wood Sword", sword},
    {"Wood Plank", plank}
};

我希望能够在一个类文件中定义所有配方,并在另一个类文件中循环遍历它们。

我没有太多的 c# 经验,所以我不太擅长使用列表和字典,所以我很感激任何建议和帮助。

谢谢!

【问题讨论】:

    标签: c# list dictionary unity3d


    【解决方案1】:

    嗯.. 根据你的规格,我想出了这样的东西:

        public enum Ingredient = { Wood, Stone, Water, Gold/*and so on*/ };
    
        public class RecipeItem 
        {
                public Ingredient Ingredient{ get; set;}
                public int Amount {get; set;}
        }
    
        public class Recipe
        {
                public string Name {get; set;}
                public List<RecipeItem> RecipeItems {get;set;}
        }
    

    定义配方时代码中的某处:

        var recipes = new List<Recipe>(){
                new Recipe(){
                        Name = "Wood Sword",
                        RecipeItems = new List<RecipeItem>(){
                                new RecipeItem(){
                                        Ingredient = Wood,
                                        Amount = 1
                                },
                                new RecipeItem(){
                                        Ingredient = Stone,
                                        Amount = 5
                                }
    
                        }
                },
                new Recipe(){
                        Name = "Plank",
                        RecipeItems = new List<RecipeItem>(){
                                new RecipeItem(){
                                        Ingredient = Wood,
                                        Amount = 5
                                }
    
                        }
                }
                // More recipes
        };
    

    PS:我建议你花几天时间学习 C# 的基础知识

    【讨论】:

    • 谢谢,我最终做了类似的事情。
    【解决方案2】:

    您要查找的内容类似于 Dictionary&lt;string, Dictionary&lt;string, int&gt;&gt; - 带有 string 键和 Dictionary&lt;string, int&gt; 值的 Dictionary。像这样初始化它:

    var recipes = new Dictionary<string, Dictionary<string, int>>
    {
        { "Sword", new Dictionary<string, int> { { "stone", 2 }, { "wood", 1 } } },
        { "Plank", new Dictionary<string, int> { { "wood", 5 } } }
    };
    

    就我个人而言,我觉得这有点沉重,而且很容易搞砸。我更倾向于使用结构来保存内部结构,例如:

    public struct RecipeComponent
    {
        public readonly string Material;
        public readonly int Count;
    
        public RecipeComponent(string material, int count)
        {
            Material = material;
            Count = count;
        }   
    }
    
    public readonly Dictionary<string, RecipeComponent[]> recipes = new Dictionary<string, RecipeComponent[]>
    {
        { "Sword", new[] { new RecipeComponent("stone", 2), new RecipeComponent("Wood", 1) } },
        { "Plank", new[] { new RecipeComponent("Wood", 5) } }
    };
    

    稍微更健壮,但您仍然可以在运行时操作recipes 列表的内容,包括替换所需组件的列表。 ReadOnlyDictionary 有助于解决这个问题。

    【讨论】:

      【解决方案3】:

      到目前为止(对我来说),最简单的方法是使用通用列表和 for 循环。 第一:使用此功能检查物品是否在库存中 public bool CheckItems(列表检查) {

      List<Item> it = new List<Item> (check);// Make sure original list is not changed.
              foreach(Item inventoryItem in myItems) {// Loop through your INVENTORY's items.
                  if (it.Contains(inventoryItem)) {//If contains, remove.
                      it.Remove(inventoryItem);
                      if(it.Count == 0) {
                          return true; //When the count is less than zero, you can craft the item.
                      }
                  }
              }
              return false;
          }
      

      只需要在craft函数中调用,如果返回true,就可以制作物品了。 注意:Check list 是工艺所需的物品清单,myItems 是库存物品清单。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多