Guest User

Untitled

a guest
Feb 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. private void SetupTiles()
  2. {
  3. //get reference positions
  4. int centerRow = chunk.size / 2;
  5. int centerCol = chunk.size / 2;
  6.  
  7. // Loop through the chunk and add tiles
  8. for(int row = 0; row < chunk.size; row ++)
  9. {
  10. for(int col = 0; col < chunk.size; col ++)
  11. {
  12. //Water - 0
  13. //Grass - 1
  14.  
  15. //Create TILE
  16. //default tile is water
  17. Tile tile = new Tile(col, row, chunk.tileSize, tileType.Water, GetRandomWaterTexture());
  18. //since the island is smaller than chunk,
  19. //check if current index is within island size.
  20. if(row >= border && row < (chunkSize-border) && col >= border && col < (chunkSize-border))
  21. {
  22. //Randomly assign tiles type
  23. //TODO: This random system is not very good...
  24. if(MathUtils.random(100)>50)
  25. {
  26. tile.texture = GetRandomGrassTexture();
  27. tile.type = tileType.Grass;
  28. }
  29. }
  30.  
  31. //add tile to chunk.
  32. chunk.tiles[row][col] = tile;
  33. }
  34. }
  35.  
  36. //Initialize smoothedChunk with water
  37. //At this point, chunk is randomly filled with grass, and smoothedChunk is just water.
  38. for(int i = 0; i < chunkSize; i++)
  39. {
  40. for(int j = 0; j < chunkSize; j++)
  41. {
  42. Tile tile = new Tile(i, j, 8, tileType.Water, GetRandomWaterTexture());
  43. smoothedChunk.tiles[i][j] = tile;
  44. }
  45. }
  46.  
  47. //TODO: Smoothed island is not really smooth...
  48. //Smooth out chunk
  49. SmoothMap();
  50. //Set centre tile for camera positioning
  51. centreTile = chunk.GetTile(centerRow, centerCol);
  52. }
  53.  
  54. private void SmoothMap ()
  55. {
  56. //first loop, get surrounding tiles for each tile in chunk
  57. //we are excluding the outer most edge
  58. for(int r = 1; r < chunkSize-1; r++)
  59. {
  60. for(int c = 1; c < chunkSize-1; c++)
  61. {
  62. //if current tile is not null
  63. if(chunk.tiles[r][c] != null)
  64. {
  65. //Get its surrounding tiles.
  66. //At this point, smoothedChunk is still empty.
  67. chunk.tiles[r][c].surroundingTiles = GetSurroundingTiles(r, c, chunk);
  68. //Debug info
  69. //System.out.println("Position: " + chunk.tiles[r][c].row + ", " + chunk.tiles[r][c].col);
  70. //System.out.println("Current tile type: " + chunk.GetTileCode(r, c));
  71. //System.out.println("Neighboring tiles: " + chunk.tiles[r][c].surroundingTiles);
  72. //System.out.println();
  73. }
  74. }
  75. }
  76.  
  77. //second loop, smooth tiles according to their surrounding tiles
  78. for(int r = 1; r < chunkSize-1; r++)
  79. {
  80. for(int c = 1; c < chunkSize-1; c++)
  81. {
  82. //Write into smoothedChunk
  83. //can the threshold be a variable?
  84. if (chunk.tiles[r][c].surroundingTiles >= 4)
  85. {
  86. smoothedChunk.tiles[r][c].texture = GetRandomWaterTexture();
  87. smoothedChunk.tiles[r][c].type = tileType.Water;
  88. }
  89. else if (chunk.tiles[r][c].surroundingTiles < 4)
  90. {
  91. smoothedChunk.tiles[r][c].texture = GetRandomGrassTexture();
  92. smoothedChunk.tiles[r][c].type = tileType.Grass;
  93. }
  94. }
  95. }
  96.  
  97. //copy tiles over from smoothedChunk to chunk,
  98. //because we still want to use chunk for everything.
  99. for(int i = 0; i < chunkSize; i++)
  100. {
  101. for(int j = 0; j < chunkSize; j++)
  102. {
  103. chunk.tiles[i][j] = smoothedChunk.tiles[i][j];
  104. }
  105. }
  106. //don't know the difference between the loop above and
  107. //chunk = smoothedChunk;
  108. }
Add Comment
Please, Sign In to add comment