Ejemplo n.º 1
0
    def insert_objects(self):
        '''Insert any objects you want to be present in the zone into the
        database in this call.

        This gets called exactly once per database. If you change something here
        and want it to appear in the zone's database, you will need to clear the
        database first.

        Deleting the "Loading Complete" object will only cause duplicates.
        Do not do this.
        '''

        # Place 10 chickens randomly:
        for i in xrange(10):
            obj = ScriptedObject()
            obj.name = "Chicken #%d" % i
            obj.resource = 'chicken'
            obj.loc = IntVector(x=randloc(), y=randloc(), z=randloc())
            obj.rot = FloatVector(x=randrot(), y=randrot(), z=randrot())
            obj.scale = FloatVector(x=randscale(), y=randscale(), z=randscale())
            obj.vel = FloatVector(x=0, y=0, z=0)
            obj.states.extend(['alive', 'whole', 'clickable'])
            obj.scripts = ['games.objects.chicken']
            obj.save()

        print [o.name for o in Object.objects()]
Ejemplo n.º 2
0
 def create(cls):
     obj = ScriptedObject()
     obj.name = "Linnea"
     obj.resource = 'girl'
     obj.loc = IntVector(x=0, y=0, z=0)
     obj.rot = FloatVector(x=0, y=0, z=0)
     obj.scale = FloatVector(x=1, y=1, z=1)
     obj.vel = FloatVector(x=0, y=0, z=0)
     obj.states.extend(['alive', 'whole', 'clickable'])
     obj.scripts = ['games.objects.chatbot']
     obj.save()
     return obj
Ejemplo n.º 3
0
 def create(cls, number=0):
     obj = ScriptedObject()
     obj.name = "Chicken #%d" % number
     obj.resource = 'chicken'
     obj.loc = IntVector(x=randloc(), y=randloc(), z=randloc())
     obj.rot = FloatVector(x=randrot(), y=randrot(), z=randrot())
     obj.scale = FloatVector(x=randscale(), y=randscale(), z=randscale())
     obj.vel = FloatVector(x=0, y=0, z=0)
     obj.states.extend(['alive', 'whole', 'clickable'])
     obj.scripts = ['games.objects.chicken']
     obj.save()
     return obj
Ejemplo n.º 4
0
    def activate_object(self, object_id, character):
        # Instantiate the scripted object and call its activate thing.
        retval = []
        for o in ScriptedObject.objects(id=object_id):
            # TODO: Break this block into a method.
            for script in o.scripts:
                scriptclass = script.split('.')[-1]
                module = __import__(script, globals(), locals(), [scriptclass], -1)
                # For each entry in the script's dir()
                for key in dir(module):
                    C = getattr(module, key)
                    try:
                        if not issubclass(C, Script):
                            # If it isn't a subclass of Script, skip it.
                            continue
                    except TypeError:
                        # If C isn't a class at all, skip it.
                        continue

                    # Finally activate the script.
                    script_val = C(o).activate(character)
                    if script_val:
                        # Only return it if the script actually returns something.
                        # So clients should only react to activating scripts that
                        # return something meaningful.
                        retval.append(script_val)

        return retval
Ejemplo n.º 5
0
    def load_scripts(self):
        '''(Re)Load scripts for objects in this zone.'''
        self.scripts = {}

        # Query DB for a list of all objects' script names,
        #   ordered according to proximity to players
        logger.info(ScriptedObject.objects)
        for o in ScriptedObject.objects(scripts__exists=True):
            logger.info("Scripted Object: {0}".format(o.name))
            # Store list of script names in self

            # For each script name in the list:
            for script in o.scripts:
                logger.info("Importing %s" % script)
                if script not in self.scripts:
                    self.scripts[script] = []

                # Import those by name via __import__
                scriptclass = script.split('.')[-1]
                module = __import__(script, globals(), locals(), [scriptclass], -1)
                # For each entry in the script's dir()
                for key in dir(module):
                    C = getattr(module, key)

                    # No sense in instantiating the default Script instance.
                    if C == Script:
                        continue

                    try:
                        # Does this object have the attributes that scripts need?
                        if not all((C.tick, C.activate, C.create)):
                            continue
                    except AttributeError:
                        continue

                    # Store object instance in a list.
                    self.scripts[script].append(C(mongo_engine_object=o))
        return self.scripts