在列表中搜索子类属性

现在我正在创建一个任务要求检查器,以查看玩家是否能够完成任务。我现在正在处理的任务类型是“库存中有物品”,如您所见,如果玩家在他/她的库存中有一个或多个指定的物品,它将完成。


现在,我的确切问题是什么?嗯,……物品。


首先。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;

}

并且有一个名为的类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;

}

现在,这就是我创建每个工具的方式:


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

我的播放器对象有和这样的项目列表:


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

它作为它的库存。另外,要向列表中添加新项目,我使用此功能:


player.AddItem(tool, 1);


public void AddItem(Items.Item item, int quantity)

{

    for(int i = 0; i < quantity; i++)

    {

        InventoryItems.Add(item);

    }

}

另一方面,我当前的任务类型“库存中有物品”有一个属性,同时也是一个物品列表:


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

这就是我将项目添加到此列表的方式:


quest.AddRequiredItem(tool, 1);


public void AddRequiredItem(Items.Item item, int quantity)

{

    for(int i = 0; i < quantity; i++)

    {

        RequiredItems.Add(item);

    }

}

为了完成这个任务,玩家必须拥有与RequiredItems列表中相同数量(或更多)的物品。InventoryItems所以,如果这个任务要求玩家四处寻找 3 把劣质木铲,它的清单上应该至少有 3 把劣质木铲。


我的任务是一个名为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;

}


互换的青春
浏览 89回答 1
1回答

慕村225694

编辑:我已经编辑了我的答案以解决通用任务方法的想法。如果您希望任务在多种不同类型上通用,您可能希望在 上实现IsSameorIsEquivalent方法Items.Item,然后继承该方法。您甚至可以覆盖 Object.Equals 方法(这可能是更合适的方式)。class Item{&nbsp; &nbsp; public virtual bool IsSame(Item comp){ return comp.Name == Name; }}class Tool: Item{&nbsp; &nbsp; public override bool IsSame(Item comp)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return base.IsSame(comp) && (comp is Tool) && ((Tool)comp).Material == Material && ((Tool)comp).Classification == Classification;&nbsp; &nbsp; }}然后在您完成迭代中:override public bool Accomplish(){&nbsp; &nbsp; bool questAccomplished = true;&nbsp; &nbsp; foreach (var group in RequiredItems.GroupBy(x => x))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (Application._player.InventoryItems.Count&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x.IsSame(group.Key)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) < group.Count())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; questAccomplished = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return questAccomplished;}
打开App,查看更多内容
随时随地看视频慕课网APP