C#中的RPG体验系统?

时间:2017-10-24 03:34:27

标签: c#

是否有一种体面的方式来实施具有以下要求的体验系统:

  • 能够处理不同类型的经验(角色,武器,技能/法术等)

  • 足够灵活,允许添加不同类型的体验类型

  • 能够为获得的等级授予适当的奖励/奖金

是否有适合这种情况的设计模式,所以我不必去重新发明轮子?或者我应该继续尝试从头开始提出一些来自y'所有可能的建议吗?

编辑:我已经想过使用一个名为ILevelable的界面,并将其作为构建课程的基础,但我无法帮助,但我觉得我必须拥有针对特定情况的更具体的接口 - 或者一个ILevelable接口是否足以满足其所有要求?

interface ILevelable
    {
        int Level { get; set; }
        int CurrentExp { get; set; }
        int ExpToNextLevel { get; set; }

        void GainExp(int xp); // This may return a bool at some point - depends on design
        int CalculateNextLevel(); // Will be used to determine what the next level-up exp amount will be
        int LevelUp(); // Returns the current new level - this could be where rewards are granted?
    }

1 个答案:

答案 0 :(得分:0)

不清楚“handle”的含义,但我认为Dictionary<enum, delegate>方法,其中enum是体验类型的索引,而{delegate 1}}负责处理每种类型,应该满足这些要求。

“C#from memory”编程语言中的示例,应该被认为是类似C#的伪代码(仅仅因为我没有打开IDE并且现在不打扰):< / p>

//XP types
enum ExperienceType {
    Character, Weapon, Skill, Spell, Programming
}

//Maps types to handlings...
//You can map different types to different behaviors (requirement #1)
//You can map as many types and/to as many behaviors as you want (requirement #2)
//You can handle rewards in the delegates (requirement #3)
Dictionary<ExperienceType, Action<double>> handlers = /*...*/;

//Handle XP gained according to type; handler takes care of different behaviour
public void EarnXP(double amount, ExperienceType type){
    //handlers[type] gets delegate from dictionary
    //(amount) invokes the acquired delegate using amount as parameter
    handlers[type](amount);

    //delegate is responsible for handling what to do...
    //So you can effectively have a different handler (method) for each XP type
    //While the addition of more XP is a "simple" EarnXP(amount, type)
}