DNA
7/2/2018 - 4:01 AM

Stats class for Renpy

Stats class for Renpy

class Stat(object):
    class Delta(object):
        def __init__(self, parent):
            self.parent = parent
            self.value = parent.value

        def __repr__(self):
            return '<Stat.Delta value={}>'.format(self.calculate())

        def __str__(self):
            return str(self.calculate())

        def calculate(self):
            delta = self.parent.value - self.value
            if self.parent.dir == 'down': delta *= -1
            return delta

        def is_positive(self): return self.calculate > 0
        def is_negative(self): return self.calculate < 0
        def is_zero(self): return self.calculate == 0
        def reset(self): self.value = self.parent.value

    def __init__(self, name, value, max, min):
        self.name = name
        self.value = value
        self.max = max
        self.min = min
        self.dir = dir
        self.delta = self.Delta(self)

    def __repr__(self):
        return '<Stat "{}" value={} delta={}>'.format(self.name, self.value, self.delta)

    def __str__(self):
        return str(self.value)

    def __add__(self, value):
        self.value += value
        return self.value

    def __sub__(self, value):
        self.value -= value
        return self.value

    def __eq__(self, other): return self.value == other
    def __ne__(self, other): return self.value != other
    def __lt__(self, other): return self.value < other
    def __le__(self, other): return self.value <= other
    def __gt__(self, other): return self.value > other
    def __ge__(self, other): return self.value >= other

strength = Stat('Strength', 20, 0, 100)

print repr(strength)
print repr(strength.delta)
print strength + 6
print strength.delta
print repr(strength)
print strength - 8
print strength.delta
print repr(strength)
strength.delta.reset()
print repr(strength)