c# - 角色扮演游戏中临时将玩家变成动物的设计考虑

标签 c# design-patterns decorator

我正在开发一个角色扮演游戏来获得乐趣并练习设计模式。我希望玩家能够将自己变成不同的动物。例如,德鲁伊可能能够变形为猎豹。现在我正计划使用装饰器模式来做到这一点,但我的问题是 - 我如何做到这一点,以便当德鲁伊处于猎豹形态时,他们只能获得猎豹的技能?换句话说,他们不应该能够获得正常的德鲁伊技能。

使用装饰器模式,看起来即使在猎豹形态下,我的德鲁伊也能使用他们正常的德鲁伊技能。

class Druid : Character
{
   // many cool druid skills and spells
   void LightHeal(Character target) { }
}

abstract class CharacterDecorator : Character
{
    Character DecoratedCharacter;
}

class CheetahForm : CharacterDecorator
{
    Character DecoratedCharacter;
    public CheetahForm(Character decoratedCharacter)
    {
       DecoratedCharacter= decoratedCharacter;
    }

    // many cool cheetah related skills
    void CheetahRun()
    {
       // let player move very fast
    }
}

现在使用类

Druid myDruid = new Druid();
myDruid.LightHeal(myDruid); // casting light heal here is fine
myDruid = new CheetahForm(myDruid);
myDruid.LightHeal(myDruid); // casting here should not be allowed

Hmmmm...现在我想起来了,myDruid 是否无法使用 Druid 类法术/技能,除非该类被贬低?但即使是这样,是否有更好的方法来确保此时 myDruid 被所有 Druid 相关的法术/技能锁定,直到它被施放回Druid(因为目前它在 CheetahForm 中)

最佳答案

你不应该将你的法术和技能作为类里面的方法。它们应该是可通过角色(或可能是生物)界面访问的法术和技能对象的集合。然后您的 CheetaForm 可以重写 getSkills getSpells 方法以返回适当技能的新修改集合。

关于c# - 角色扮演游戏中临时将玩家变成动物的设计考虑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2220448/

相关文章:

C# % 抛出 DivideByZeroException

c++ - 如何在不破坏 Decorator 模式的情况下精简 Fat Interface?

java - 简单的java消息调度系统

c++ - 如何在 C++17 中制作模板包装器/装饰器

python - 我怎样才能重写这个 fixture 调用,这样它就不会被直接调用?

c# - MVC 图表仅显示文本流

c# - 如果 x of y 值相等

c# - C# 中的 WebBrowsing - 库、工具等 - 是否类似于 Perl 中的 Mechanize?

list - Haskell 列表中的模式匹配失败

c# - 单例模式 - 默认属性