基于Django的技能实施

awi*_*row 3 python django

我正在使用django开发RPG,并且正在考虑实施部分技能系统的不同选项.

假设我有一个基本技能课程,例如:

class Skill (models.Model):
      name = models.CharField()
      cost = models.PositiveIntegerField()
      blah blah blah
Run Code Online (Sandbox Code Playgroud)

实施特定技能的方法是什么?想到的第一个选择是:

1)每项技能都扩展了Skill类并覆盖了特定的功能:

不确定这在django中是如何工作的.似乎拥有每个技能的数据库表将是矫枉过正.当Skill类有条目时,子类是否可以是抽象的?听起来不对劲.如何使用代理类?

还有什么其他选择.我想避免使用纯django方法的脚本方法.

T. *_*one 5

也许您可以考虑分离技能及其相关效果.更有可能的是,技能最终会产生一种或多种与之相关的效果,并且这种效果可能会被多种技能所使用.

例如,效果可能是"N霜对当前目标的伤害"."暴雪之箭","冰霜爆炸"和"冰冷新星"技能可以使用这种效果.

models.py

class Skill(models.Model):
    name = models.CharField()
    cost = models.PositiveIntegerField()
    effects = models.ManyToManyField(Effect)

class Effect(models.Model):
    description = models.CharField()
    action = models.CharField()

    # Each Django model has a ContentType.  So you could store the contenttypes of
    # the Player, Enemy, and Breakable model for example
    objects_usable_on = models.ManyToManyField(ContentType)

    def do_effect(self, **kwargs):
        // self.action contains the python module to execute
        // for example self.action = 'effects.spells.frost_damage'
        // So when called it would look like this:
        // Effect.do_effect(damage=50, target=target)
        // 'damage=50' gets passed to actions.spells.frost_damage as
        // a keyword argument    

        action = __import__(self.action)
        action(**kwargs)
Run Code Online (Sandbox Code Playgroud)

效果\ spells.py

def frost_damage(**kwargs):
    if 'damage' in kwargs:
        target.life -= kwargs['damage']

        if target.left <= 0:
            # etc. etc.
Run Code Online (Sandbox Code Playgroud)