Get coordinates of arraylist

Posted by opiop65 on Game Development See other posts from Game Development or by opiop65
Published on 2012-11-10T01:01:32Z Indexed on 2012/11/10 5:16 UTC
Read the original article Hit count: 316

Filed under:
|

Here's my map class:

public class map{ public static final int CLEAR = 0;
public static final ArrayList<Integer> STONE = new ArrayList<Integer>();
public static final int GRASS = 2;
public static final int DIRT = 3;

public static final int WIDTH = 32;
public static final int HEIGHT = 24;

public static final int TILE_SIZE = 25;

// static int[][] map = new int[WIDTH][HEIGHT];
ArrayList<ArrayList<Integer>> map = new ArrayList<ArrayList<Integer>>(WIDTH * HEIGHT);

enum tiles {
    air, grass, stone, dirt
}

Image air, grass, stone, dirt;

Random rand = new Random();

public Map() {

    /* default map */

    /*for(int y = 0; y < WIDTH; y++){
        map[y][y] = (rand.nextInt(2));
        System.out.println(map[y][y]);
    }*/

    /*for (int y = 18; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            map[x][y] = STONE;
        }

    }

    for (int y = 18; y < 19; y++) {
        for (int x = 0; x < WIDTH; x++) {
            map[x][y] = GRASS;
        }

    }

    for (int y = 19; y < 20; y++) {
        for (int x = 0; x < WIDTH; x++) {
            map[x][y] = DIRT;
        }

    }*/

    for (int y = 0; y < HEIGHT; y++) {
        for(int x = 0; x < WIDTH; x++){
            map.set(x * WIDTH + y, STONE);
        }
    }

    try {
        init(null, null);
    } catch (SlickException e) {
        e.printStackTrace();
    }
    render(null, null, null);

}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
    air = new Image("res/air.png");
    grass = new Image("res/grass.png");
    stone = new Image("res/stone.png");
    dirt = new Image("res/dirt.png");
}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) {

    for (int x = 0; x < WIDTH; x++) {
        for (int y = 0; y < HEIGHT; y++) {
            switch (map.get(x * WIDTH + y)) {
            case CLEAR:
                air.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
                break;
            case STONE:
                stone.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
                break;
            case GRASS:
                grass.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
                break;
            case DIRT:
                dirt.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
                break;
            }
        }
    }
}

public static boolean blocked(float x, float y) {
    return map[(int) x][(int) y] == STONE;
}

public static Rectangle blockBounds(int x, int y) {
    return (new Rectangle(x, y, TILE_SIZE, TILE_SIZE));
}

}

Specifically I am looking at this:

for (int x = 0; x < WIDTH; x++) {
        for (int y = 0; y < HEIGHT; y++) {
            switch (map.get(x * WIDTH + y).intValue()) {
            case CLEAR:
                air.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
                break;
            case STONE:
                stone.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
                break;
            case GRASS:
                grass.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
                break;
            case DIRT:
                dirt.draw(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
                break;
            }
        }
    }

How can I access the coordinates of my arraylist map and then draw the tiles to the screen? Thanks!

© Game Development or respective owner

Related posts about java

Related posts about tiles