Source

pygamekit / controller.pxi


class Controller(object):
    
    def __init__(self, function):
        self.f = function
        self.calling_sensor = None
        self.sensors = []
        self.actuators = []
        self.instance = None
        
    def __get__(self, instance, klass):
        # This allows the controller to decorate a method,
        # which at the time of the decoration is still unbound.
        # http://wiki.python.org/moin/PythonDecoratorLibrary#Class_method_decorator_using_instance
        self.instance = instance if instance else klass
        return self
        
    def __call__(self, *args):
        if self.instance is not None:
            ret = self.f(self.instance, *self.sensors)
        else:
            ret = self.f(*self.sensors)
        for a in self.actuators:
            a(ret)
        
    def link(self, *nodes):
        for n in nodes:
            if isinstance(n, Sensor):
                n.link(self)
            else:
                self.actuators.append(n)