Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-based skill implementation

Tags:

python

django

I'm working on a RPG using django and am considering different options for implementing part of the skill system.

Say I have a base skill class ie, something like:

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

What would be some approaches to implementing specific skills? The first option that comes to mind is:

1) Each skill extends Skill class and overrides specific functions:

Not sure how this would work in django. Seems like having a db table for each skill would be overkill. Could the child class be abstract while the Skill class have an entry? Doesn't sound right. How about using a proxy class?

What are some other options. I'd like to avoid a scripted approach for a pure django approach.

like image 295
awithrow Avatar asked Sep 13 '26 05:09

awithrow


2 Answers

Perhaps you might consider separating a skill and it's associated effect. More that likely, skills will end up having one or more effect associated with them, and that effect could potentially be used by multiple skills.

For example, an effect could be "Does N frost damage to current target". That effect could be used by the skills "Blizzard Bolt", "Frost Blast", and "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)

effects\spells.py

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

        if target.left <= 0:
            # etc. etc.
like image 126
T. Stone Avatar answered Sep 15 '26 20:09

T. Stone


I'm kind of tired (late here in Sweden), so I am sorry if i misunderstood, but the first thing that popped into my head was extra fields on many-to-many relationships.

like image 20
rinti Avatar answered Sep 15 '26 18:09

rinti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!