C# 纸牌游戏动作结构中的多个委托分配和调用

Multiple delegate assignments and call in a C# card game action sctruture

我正在使用 C# 开发纸牌游戏,但我不知道如何执行以下操作:

我有我的卡片 class,上面有一张卡片可以执行的攻击动作列表。

这些攻击是自定义的 class 命名 Act(我在示例中将其称为 Rules)。

当我加载卡片时,我调用了我创建的初始化卡片列表以及每张卡片的攻击列表的 Inicialize 方法。

我希望能够将我的攻击表达为带有参数的多重方法调用,仅当我调用该攻击执行时才会调用这些参数。

例如,类似的东西

ActionList = new List<CardRule>();
        ActionList.Add(new CardRule()
        {
            Description = "Cause 20 damage, then causes burn status.",
            Rules = Damage(20).CauseStatus(Status.Burn);
        });

我想将操作(我称之为规则)定义为多方法调用,传递参数,

并调用 Rules.Execute() 来执行所有方法调用等。

我知道它与委托有关,但我不知道如何使用预定义参数调用多个方法。

提前谢谢你,抱歉英语不好,我也是 Stack Overflow 的新手..

此致,

您要找的实际上不是委托,而是 class 跟踪需要完成的事情。这是流式 API 经常使用的模式,但为您的示例创建一些内容相对简单。

对于您的示例,您可能有一个 IActionConfiguration 接口,例如:

public interface IActionConfiguration
{
    void PerformAction(MyTarget target);
}

现在,您需要几种不同的实现方式。比如代表伤害的一个:

class DamageActionConfiguration : IActionConfiguration
{
    private int m_damageStrength;

    public DamageActionConfiguration(int strength)
    {
        m_damageStrength = strength;
    }

    public void PerformAction(MyTarget target)
    {
        target.Health -= strength;
    }
}

还有一个代表状态效果:

class CauseStatusActionConfiguration : IActionConfiguration
{
    private Status m_status;

    public CauseStatusActionConfiguration(Status status)
    {
        m_status = status;
    }

    public void PerformAction(MyTarget target)
    {
        target.StatusEffects.Add(m_status);
    }
}

现在,您还将获得一个表示多个操作的实现。

class CompositeActionConfiguration : IActionConfiguration
{
    private IActionConfiguration m_firstWrappedConfiguration;
    private IActionConfiguration m_secondWrappedConfiguration;

    public CompositeActionConfiguration(IActionConfiguration first, IActionConfiguration second)
    {
        m_firstWrappedConfiguration = first;
        m_secondWrappedConfiguration = second;
    }

    public void PerformAction(MyTarget target)
    {
        m_firstWrappedConfiguration.PerformAction();
        m_secondWrappedConfiguration.PerformAction();
    }
}

这非常简单,但对于我们的示例来说已经足够好了。现在你有一个 class (CompositeActionConfiguration) 可以表示多个动作 - 你所需要的只是让你轻松地将它们链接在一起的魔法。

public static class ActionRules
{

    //First, you want a few static methods which can start a chain:
    public static IActionConfiguration Damage(int strength)
    {
        return new DamageActionConfiguration(strength);
    }

    public static IActionConfiguration Status(Status status)
    {
        return new CauseStatusActionConfiguration(status);
    }

    // Next, some extension methods which allow you to chain things together
    // This is really the glue that makes this whole solution easy to use
    public static IActionConfiguration WithDamage(this IActionConfiguration source, int strength)
    {
        return new CompositeActionConfiguration(source, Damage(strength));
    }

    public static IActionConfiguration WithStatus(this IActionConfiguration source, Status status)
    {
        return new CompositeActionConfiguration(source, Status(status));
    }
}

就是这样。这为您提供了一个或多或少可以满足您需求的解决方案。您可以将规则 属性 定义为 IActionConfiguration,然后您可以像这样使用它:

var card = new CardRule()
{
    Description = "Cause 20 damage, then causes burn status.",
    Rules = ActionRules.Damage(20).WithStatus(Status.Burn);
};

card.PerformAction(target);