如何存储和处理游戏中的食谱 [英] How to store and handle ingame recipes

查看:31
本文介绍了如何存储和处理游戏中的食谱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建类似泰拉瑞亚的游戏,但在存储食谱的位置时遇到了问题..

I am building a Terraria like game and I am having a problem with where to store the recipes..

基本上我的游戏应该做的是检查玩家库存(这是一个 id 数组),如果所有物品都在玩家库存中,则逐个检查配方.

basically what my game should do is go through the player inventory (which is an array of id's) and check recipe by recipe if all the item's are in the player inventory.

我不知道如何存储食谱以及如何处理它们,我虽然使用数组,但数组的大小因项目而异,我也想使用列表,但写了很多,我想要一个干净"的代码.

I dont know how to store the recipes and how to handle them, i though of using array but the size of the array vary from item to item, I though of list too but it is a lot of writing and I want a "clean" code.

我应该用什么来存储我的食谱?

what should i use to store my recipes?

如果您建议我使用数组,我是否应该将其设为静态并声明每个食谱和我的Crafting"课程?

and if you suggest me to use array, should i make it static and declare every recipe and my "Crafting" class?

谢谢.

(配方应该是id和每个id的数量)

推荐答案

我从来没有玩过泰拉瑞亚,但听起来像是一个非常简单的 LINQ 查询:

I've never played Terraria, but it sounds like a pretty simple LINQ query:

如果您的配方对象包含一个 InventoryItem 列表:

If your recipe object contains a List of InventoryItem:

struct InventoryItem
{
   int itemId;
   int itemCount;
}

class Recipe
{
   String name;
   List<InventoryItem> RequiredItems { get; set; }
}

而你的库存是一个结构相同的列表,那么它只是:

And your inventory is a list of the same structure, then its just:

bool canUseRecipe = recipe.RequiredItems.All(i => 
   {
      InventoryItem itemInInventory = Inventory.FirstOrDefault(x => x.itemId == i.itemId);
      return itemInInventory == null ? false : itemInInventory.itemCount >= i.itemCount;
   });

可能有一种方法可以将其折叠成一个衬垫,但这可能更清楚!

There might be a way to collapse that into a one liner, but this is probably more clear!

您也可以将其拆分为不同的功能:

You could also seperate it out into a different function:

bool canUseRecipe = recipe.RequiredItems.All(i => SufficientItemsInInventory(i));
//Or
bool canUseRecipe = recipe.RequiredItems.All(SufficientItemsInInventory);
...

private bool SufficentItemsInInventory(InventoryItem item)
{
   InventoryItem itemInInventory = Inventory.FirstOrDefault(i => i.itemId == item.itemId);
   return itemInInventory == null ? false : itemInInventory.itemCount >= i.itemCount;
});

这篇关于如何存储和处理游戏中的食谱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆