简体   繁体   English

在列表中搜索子类属性

[英]Searching for child class properties in a list

Right now I'm creating a Quest Requirement Checker in order to find if a player is able to accomplish a mission.现在我正在创建一个 Quest Requirement Checker,以查看玩家是否能够完成任务。 The type of mission that I'm working with right now is the "Have An Item In Inventory" which, as you can see, will get done if the player has one or more specified items inside his/her inventory.我现在正在处理的任务类型是“在库存中有一件物品”,如您所见,如果玩家在他/她的库存中有一个或多个指定的物品,就会完成。

Now, what is my exact problem?现在,我的确切问题是什么? Well, the... items.嗯,……物品。

First of all.首先。 Item is a class with the next structure: Item是一个具有下一个结构的类:

public int ID { get; set; }
public string Name { get; set; }
public double Price { get; set; }

public Item(int id, string name, double price)
{
    ID = id;
    Name = name;
    Price = price;
}

And there is a class named Tool which extends the Item class:还有一个名为Tool的类,它扩展了Item类:

public string Material { get; set; }
public string Classification { get; set; }

public Tool
    (
        int id,
        string name,
        double price,
        string material,
        string classification
    ) : base(id, name, price)
{
    Material = material;
    Classification = classification;
}

Now, this is how I create every Tool:现在,这就是我创建每个工具的方式:

Items.Tool tool = new Items.Tool(1, "Shovel", 100, "Wood", "Poor");

My player object has and List of items like this one:我的播放器对象有这样的项目列表:

public List<Items.Item> InventoryItems { get; set; }

And it works as its inventory.它作为它的库存。 Also, to add a new item to the list, I use this function:此外,要向列表中添加新项目,我使用此函数:

player.AddItem(tool, 1);

public void AddItem(Items.Item item, int quantity)
{
    for(int i = 0; i < quantity; i++)
    {
        InventoryItems.Add(item);
    }
}

On the other hand, my current quest type "Has Items In Inventory" has a property that is, at the same time, a list of items:另一方面,我当前的任务类型“库存中有物品”有一个属性,同时也是一个物品列表:

public List<Items.Item> RequiredItems { get; set; }

And this is how I add items to this list:这就是我向此列表添加项目的方式:

quest.AddRequiredItem(tool, 1);

public void AddRequiredItem(Items.Item item, int quantity)
{
    for(int i = 0; i < quantity; i++)
    {
        RequiredItems.Add(item);
    }
}

In order to fulfill this quest, the player must have the same amount (or more) of items that the RequiredItems list have.为了完成这个任务,玩家必须拥有与RequiredItems列表相同数量(或更多)的物品。 So, if this quest ask the player to look around for 3 Poor Wooden Shovels, it should have at least 3 Poor Wooden Shovels on its InventoryItems list.所以,如果这个任务要求玩家四处寻找 3 个劣质木铲,它的InventoryItems列表中应该至少有 3 个劣质木铲。

My quest, which is a class named HaveItemsInInventory implements the next function in order to evaluate that condition:我的任务是一个名为HaveItemsInInventory的类,它实现了下一个函数以评估该条件:

override public bool Accomplish()
{
    bool questAccomplished = true;
    foreach (var group in RequiredItems.GroupBy(x => x))
    {
        if (Application._player.InventoryItems.Count
            (
                x =>
                (
                    x.Name == group.Key.Name && 
                    x.Material == group.Key.Material &&
                    x.Classification == group.Key.Classification
                )
            ) < group.Count())
        {
            questAccomplished = false;
            break;
        }
    }

    return questAccomplished;
}

And this where all my problems appear.这就是我所有问题出现的地方。 This two lines or code are wrong:这两行或代码是错误的:

x.Material == group.Key.Material &&
x.Classification == group.Key.Classification

Because there's no such thing as a Material or Classification in a Item .因为在Item没有MaterialClassification之类的东西。

What i want to do is to implement different types of evaluations.我想做的是实施不同类型的评估。

  • If an quest ask for a Glass of Water, I should look for properties living inside my Beberage class.如果任务要求一杯水,我应该寻找住在我的Beberage班级内的财产。

  • If the quest ask for just a Sword.如果任务只要求一把剑。 I should look for a item with the Name "Sword" in its inventory.我应该在其库存中寻找名称为“Sword”的物品。

  • If the quest ask for a Diamond Legendary Sword, well... You get my point.如果任务要求钻石传奇剑,那么......你明白我的意思。

Is there a way to look for these extended classes properties within my system?有没有办法在我的系统中查找这些扩展类属性? I can't find a way to do so.我找不到这样做的方法。

PD: Sorry for my bad english, not a native speaker. PD:对不起,我的英语不好,不是母语人士。

EDIT: I've edited my answer to address the idea of a generic task method.编辑:我已经编辑了我的答案以解决通用任务方法的想法。

If you want the task to be generic across multiple different types, you probably want to implement an IsSame or IsEquivalent method on Items.Item , and then inherit that method.如果您希望任务能够跨多种不同类型的通用的,你可能想实现一个IsSameIsEquivalent的方法Items.Item ,然后继承该方法。 You could even override the Object.Equals method (and that might be a more appropriate way).您甚至可以覆盖 Object.Equals 方法(这可能是更合适的方法)。

class Item
{
    public virtual bool IsSame(Item comp){ return comp.Name == Name; }
}

class Tool: Item
{
    public override bool IsSame(Item comp)
    {
        return base.IsSame(comp) && (comp is Tool) && ((Tool)comp).Material == Material && ((Tool)comp).Classification == Classification;
    }
}

Then in your accomplish iteration:然后在你的完成迭代中:

override public bool Accomplish()
{
    bool questAccomplished = true;
    foreach (var group in RequiredItems.GroupBy(x => x))
    {
        if (Application._player.InventoryItems.Count
            (
                x =>
                (
                   x.IsSame(group.Key)
                )
            ) < group.Count())
        {
            questAccomplished = false;
            break;
        }
    }

    return questAccomplished;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM