Example #1
0
    def load_map(self, map_name):
        """Returns map data as a dictionary to be used for map changing and preloading
        """
        map_data = {}
        map_data["data"] = map.Map(map_name)
        map_data["events"] = map_data["data"].events
        map_data["inits"] = map_data["data"].inits
        map_data["interacts"] = map_data["data"].interacts
        map_data["tiles"], map_data["collision_map"], map_data["collision_lines_map"], map_data["map_size"] = \
            map_data["data"].loadfile(self.tile_size)

        # Scale the loaded tiles if enabled
        if prepare.CONFIG.scaling == "1":
            # Loop through each row in the map. Each row is a list of
            # Tile objects in that row.
            for y_pos, row in enumerate(map_data["tiles"]):
                # Now loop through each tile in the row and scale it accordingly.
                for x_pos, column in enumerate(row):
                    if column:
                        for layer_pos, tile in enumerate(column):
                            tile['surface'] = tools.scale_tile(
                                tile['surface'], self.tile_size)
                            map_data["tiles"][y_pos][x_pos][layer_pos] = tile

        return map_data
Example #2
0
    def change_map(self, map_name):
        # Set the currently loaded map. This is needed because the event
        # engine loads event conditions and event actions from the currently
        # loaded map. If we change maps, we need to update this.

        # reset controls and stop moving to prevent player from
        # moving after the teleport and being out of control
        self.game.reset_controls()
        try:
            self.player1.direction['up'] = False
            self.player1.direction['down'] = False
            self.player1.direction['left'] = False
            self.player1.direction['right'] = False
            self.player1.moving = False
        except AttributeError:  # will be raised if this is first map change
            pass

        self.current_map = map.Map(map_name)
        self.event_engine.current_map = map.Map(map_name)

        self.tiles, self.collision_map, self.collision_lines_map, self.map_size = \
            self.current_map.loadfile(self.tile_size)

        # Get the events actions and conditions from the current map
        self.game.events = self.current_map.events

        # Clear out any existing NPCs
        self.npcs = []
        self.npcs_off_map = []

        # Scale the loaded tiles if enabled
        if prepare.CONFIG.scaling == "1":
            # Loop through each row in the map. Each row is a list of
            # Tile objects in that row.
            for y_pos, row in enumerate(self.tiles):
                # Now loop through each tile in the row and scale it accordingly.
                for x_pos, column in enumerate(row):
                    if column:
                        for layer_pos, tile in enumerate(column):
                            tile['surface'] = tools.scale_tile(tile['surface'], self.tile_size)
                            self.tiles[y_pos][x_pos][layer_pos] = tile
Example #3
0
    def teleport(self, game, action):
        """Teleport the player to a particular map and coordinates

        :param game: The main game object that contains all the game's variables.
        :param action: The action (tuple) retrieved from the database that contains the action's
            parameters

        :type game: core.control.Control
        :type action: Tuple

        :rtype: None
        :returns: None

        Valid Parameters: map_name,coordinate_x,coordinate_y

        **Examples:**

        >>> action.__dict__
        {
            "type": "teleport",
            "parameters": [
                "map1.tmx",
                "5",
                "5"
            ]
        }

        """
        # Get the player object from the game.
        player = game.player1
        world = game.current_state

        # Get the teleport parameters for the position x,y and the map to load.
        mapname = str(action.parameters[0])
        position_x = int(action.parameters[1])
        position_y = int(action.parameters[2])

        # If we're doing a screen transition with this teleport, set the map name that we'll
        # load during the apex of the transition.
        if world.start_transition:
            world.delayed_mapname = mapname

        # Check to see if we're also performing a transition. If we are, wait to perform the
        # teleport at the apex of the transition
        if world.start_transition:
            world.delayed_teleport = True
            # Set the global_x/y variables based on the player's pixel position and the tile size.
            world.delayed_x = player.position[0] - (position_x *
                                                    player.tile_size[0])
            world.delayed_y = player.position[1] - (
                position_y * player.tile_size[1]) + player.tile_size[1]
        # If we're not doing a transition, then just do the teleport
        else:
            # Set the global_x/y variables based on the player's pixel position and the tile size.
            world.global_x = player.position[0] - (position_x *
                                                   player.tile_size[0])
            world.global_y = player.position[1] - (
                position_y * player.tile_size[1]) + player.tile_size[1]

            ### THIS NEEDS TO BE MOVED IN ITS OWN FUNCTION AND IS DUPLICATED IN THE World STATE ###
            from core.components.map import Map
            if prepare.BASEDIR + "resources/maps/" + mapname != world.current_map.filename:
                world.current_map = Map(prepare.BASEDIR + "resources/maps/" +
                                        mapname)
                world.event_engine.current_map = Map(prepare.BASEDIR +
                                                     "resources/maps/" +
                                                     mapname)
                world.tiles, world.collision_map, world.collision_lines_map, world.map_size = world.current_map.loadfile(
                    world.tile_size)
                world.game.events = world.current_map.events

                # Clear out any existing NPCs
                world.npcs = []

                # Scale the loaded tiles if enabled
                if world.scale > 1:
                    x_pos = 0  # Here we need to keep track of the x index of the list
                    # Loop through each row in the map. Each row is a list of
                    # Tile objects in that row.
                    for row in world.tiles:
                        y_pos = 0  # Here we need to keep track of the y index of the list within the row
                        # Now loop through each tile in the row and scale it
                        # accordingly.
                        for column in row:
                            if column:
                                layer_pos = 0
                                for tile in column:
                                    tile["surface"] = tools.scale_tile(
                                        tile["surface"], player.tile_size)
                                    world.tiles[x_pos][y_pos][layer_pos] = tile
                                    layer_pos += 1
                            y_pos += 1
                        x_pos += 1

        # Update the server/clients of our new map and populate any other players.
        if game.isclient or game.ishost:
            game.add_clients_to_map(game.client.client.registry)
            game.client.update_player(player.facing)

        # Stop the player's movement so they don't continue their move after they teleported.
        player.moving = False
Example #4
0
    def teleport(self, game, action):
        """Teleport the player to a particular map and coordinates

        :param game: The main game object that contains all the game's variables.
        :param action: The action (tuple) retrieved from the database that contains the action's
            parameters

        :type game: core.control.Control
        :type action: Tuple

        :rtype: None
        :returns: None

        Valid Parameters: map_name,coordinate_x,coordinate_y

        **Examples:**

        >>> action.__dict__
        {
            "type": "teleport",
            "parameters": [
                "map1.tmx",
                "5",
                "5"
            ]
        }

        """
        # Get the player object from the game.
        player = game.player1
        world = game.current_state

        # Get the teleport parameters for the position x,y and the map to load.
        mapname = str(action.parameters[0])
        position_x = int(action.parameters[1])
        position_y = int(action.parameters[2])

        # If we're doing a screen transition with this teleport, set the map name that we'll
        # load during the apex of the transition.
        if world.start_transition:
            world.delayed_mapname = mapname

        # Check to see if we're also performing a transition. If we are, wait to perform the
        # teleport at the apex of the transition
        if world.start_transition:
            world.delayed_teleport = True
            # Set the global_x/y variables based on the player's pixel position and the tile size.
            world.delayed_x = player.position[0] - (position_x * player.tile_size[0])
            world.delayed_y = player.position[1] - (position_y * player.tile_size[1]) + player.tile_size[1]
        # If we're not doing a transition, then just do the teleport
        else:
            # Set the global_x/y variables based on the player's pixel position and the tile size.
            world.global_x = player.position[0] - (position_x * player.tile_size[0])
            world.global_y = player.position[1] - (position_y * player.tile_size[1]) + player.tile_size[1]

            ### THIS NEEDS TO BE MOVED IN ITS OWN FUNCTION AND IS DUPLICATED IN THE World STATE ###
            from core.components.map import Map
            if prepare.BASEDIR + "resources/maps/" + mapname != world.current_map.filename:
                world.current_map = Map(
                    prepare.BASEDIR + "resources/maps/" + mapname)
                world.event_engine.current_map = Map(
                    prepare.BASEDIR + "resources/maps/" + mapname)
                world.tiles, world.collision_map, world.collision_lines_map, world.map_size = world.current_map.loadfile(
                    world.tile_size)
                world.game.events = world.current_map.events

                # Clear out any existing NPCs
                world.npcs = []

                # Scale the loaded tiles if enabled
                if world.scale > 1:
                    x_pos = 0        # Here we need to keep track of the x index of the list
                    # Loop through each row in the map. Each row is a list of
                    # Tile objects in that row.
                    for row in world.tiles:
                        y_pos = 0       # Here we need to keep track of the y index of the list within the row
                        # Now loop through each tile in the row and scale it
                        # accordingly.
                        for column in row:
                            if column:
                                layer_pos = 0
                                for tile in column:
                                    tile["surface"] = tools.scale_tile(tile["surface"], player.tile_size)
                                    world.tiles[x_pos][y_pos][layer_pos] = tile
                                    layer_pos += 1
                            y_pos += 1
                        x_pos += 1

        # Update the server/clients of our new map and populate any other players.
        if game.isclient or game.ishost:
            game.add_clients_to_map(game.client.client.registry)
            game.client.update_player(player.facing)

        # Stop the player's movement so they don't continue their move after they teleported.
        player.moving = False