嵌套策略设计模式

S.A*_*hid 0 c# design-patterns unity-game-engine

假设我们有一个玩家控制一个名为 Player1 的角色。为了在运行时启用切换武器,我有以下代码:

public interface IWeapon
{
    void Fire();
}

public class Player1Weapon1 : IWeapon
{
    ...
    void Fire()
    {
        //do actions of Weapon1
    }
    ...
}

public class Player1Weapon2 : IWeapon
{
    ...
    void Fire()
    {
        //do actions of Weapon2
    }
    ...
}

public class Player1
{
    IWeapon weapon;
    Player1Weapon1 w1;
    Player1Weapon2 w2;

    public Player1() 
    { 
        w1 = new  Player1Weapon1(this);
        w2 = new  Player1Weapon2(this);
        SetWeapon(w1);
    }
    void Update()
    {
        if(SWITCH_BTN_HELD)
        {
            if(weapon.equals(w1))  SetWeapon(w2);
            if(weapon.equals(w2))  SetWeapon(w1);
        }
        if(FIRE_BTN_HELD)
            weapon.Fire();
    }
    void SetWeapon(w)
    {
        weapon = w;
    }
}
Run Code Online (Sandbox Code Playgroud)

这完美地工作。

但是现在玩家可以选择另一个名为 Player2 的角色。请注意,玩家 2 的武器与玩家 1 不同。所以我们可以像这样添加类:

public class Player2Weapon1 : IWeapon
{
    ...
    void Fire()
    {
        //do actions of Weapon1
    }
    ...
}
public class Player2Weapon2 : IWeapon
{
    ...
    void Fire()
    {
        //do actions of Weapon2
    }
    ...
}

public class Player2
{
    IWeapon weapon;
    Player2Weapon1 w1;
    Player2Weapon2 w2;

    public Player2() 
    { 
        w1 = new  Player2Weapon1(this);
        w2 =new  Player2Weapon2(this);
        SetWeapon(w1);
    }
    void Update()
    {
        if(SWITCH_BTN_HELD)
        {
        if(weapon.equals(w1))  SetWeapon(w2);
        if(weapon.equals(w2))  SetWeapon(w1);
        }
        if(FIRE_BTN_HELD)
        weapon.Fire();
    }
    void SetWeapon(w)
    {
        weapon=w;
    }
}
Run Code Online (Sandbox Code Playgroud)

它会再次工作,但它非常紧张,如果玩家想玩 Player3,我应该在项目中添加更多类。

我想知道如何为 flayers 制作策略模式,如下所示:

interface IPlayer()
{

}
Run Code Online (Sandbox Code Playgroud)

不知道IPlayer会放哪些方法?如何创建嵌套的策略设计模式?

Mik*_*ala 5

不确定我是否明白您的要求,或者您不知道如何/问什么。策略模式不是您在这里最需要的。我试着给我的两分钱。

首先,我不认为在你的播放器中有具体的类是一个好主意,比如Player1Weapon. 它应该只包含IWeapons。这样你就不需要专门定义你的玩家使用哪些武器,也不需要为每个变体创建新的玩家类。

考虑以下。你IWeaponsSomeNamespace命名空间中有这些。

public interface IWeapon
{
    void Fire();
}

public class Shotgun : IWeapon
{
    public void Fire()
    {
        Console.WriteLine("Shotgun goes boom");
    }
}

public class Knife : IWeapon
{
    public void Fire()
    {
        Console.WriteLine("Stabbed teh sucker");
    }
}

public class NuclearBomb : IWeapon
{
    public void Fire()
    {
        Console.WriteLine("Game over for everyone!!1");
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您的Player类可能如下所示。只需添加您可能需要的任何抽象级别,在这里我只是假设您没有。

public class Player
{
    private IWeapon _wielded;

    public Player(string name)
        :this(name, null, null)
    {}

    public Player(string name, IWeapon primary, IWeapon secondary)
    {
        Name = name;
        Primary = _wielded = primary;
        Secondary = secondary;

        Console.WriteLine(string.Format("Player '{0}' spawned", Name));
    }

    public void Switch()
    {
        _wielded = _wielded != Primary ? Primary : Secondary;
    }

    public void Fire()
    {
        if (_wielded != null)
            _wielded.Fire();
    }

    public string Name { get; set; }

    public IWeapon Primary { get; set; }

    public IWeapon Secondary { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

要创建“任何玩家”,您可以使用一个简单的工厂来“生成”它们,并将所需的属性作为参数。

public class PlayerFactory
{
    // key = player name, value = weapons
    public Player Create(KeyValuePair<string, string[]> args)
    {
        var primary = Activator.CreateInstance(Type.GetType(args.Value[0])) as IWeapon;
        var secondary = Activator.CreateInstance(Type.GetType(args.Value[1])) as IWeapon;

        var player = new Player(args.Key, primary, secondary);
        return player;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在如果你运行下面的“初始化”......

// this would come from config file or similar
var config = new List<KeyValuePair<string, string[]>>
    {
        new KeyValuePair<string,string[]>(
            "Player1", new[] { "SomeNamespace.Shotgun", "SomeNamespace.Knife" }),
        new KeyValuePair<string,string[]>(
            "Player2", new[] { "SomeNamespace.NuclearBomb", "SomeNamespace.Knife" })
    };

var factory = new PlayerFactory();
foreach (var entry in config)
{
    var player = factory.Create(entry);
    player.Fire();
    player.Switch();
    player.Fire();
}
Run Code Online (Sandbox Code Playgroud)

...你最终得到以下控制台日志

玩家 'Player1' 产生了
霰弹枪,被
刺伤了吸盘

玩家 'Player2'
为每个人生成了游戏!!1
刺伤了傻瓜