gpt4 book ai didi

c# - 游戏制作系统逻辑?

转载 作者:太空宇宙 更新时间:2023-11-03 10:51:13 24 4
gpt4 key购买 nike

各位程序员大家好。我有一个关于在 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;
}

最佳答案

我会通过以下方式做到这一点:

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();
}

关于c# - 游戏制作系统逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21353398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com