Guest User

Untitled

a guest
Dec 9th, 2019
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.neet.MapViewer.TileMap;
  2.  
  3. import javafx.scene.canvas.Canvas;
  4. import com.neet.MapViewer.Helper.*;
  5.  
  6. public class TileMap {
  7. private int[][] tileType;
  8.  
  9. public Canvas mapCanvas = new Canvas(640,640); //640 because there are 40 tile with tile size 16;
  10.  
  11. /**
  12. * initialise canvas for the tile map.
  13. */
  14. public void render(){
  15. drawMap();
  16. }
  17.  
  18. public void drawMap() {
  19. tileType = new int[Map.getNumRows()][Map.getNumCols()];
  20.  
  21. for(int row = 0; row < Map.getNumRows(); row++) {
  22. for(int col = 0; col < Map.getNumCols(); col++) {
  23. if(Map.getTile(row, col) == 0) continue;
  24.  
  25. /* Variable r is for any tile that is not dividable by 20 is a non blocked tile
  26. Variable c is for the column of source. If the tile mob 20 is 0 thn it its at first row of source
  27. and if mob 20 is 1 than it is at the second row of source.
  28. */
  29. int r = Map.getTile(row, col) / Img.getNumTilesAcross();
  30. int c = Map.getTile(row, col) % Img.getNumTilesAcross();
  31.  
  32. if (r == 0) {
  33. mapCanvas.getGraphicsContext2D().drawImage(
  34. Img.tileSet, c * Img.TILESIZE, 0, Img.TILESIZE, Img.TILESIZE,
  35. col * Img.TILESIZE, row * Img.TILESIZE, Img.TILESIZE, Img.TILESIZE);
  36. tileType[row][col] = 0;
  37. }
  38. else {
  39. mapCanvas.getGraphicsContext2D().drawImage(
  40. Img.tileSet, c * Img.TILESIZE, Img.TILESIZE, Img.TILESIZE, Img.TILESIZE,
  41. col * Img.TILESIZE, row * Img.TILESIZE, Img.TILESIZE, Img.TILESIZE);
  42. tileType[row][col] = 1;
  43. }
  44. }
  45. }
  46. }
  47. }
RAW Paste Data