基于Django的技能实施

时间:2009-12-03 00:28:30

标签: python django

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

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

class Skill (models.Model):
      name = models.CharField()
      cost = models.PositiveIntegerField()
      blah blah blah

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

1)每项技能都扩展了技能等级和     覆盖特定功能:

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

还有哪些其他选择。我想避免使用纯django方法的脚本方法。

5 个答案:

答案 0 :(得分:5)

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

例如,效果可能是“N霜对当前目标的伤害”。 “Blizzard Bolt”,“Frost Blast”和“Icy Nova”技能可以使用这种效果。

<强> 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)

<强>效果\ spells.py

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

        if target.left <= 0:
            # etc. etc.

答案 1 :(得分:1)

我有点累了(瑞典已经很晚了),所以如果我误解了,我很抱歉,但是第一件事就是extra fields on many-to-many relationships

答案 2 :(得分:1)

我会建立一些继承。

class BaseSkill(models.Model):
    name = models.CharField()
    cost = models.PositiveIntegerField()
    type = models.CharField()
    ....

class FireSkill(BaseSkill):
    burn_time = models.PositiveIntegerField()

    def save():
        self.type = 'fire_skill'
        return super(FireSkill, self).save()

class IceSkill(BaseSkill):
    freeze_time = models.PositiveIntegerField()

    def save():
        self.type = 'ice_skill'
        return super(IceSkill, self).save()

这样做的好处就是你只想列出你需要使用BaseSkill类的玩家技能。如果供应商正在销售技能,您只需要列出BaseSkill类的价格。当您需要技能的更详细属性时,可以轻松地使用该类型来访问它。例如。如果你有:skill = BaseSkill.objects()。get(pk = 1)你可以通过 skill.ice_skill.freeze_time 或更常见的来访问冰技能get_attribute(skill,skill.type).field_name

答案 3 :(得分:1)

当我看到这个时,有两个类:一个用于技能作为一个抽象实例(例如说瑞典语的技能,一个Excel开发技能),然后是一个外国人拥有的实际技能技能的关键。

答案 4 :(得分:0)

您还可以使用单个表并在pickled字段中保存基于对象的内部模型。