Finding And Focusing On Player

When displaying a portion of the map upon the combat screen I had to ensure that the player was displayed on screen at all times.

Finding The Player

In order to find the player within the map I created the “checkTileForPlayer” method which checks if a single integer is one of the various numbers that represent the player.

/**
     * A method that checks for any numbers that represent player-occupied versions of tiles
     * (such as "blank_with_player" and "door_with_player").
     * @param tile The number to be checked.
     * @return A boolean indicating if the tile contains the player character.
     */
    public Boolean checkTileForPlayer(int tile) {
        boolean playerFound = false;
        if(tile == 7 || tile == 104 || tile == 108 || tile == 112 || tile == 116 || tile == 126) {
            playerFound = true;
        }


        return playerFound;
    }

However this still left the issue of going through the unusual layout of my integer array maps.

I solved this issue with the creation of my most ramshackle and unwieldy method “findPlayer”.

This determines the “Column Number”, “Column Depth”, “Row”, “Item Number” and overall “Position” of the players current location.

/**
     * Locates the player within the map and provides their precise coordinates.
     * @return columnAndDepthAndRowAndItemAndPosition An array containing the Column Number,
     * Column Depth, Row, Item Number and overall Position of the players current location.
     */
    public int[] findPlayer() {
        /**
         * An array containing the column number and column depth to be returned.
         *
         * Now also includes which row contains the player along with the exact Item number
         * of the player to pinpoint their exact position with precision.
         */
        int[] columnAndDepthAndRowAndItemAndPosition = {0, 0, 0, 0, 0};

        //int a = 0;
        //^ REPLACED WITH columnAndDepthAndRowAndItemAndPosition[4] !!!!
        int currentRow = 0;

        int columnDepth = 0;
        while (columnAndDepthAndRowAndItemAndPosition[4] < map.length) {
            int currentColumn = 0;
            /**
             * Number Of Columns Loop
             * loops whatever the single column loop would have done several times depending on how many columns there are
             */
            for (int column = 0; column < numberOfColumns; column++) {
                /**
                 * Single Column Loop
                 * loop: goes through all 11 lines/rows of a single column in the map
                 *
                 * Line: the line being looked at in the column (11 each column)
                 */
                for (int line = 0; line < mapHeight; line++) {
                    currentColumn++;
                    /**
                     * Single Line Loop
                     * loop: goes through a line in the map
                     *
                     * itemNum: the item being looked at in the line (17 each line).
                     */
                    for (int itemNum = 0; itemNum < mapWidth; itemNum++) {
                        /**
                         * If reached the end of the current row (across all columns) increase currentRow by 1.
                         *
                         * if(a % 51 == 0)
                         * maybe replace 51 with ((mapWidth * numberOfColumns) - 1)?
                         * maybe replace 51 with (mapWidth * numberOfColumns)?
                         */
                        if (columnAndDepthAndRowAndItemAndPosition[4] % (mapWidth * numberOfColumns) == 0) {
                            /**
                             * if this is the last row in a column then depth increases by 1.
                             */
                            if (currentRow % mapHeight == 0) {
                                columnDepth++;
                            }
                            currentRow++;
                        }
                        /**
                         * PLAYER FOUND!
                         *
                         * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                         * current version checks for player by seeing if a is "7" as that is the number that represents "blank_with_player"
                         * Future versions should have a "checkTileForPlayer(int tile)" method that checks for any numbers that represent player-occupied versions of tiles (such as "blank_with_player" and "door_with_player")
                         * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                         */
                        if (checkTileForPlayer(map[columnAndDepthAndRowAndItemAndPosition[4]])) {
                            columnAndDepthAndRowAndItemAndPosition[0] = currentColumn;
                            columnAndDepthAndRowAndItemAndPosition[1] = columnDepth;
                            /**
                             * USE "CURRENTROW" AND "ITEMNUM" TO FIND PLAYER BEFORE AND AFTER MOVING THEN
                             *
                             * MAKE FINDPLAYER() RETURN "CURRENTROW" AND "ITEMNUM" ASWELL!
                             *
                             *
                             */
                            columnAndDepthAndRowAndItemAndPosition[2] = currentRow;
                            columnAndDepthAndRowAndItemAndPosition[3] = itemNum;
                            //System.out.println("Row: " + (currentRow));
                            //System.out.println("Item: " + (itemNum));
                            System.out.println("LOCATION: " + (columnAndDepthAndRowAndItemAndPosition[4]));
                            /**
                             * If I put return here does it skip the rest of the loop?
                             */
                            return columnAndDepthAndRowAndItemAndPosition;
                        }
                        columnAndDepthAndRowAndItemAndPosition[4]++;
                    }
                    if (currentColumn == numberOfColumns) {
                        currentColumn = 0;
                    }
                }
            }
        }
        return columnAndDepthAndRowAndItemAndPosition;
    }

Although this method likely returns more information than is needed it performs it’s intended purpose.

A section of the map integer array highlighting the number representing the player standing on stone.

Focusing On The Player

The “setupMap” method does the lions share of the work in actually going through the map and finding numbers representing images to assign to tiles on the combat screen.

As the map is split into nine sections, each big enough to fill the combat screen, the method only displays whichever section currently contains the player.

/**
     * Peruses a section of the map for numbers representing images to assign to tiles.
     * @param targetColumn The column number of the target section of the map.
     * @param targetColumnDepth The column depth (as im using the word "row" for something else) of
     *                          the target section of the map.
     */
    public void setupMap(int targetColumn, int targetColumnDepth) {

        /**
         * The map item currently being looked at.
         */
        int a = 0;
        /**
         * The row currently being looked at.
         */
        int currentRow = 0;
        /**
         * The column depth currently being looked at.
         */
        int columnDepth = 0;
        /**
         * Tile currently being set/worked-on.
         */
        int currentTile = 0;
        System.out.println("LENGTH: " + map.length);
        while (a < map.length) {
            int currentColumn = 0;
            /**
             * Number Of Columns Loop
             * loops whatever the single column loop would have done several times depending on how many columns there are
             */
            for (int column = 0; column < numberOfColumns; column++) {
                /**
                 * Single Column Loop
                 * loop: goes through all 11 lines/rows of a single column in the map
                 *
                 * Line: the line being looked at in the column (11 each column)
                 */
                for (int line = 0; line < mapHeight; line++) {
                    currentColumn++;
                    /**
                     * The entire row in the form of a string (USED FOR TESTING).
                     */
                    //String row = "";
                    /**
                     * Single Line Loop
                     * loop: goes through a line in the map
                     *
                     * itemNum: the item being looked at in the line (17 each line).
                     */
                    for (int itemNum = 0; itemNum < mapWidth; itemNum++) {

                        // if (row == "") {
                        //      row = row + map[a];
                        //  } else {
                        //       row = row + ", " + map[a];
                        //    }
                        /**
                         * If reached the end of the current row (across all columns) increase currentRow by 1.
                         *
                         * if(a % 51 == 0)
                         * maybe replace 51 with ((mapWidth * numberOfColumns) - 1)?
                         * maybe replace 51 with (mapWidth * numberOfColumns)?
                         */
                        if (a % (mapWidth * numberOfColumns) == 0) {
                            /**
                             * if this is the last row in a column then depth increases by 1.
                             */
                            if (currentRow % mapHeight == 0) {
                                columnDepth++;
                            }
                            currentRow++;
                        }
                        /**
                         * what is needed to set parameters of where on the map to load?
                         * targetColumn:
                         * targetColumnDepth:
                         */
                        if (currentColumn == targetColumn && columnDepth == targetColumnDepth) {
                            String i = ("i" + currentTile);
                            //System.out.println(i + " ----------------- Tile: " + currentTile + "      map: " + a);
                            /**
                             * set tile
                             */
                            setTile(a, tiles[currentTile]);
                            //tiles[currentTile].setImageResource(R.drawable.blank);
                            currentTile++;
                        }
                        //System.out.println("Column: " + (currentColumn));
                        //System.out.println("Column Depth: " + (columnDepth));

                        /**
                         * USE "CURRENTROW" AND "ITEMNUM" TO FIND PLAYER BEFORE AND AFTER MOVING THEN
                         *
                         * MAKE FINDPLAYER() RETURN "CURRENTROW" AND "ITEMNUM" ASWELL!
                         *
                         *
                         */
                        //System.out.println("Row: " + (currentRow));
                        //System.out.println("Item: " + (itemNum));
                        a++;
                    }
                    //System.out.println("Line: " + (line + 1));
                    //System.out.println(row);
                    if (currentColumn == numberOfColumns) {
                        currentColumn = 0;
                    }
                }
            }
        }
    }

Assigning of appropriate images to tiles on the combat screen is done using the “setTile” method.

/**
     * Sets the image displayed by the ImageView to the appropriate tile for the number provided.
     * @param tileNum The number taken from the map which represents a specific type of tile.
     * @param tile The ImageView used to display a tile upon the combat screen.
     */
    public void setTile(int tileNum, ImageView tile) {
        switch (map[tileNum]) {
            case 101: //Stone Wall Up
                tile.setImageResource(R.drawable.stone_wall_up);
                break;
            case 102: //Stone Wall Up Door
                tile.setImageResource(R.drawable.stone_wall_up_door);
                break;
            case 103: //Stone Wall Up Door Open
                tile.setImageResource(R.drawable.stone_wall_up_door_open);
                break;
            case 104: //Stone Wall Up Door Open With Player
                tile.setImageResource(R.drawable.stone_wall_up_door_open_with_player);
                break;

            case 105: //Stone Wall Left
                tile.setImageResource(R.drawable.stone_wall_left);
                break;
            case 106: //Stone Wall Left Door
                tile.setImageResource(R.drawable.stone_wall_left_door);
                break;
            case 107: //Stone Wall Left Door Open
                tile.setImageResource(R.drawable.stone_wall_left_door_open);
                break;
            case 108: //Stone Wall Left Door Open With Player
                tile.setImageResource(R.drawable.stone_wall_left_door_open_with_player);
                break;

            case 109: //Stone Wall Right
                tile.setImageResource(R.drawable.stone_wall_right);
                break;
            case 110: //Stone Wall Right Door
                tile.setImageResource(R.drawable.stone_wall_right_door);
                break;
            case 111: //Stone Wall Right Door Open
                tile.setImageResource(R.drawable.stone_wall_right_door_open);
                break;
            case 112: //Stone Wall Right Door Open With Player
                tile.setImageResource(R.drawable.stone_wall_right_door_open_with_player);
                break;

            case 113: //Stone Wall Down
                tile.setImageResource(R.drawable.stone_wall_down);
                break;
            case 114: //Stone Wall Down Door
                tile.setImageResource(R.drawable.stone_wall_down_door);
                break;
            case 115: //Stone Wall Down Door Open
                tile.setImageResource(R.drawable.stone_wall_down_door_open);
                break;
            case 116: //Stone Wall Down Door Open With Player
                tile.setImageResource(R.drawable.stone_wall_down_door_open_with_player);
                break;

            case 117: //Stone Wall Up Left Corner
                tile.setImageResource(R.drawable.stone_wall_up_left_corner);
                break;
            case 118: //Stone Wall Up Right Corner
                tile.setImageResource(R.drawable.stone_wall_up_right_corner);
                break;
            case 119: //Stone Wall Down Left Corner
                tile.setImageResource(R.drawable.stone_wall_down_left_corner);
                break;
            case 120: //Stone Wall Down Right Corner
                tile.setImageResource(R.drawable.stone_wall_down_right_corner);
                break;


            case 121: //Stone Wall Up Left Square
                tile.setImageResource(R.drawable.stone_wall_up_left_square);
                break;
            case 122: //Stone Wall Up Right Square
                tile.setImageResource(R.drawable.stone_wall_up_right_square);
                break;
            case 123: //Stone Wall Down Left Square
                tile.setImageResource(R.drawable.stone_wall_down_left_square);
                break;
            case 124: //Stone Wall Down Right Square
                tile.setImageResource(R.drawable.stone_wall_down_right_square);
                break;

            case 125: //Stone
                tile.setImageResource(R.drawable.stone);
                break;

            case 126: //Stone With Player
                tile.setImageResource(R.drawable.stone_with_player);
                break;

            case 127: //Stone With Poison Gas
                tile.setImageResource(R.drawable.stone_with_poison_gas);
                break;


            case 128: //Stone Wall Up Left Corner
                tile.setImageResource(R.drawable.stone_wall_up_left_corner_with_poison_gas);
                break;
            case 129: //Stone Wall Up Right Corner
                tile.setImageResource(R.drawable.stone_wall_up_right_corner_with_poison_gas);
                break;
            case 130: //Stone Wall Down Left Corner
                tile.setImageResource(R.drawable.stone_wall_down_left_corner_with_poison_gas);
                break;
            case 131: //Stone Wall Down Right Corner
                tile.setImageResource(R.drawable.stone_wall_down_right_corner_with_poison_gas);
                break;


            case 132: //Stone Wall Up Left Square
                tile.setImageResource(R.drawable.stone_wall_up_left_square_with_poison_gas);
                break;
            case 133: //Stone Wall Up Right Square
                tile.setImageResource(R.drawable.stone_wall_up_right_square_with_poison_gas);
                break;
            case 134: //Stone Wall Down Left Square
                tile.setImageResource(R.drawable.stone_wall_down_left_square_with_poison_gas);
                break;
            case 135: //Stone Wall Down Right Square
                tile.setImageResource(R.drawable.stone_wall_down_right_square_with_poison_gas);
                break;

            case 136: //Stone Wall Down
                tile.setImageResource(R.drawable.stone_wall_up_with_poison_gas);
                break;

            case 137: //Stone Wall Down
                tile.setImageResource(R.drawable.stone_wall_left_with_poison_gas);
                break;

            case 138: //Stone Wall Down
                tile.setImageResource(R.drawable.stone_wall_right_with_poison_gas);
                break;

            case 139: //Stone Wall Down
                tile.setImageResource(R.drawable.stone_wall_down_with_poison_gas);
                break;


            case 140: //Stone Wall Up Door
                tile.setImageResource(R.drawable.stone_wall_up_door_with_poison_gas);
                break;
            case 141: //Stone Wall Up Door Open
                tile.setImageResource(R.drawable.stone_wall_up_door_open_with_poison_gas);
                break;
            case 142: //Stone Wall Left Door
                tile.setImageResource(R.drawable.stone_wall_left_door_with_poison_gas);
                break;
            case 143: //Stone Wall Left Door Open
                tile.setImageResource(R.drawable.stone_wall_left_door_open_with_poison_gas);
                break;
            case 144: //Stone Wall Right Door
                tile.setImageResource(R.drawable.stone_wall_right_door_with_poison_gas);
                break;
            case 145: //Stone Wall Right Door Open
                tile.setImageResource(R.drawable.stone_wall_right_door_open_with_poison_gas);
                break;
            case 146: //Stone Wall Down Door
                tile.setImageResource(R.drawable.stone_wall_down_door_with_poison_gas);
                break;
            case 147: //Stone Wall Down Door Open
                tile.setImageResource(R.drawable.stone_wall_down_door_open_with_poison_gas);
                break;

            case 148: //Stone With Player
                tile.setImageResource(R.drawable.stone_with_player_with_poison_gas);
                break;
            case 149: //Stone Wall Up Door Open With Player
                tile.setImageResource(R.drawable.stone_wall_up_door_open_with_player_with_poison_gas);
                break;
            case 150: //Stone Wall Left Door Open With Player
                tile.setImageResource(R.drawable.stone_wall_left_door_open_with_player_with_poison_gas);
                break;
            case 151: //Stone Wall Right Door Open With Player
                tile.setImageResource(R.drawable.stone_wall_right_door_open_with_player_with_poison_gas);
                break;
            case 152: //Stone Wall Down Door Open With Player
                tile.setImageResource(R.drawable.stone_wall_down_door_open_with_player_with_poison_gas);
                break;


            case 153: //Stone With Acid
                tile.setImageResource(R.drawable.stone_with_acid);
                break;
        }
    }

Leave a Reply