C# 游戏制作系统逻辑?

C# 游戏制作系统逻辑?,c#,unity3d,C#,Unity3d,各位程序员好。我有一个关于Unity3d游戏制作的问题,但更多的是关于C#。 我有一个名为Item的类(它只包含字符串itemname和int值)。我还有一个名为crafting recipe的类,其中有一个项名为output,我不确定的是,我是否应该为inputitems变量使用字典或泛型列表。此外,在创建库存类时,我是否使用字典或库存中项目的通用列表 虽然这是我真正需要帮助的部分(我以前尝试过,但没有成功),但我如何让它在我制作时检查库存中的所有项目,如果我有库存中所需的所有项目(因此检查库

各位程序员好。我有一个关于Unity3d游戏制作的问题,但更多的是关于C#。 我有一个名为Item的类(它只包含字符串itemname和int值)。我还有一个名为crafting recipe的类,其中有一个项名为output,我不确定的是,我是否应该为inputitems变量使用字典或泛型列表。此外,在创建库存类时,我是否使用字典或库存中项目的通用列表

虽然这是我真正需要帮助的部分(我以前尝试过,但没有成功),但我如何让它在我制作时检查库存中的所有项目,如果我有库存中所需的所有项目(因此检查库存中是否有所有输入项),它将删除它们,并将输出项目添加到库存中。我也用C#。 谢谢:)

下面是一个例子:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Inventory : MonoBehaviour {
//Should this be a dictionary.
    public List<Item> InventoryList = new List<Item>();

}

public class Item{

    public string ItemName;
    public int ItemValue;

}

public class CraftingItem{
//Should I make this a dictionary or leave it?
    public List<Item> InputItems = new List<Item>();
    public Item Output;
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共类清单:单一行为{
//这应该是一本字典。
public List InventoryList=新列表();
}
公共类项目{
公共字符串ItemName;
公共价值;
}
公共类手工项目{
//我应该把这本书做成字典还是留下来?
public List InputItems=新列表();
公共项目产出;
}

我将按以下方式执行此操作:

class Program
{
    public class Inventory : List<Item>
    {
        public int Weight { get; set; } 
        // some other properties 
    }

    public class Item : IEquatable<Item>
    {
        public string Name { get; set; }
        public int Value { get; set; }

        public bool Equals(Item other)
        {
            return this.Name == other.Name; 
        }
    }

    public class ItemsComparer : IEqualityComparer<Item>
    {
        public bool Equals(Item x, Item y)
        {
            if (x.Name.Equals(y.Name)) return true;

            return false; 
        }

        public int GetHashCode(Item obj)
        {
            return 0; 
        }
    }

    public class CraftingRecipe
    {
        private List<Item> _recipe;
        private Item _outputItem; 

        public CraftingRecipe(List<Item> recipe, Item outputItem)
        {
            _recipe = recipe;
            _outputItem = outputItem; 
        }

        public Item CraftItem(Inventory inventory)
        {
            if (_recipe == null)
            {
                //throw some ex
            }

            var commonItems = _recipe.Intersect(inventory, new ItemsComparer()).ToList();
            if (commonItems.Count == _recipe.Count)
            {
                inventory.RemoveAll(x => commonItems.Any(y => y.Name == x.Name));
                return _outputItem; 
            }

            return null; 
        }

    }

    static void Main(string[] args)
    {
        List<Item> recipeItems = new List<Item>() 
            { 
                new Item { Name = "Sword" } ,
                new Item { Name = "Magic Stone" }
            }; 
        Item outputItem = new Item() { Name ="Super magic sword" }; 

        Inventory inventory = new Inventory() 
            { 
                new Item { Name = "Sword" } ,
                new Item { Name = "Ring" },
                new Item { Name = "Magic Stone" }
            };

        CraftingRecipe craftingRecipe =
            new CraftingRecipe(recipeItems, outputItem);

        var newlyCraftedItem = craftingRecipe.CraftItem(inventory);

        if (newlyCraftedItem != null)
        {
            Console.WriteLine(newlyCraftedItem.Name);
        }
        else
        {
            Console.WriteLine("Your item has not been crafted"); 
        }
        Console.Read(); 
    }
类程序
{
公共类目录:列表
{
公共整数权重{get;set;}
//其他一些属性
}
公共类项:IEquatable
{
公共字符串名称{get;set;}
公共int值{get;set;}
公共布尔等于(项目其他)
{
返回此.Name==other.Name;
}
}
公共类项目比较者:IEqualityComparer
{
公共布尔等于(项目x、项目y)
{
如果(x.Name.Equals(y.Name))返回true;
返回false;
}
公共int GetHashCode(项目obj)
{
返回0;
}
}
公共类工艺配方
{
私人列表(配方),;
私人物品(输出物品);;
公共工艺配方(列表配方、项目输出项)
{
_配方=配方;
_outputItem=outputItem;
}
公共物品(库存)
{
如果(_recipe==null)
{
//扔一些前男友
}
var commonItems=_recipe.Intersect(库存,新项目比较程序()).ToList();
if(commonItems.Count==\u recipe.Count)
{
inventory.RemoveAll(x=>commonItems.Any(y=>y.Name==x.Name));
返回输出项;
}
返回null;
}
}
静态void Main(字符串[]参数)
{
列表recipeItems=新列表()
{ 
新项目{Name=“Swarm”},
新项目{Name=“魔法石”}
}; 
Item outputItem=new Item(){Name=“超级魔剑”};
库存=新库存()
{ 
新项目{Name=“Swarm”},
新项目{Name=“Ring”},
新项目{Name=“魔法石”}
};
制作配方制作配方=
新工艺配方(recipeItems、OutputiItem);
var newlyCraftedItem=craftingRecipe.CraftItem(库存);
if(newlyCraftedItem!=null)
{
Console.WriteLine(newlyCraftedItem.Name);
}
其他的
{
Console.WriteLine(“您的项目尚未制作”);
}
Console.Read();
}

一些代码来演示您正在做的事情可能会对我们有所帮助。谢谢!!!您也可以键入一行代码来演示如何制作物品,同时,库存脚本必须继承monobehavior。我已经更新了代码并放置了制作和物品的逻辑(CraftItem方法)-现在它将物品从库存中删除(用于制作物品)并返回制作的物品。