Exemplo n.º 1
0
    def initFromMap(self, application, maplayer, world):
        """Initialize the world + visual from the fife map layer."""
        print "* Initializing movement grid..."
        self.application = application
        self.maplayer = maplayer
        self.world = world

        print "  * Creating wall cache..."
        # create a dictionary that maps coordinates to walls
        for instance in self.maplayer.getInstances():
            coords = instance.getLocation().getLayerCoordinates()
            z_size = gridhelper.getObjectZSize(instance.getObject().getId())
            new_wall = Wall(instance.getLocation().getLayerCoordinates(),
                            z_size,
                            instance.getObject().getId())
            new_wall.createVisual(self.application, instance)
            self.world.walls.append(new_wall)
            if z_size > 0:
                for z_offset in xrange(z_size):
                    self.world.wall_map[(coords.x, coords.y,
                                         coords.z + z_offset)] = new_wall
            elif z_size < 0:
                for z_offset in xrange(0 - z_size):
                    self.world.wall_map[(coords.x, coords.y,
                                         coords.z - z_offset - 1)] = new_wall

        print "  * Creating walkable tiles..."
        # add tiles
        for instance in self.maplayer.getInstances():
            if instance.getObject().getId() in [
                    "block_gray", "water", "block_grass"
            ]:
                tile = self.world.addTile(
                    instance.getLocation().getLayerCoordinates(),
                    instance.getObject().getId())
                if tile:
                    tile.createVisual(self.application)
        # add tiles that should be inaccessible to the to_remove list
        to_remove = []
        for tile in self.world.tiles:
            column = self.world.getTilesByLocation(tile.coords)
            last_z = 50
            for column_tile in column:
                if column_tile.coords.z + 4 >= last_z:
                    to_remove.append(column_tile)
                last_z = column_tile.coords.z
        # remove tiles in the to_remove list
        for tile in to_remove:
            self.world.removeTile(tile)