Advertisement
Guest User

Untitled

a guest
Apr 24th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. public class TileMap {
  2.  
  3. private int[][] collision, map, depth;
  4. private int tileSize;
  5. private int numRows, numCols;
  6. private int width, height;
  7. private TextureRegion[][] tileset;
  8. private int numTilesAcross;
  9. public static final int NORMAL = 0, BLOCKED = 1;
  10. private int rowOffset, colOffset;
  11. private int numRowsToDraw, numColsToDraw;
  12.  
  13. public TileMap(int tileSize) {
  14. this.tileSize = tileSize;
  15. numRowsToDraw = Bandit.HEIGHT / tileSize + 2;
  16. numColsToDraw = Bandit.WIDTH / tileSize + 2;
  17. }
  18.  
  19. public void loadTileset(Texture tex) {
  20. numTilesAcross = tex.getWidth() / tileSize;
  21. tileset = TextureRegion.split(tex, tileSize, tileSize);
  22.  
  23. }
  24.  
  25. /**
  26. * parse order:
  27. * <tileset path>
  28. * <tile size>
  29. * <map width>
  30. * <map height>
  31. * <graphics map>
  32. * <collision map>
  33. * <depth map>
  34. */
  35. public void loadMap(String path) {
  36. try {
  37. FileHandle file = Gdx.files.internal(path);
  38. BufferedReader br = file.reader(8192);
  39. br.readLine();
  40. br.readLine();
  41. numCols = Integer.parseInt(br.readLine());
  42. numRows = Integer.parseInt(br.readLine());
  43. map = new int[numRows][numCols];
  44. collision = new int[numRows][numCols];
  45. depth = new int[numRows][numCols];
  46. width = numCols * tileSize;
  47. height = numRows * tileSize;
  48.  
  49. String delims = "\\s+";
  50. for(int row = numRows - 1; row >= 0; row--) {
  51. String line = br.readLine();
  52. String[] tokens = line.split(delims);
  53. for(int col = 0; col < numCols; col++) {
  54. map[row][col] = Integer.parseInt(tokens[col]);
  55. collision[row][col] = Integer.parseInt(tokens[col]);
  56. }
  57. }
  58.  
  59. for(int row = numRows - 1; row >= 0; row--) {
  60. String line = br.readLine();
  61. String[] tokens = line.split(delims);
  62. for(int col = 0; col < numCols; col++) {
  63. depth[row][col] = Integer.parseInt(tokens[col]);
  64. }
  65. }
  66. }
  67. catch(Exception e) {
  68. System.err.println("Could not load map at " + path + ".");
  69. e.printStackTrace();
  70. }
  71. }
  72.  
  73. public int getTileSize() { return tileSize; }
  74. public int getWidth() { return width; }
  75. public int getHeight() { return height; }
  76. public int getNumRows() { return numRows; }
  77. public int getNumCols() { return numCols; }
  78.  
  79. public int getType(int row, int col) {
  80. if(row < 0 || row >= numRows || col < 0 || col >= numCols) return 0;
  81. return collision[row][col];
  82. }
  83.  
  84. public int getDepth(int row, int col) {
  85. if(row < 0 || row >= numRows || col < 0 || col >= numCols) return 0;
  86. return depth[row][col];
  87. }
  88.  
  89. public void draw(SpriteBatch sb) {
  90. for(int row = rowOffset; row < rowOffset + numRowsToDraw; row++) {
  91. if(row >= numRows) break;
  92. for(int col = colOffset; col < colOffset + numColsToDraw; col++) {
  93. if(col >= numCols) break;
  94. if(map[row][col] == 0) continue;
  95.  
  96. int rc = map[row][col];
  97. int r = rc / numTilesAcross;
  98. int c = rc % numTilesAcross;
  99.  
  100. sb.draw(tileset[r][c], col * tileSize, row * tileSize);
  101.  
  102. }
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement