简体   繁体   English

c# 如何使用不同的签名(?)

[英]c# how to approach Abstract Factory with different signatures (?)

I am trying to figure out how to create different baddies in a game.我想弄清楚如何在游戏中创建不同的坏人。

let's say I have 2 factions.假设我有 2 个派系。 Paladins and Thieves, and 3 warrior types for each faction圣骑士和盗贼,以及每个派系的 3 种战士类型

I have an abstractFactory that generates the equipment they are carrying, but I'm not sure how to make it work.我有一个抽象工厂来生成他们携带的设备,但我不确定如何让它工作。 Here is what I have in place这是我所拥有的

enum PaladinWarrior
{
    Squire,
    Novice,
    Knight

}

/// <summary>
/// abstract weapon factory
/// </summary>
abstract class EquipmentFactory
{
    public abstract IWeapon GetWeapon(Enum warriorType);
    /// maybe some other methods like armor and hp
}

/// <summary>
/// concrete weapon factory
/// </summary>
class PaladinWeaponFactory : EquipmentFactory
{
    public override IWeapon GetWeapon(PaladinWarrior warriorType)
    {
        switch (warriorType)
        {
            case PaladinWarrior.Squire:
                return new ShortSword();
            case PaladinWarrior.Novice:
                return new LongSword();
            case PaladinWarrior.Knight:
                return new GreatSword();
            default:
                break;
        }
        
    }
}

/// another factory for the thieves, etc.

The problem i am facing is that by specifying the Enum type, it no longer matches the abstract factory, and would not be straight forward to make it work.我面临的问题是,通过指定 Enum 类型,它不再匹配抽象工厂,并且不会直接使其工作。 in other words, the abstract factory's getWeapon doesnt' have the same signature as the Palading one, and i don't know how to approach that.换句话说,抽象工厂的 getWeapon 与 Palading 的签名不同,我不知道如何处理。

I am wondering what pattern/workflow would be better for this?我想知道哪种模式/工作流程会更好? considering I have multiple factions and each faction needs different warriors (With different equipment, of course).考虑到我有多个派系,每个派系都需要不同的战士(当然,装备不同)。

Edit: I wanted the Factory so i could Create a warrior like this:编辑:我想要工厂,所以我可以像这样创建一个战士:


Warrior goodGuy = new Warrior(PaladinWarrior.Squire)
Warrior badGuy = new Warrior(ThievesWarrior.BBEV) 

and have the Warrior class be able to create the armor and equipment.并让战士类能够制造盔甲和装备。 I don't know what other pattern could accomplish that other than a factory.我不知道除了工厂之外还有什么其他模式可以做到这一点。

Since the question talks about adding warrior types in a game I will answer from a game development perspective由于问题是在游戏中添加战士类型,我将从游戏开发的角度来回答

The approach with different enums and different factories creating the warriors is overly complicated, it would be much simpler to merge the enums and have only a single factory that creates the entire Warrior object.使用不同的枚举和不同的工厂来创建战士的方法过于复杂,合并枚举并且只有一个工厂来创建整个Warrior对象会简单得多。

However, in "proper game development" even that is not a good idea, because you want to give the choice of adding and modifying these warrior types to designers, who do not want to (and should not have to) modify code.然而,在“适当的游戏开发”中,即使这样也不是一个好主意,因为您希望将添加和修改这些战士类型的选择权交给不想(也不应该)修改代码的设计师。

Instead you want to make your approach data-driven, which means that all the data necessary to create a Warrior is stored externally in a file or database, using a pre-defined format (which can be a common one like JSON or something completely custom).相反,您想让您的方法数据驱动,这意味着创建Warrior所需的所有数据都使用预定义格式(可以是常见的格式,如 JSON 或完全自定义的格式)存储在外部文件或数据库中)。 Then all your code has to do is read in that data and create the objects based on it.然后您的代码所要做的就是读入该数据并基于它创建对象。

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

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