Search code examples
javaarraysloopssortingobject

How do I detect the highest z-level object in a 3d array?


I'm working on a simple game in Java (think Rogue 1980), and have a tile rendering system in the draw() method. I'm transitioning from a 2d array for tiles to the 3d array so I can have tiles persist even when walked over by the player tile

Here is the draw method:

public void draw(Graphics g) {
    Font font = new Font("TimesRoman", Font.BOLD, tileSize);
    g.setFont(font);

    // Grid Lines
    g.setColor(Color.gray);
    for (int i = 0; i < boardWidth / tileSize; i++) {
        g.drawLine(i * tileSize, 0, i * tileSize, boardHeight);
        g.drawLine(0, i * tileSize, boardWidth, i * tileSize);
    }

    //Debug output
    //System.out.println("Player coordinates: X=" + player.getX() + ", Y=" + player.getY());

    // Draw Entities Tiles
    g.setColor(Color.white);
    for (int i = 0; i < boardWidth / tileSize; i++) {
        for (int j = 0; j < boardHeight / tileSize; j++) {
            for (int k = 0; k < 1; k++) {
                Tile t = tiles[i][j][k];
                if (t != null) {  //No null exception errors
                    switch (t.getType()) {
                        case NULL:
                            break;
                        case DECO:
                            g.setColor(Color.CYAN);
                            break;
                        case ENTITY:
                            g.setColor(Color.blue);
                            break;
                        case PLAYER:
                            g.setColor(Color.green);
                            break;
                        case WALL:
                            g.setColor(Color.lightGray);
                            break;
                        case ENEMY:
                            g.setColor(Color.red);
                            break;
                        case LOOTABLE:
                            g.setColor(Color.yellow);
                            break;
                        default:
                            g.setColor(Color.white);
                            break;
                    }
                    g.drawString(String.valueOf(t.getChar()), t.getX() * tileSize, t.getY() * tileSize);
                }
            }
        }
    }
}

I've added default tiles (walls, floor deco, coins) to z-level 0, and the player to z-level 1. The goal is to only render the highest z-level tile in a grid position


Solution

  • The code in the question has this line: Tile t = tiles[i][j][k];. I assume k is the variable corresponding to the z-level.

    I also assume "the highest z-level" means the qualifying tile at the highest z-level. Otherwise, all tiles rendered would be from the same z-level.

    So, you can create a method to get the highest non-empty tile.

       public Tile getTileZ (int x, int y) {
           for (int z = zMax; z >= 0; z--) {
              if ( tiles [x][y][z].qualifies() ) { return tiles [x][y][z]; }
           }
           return tiles [x][y][0];
       }
    

    In this version, getTileZ is an instance method, and tiles is an instance variable. You could make the method static and pass tiles as an argument.

    I don't know how it is to be decided that a tile qualifies or not. So, I imagined the Tile class has an instance method public boolean qualifies ().

    So, the line Tile t = tiles[i][j][k]; is replaced with a line something like this: Tile t = getTileZ (i, j);.