Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TileMap {
- private int[][] collision, map, depth;
- private int tileSize;
- private int numRows, numCols;
- private int width, height;
- private TextureRegion[][] tileset;
- private int numTilesAcross;
- public static final int NORMAL = 0, BLOCKED = 1;
- private int rowOffset, colOffset;
- private int numRowsToDraw, numColsToDraw;
- public TileMap(int tileSize) {
- this.tileSize = tileSize;
- numRowsToDraw = Bandit.HEIGHT / tileSize + 2;
- numColsToDraw = Bandit.WIDTH / tileSize + 2;
- }
- public void loadTileset(Texture tex) {
- numTilesAcross = tex.getWidth() / tileSize;
- tileset = TextureRegion.split(tex, tileSize, tileSize);
- }
- /**
- * parse order:
- * <tileset path>
- * <tile size>
- * <map width>
- * <map height>
- * <graphics map>
- * <collision map>
- * <depth map>
- */
- public void loadMap(String path) {
- try {
- FileHandle file = Gdx.files.internal(path);
- BufferedReader br = file.reader(8192);
- br.readLine();
- br.readLine();
- numCols = Integer.parseInt(br.readLine());
- numRows = Integer.parseInt(br.readLine());
- map = new int[numRows][numCols];
- collision = new int[numRows][numCols];
- depth = new int[numRows][numCols];
- width = numCols * tileSize;
- height = numRows * tileSize;
- String delims = "\\s+";
- for(int row = numRows - 1; row >= 0; row--) {
- String line = br.readLine();
- String[] tokens = line.split(delims);
- for(int col = 0; col < numCols; col++) {
- map[row][col] = Integer.parseInt(tokens[col]);
- collision[row][col] = Integer.parseInt(tokens[col]);
- }
- }
- for(int row = numRows - 1; row >= 0; row--) {
- String line = br.readLine();
- String[] tokens = line.split(delims);
- for(int col = 0; col < numCols; col++) {
- depth[row][col] = Integer.parseInt(tokens[col]);
- }
- }
- }
- catch(Exception e) {
- System.err.println("Could not load map at " + path + ".");
- e.printStackTrace();
- }
- }
- public int getTileSize() { return tileSize; }
- public int getWidth() { return width; }
- public int getHeight() { return height; }
- public int getNumRows() { return numRows; }
- public int getNumCols() { return numCols; }
- public int getType(int row, int col) {
- if(row < 0 || row >= numRows || col < 0 || col >= numCols) return 0;
- return collision[row][col];
- }
- public int getDepth(int row, int col) {
- if(row < 0 || row >= numRows || col < 0 || col >= numCols) return 0;
- return depth[row][col];
- }
- public void draw(SpriteBatch sb) {
- for(int row = rowOffset; row < rowOffset + numRowsToDraw; row++) {
- if(row >= numRows) break;
- for(int col = colOffset; col < colOffset + numColsToDraw; col++) {
- if(col >= numCols) break;
- if(map[row][col] == 0) continue;
- int rc = map[row][col];
- int r = rc / numTilesAcross;
- int c = rc % numTilesAcross;
- sb.draw(tileset[r][c], col * tileSize, row * tileSize);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement