DrenSkywalker

Layer

Oct 10th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. public class Layer extends Terrain{
  2.  
  3.     public Block[][] map;
  4.     public ArrayList<Block> blockList = new ArrayList<Block>();
  5.  
  6.     public Layer(int x, int y, int width, int height, int cellSize, ArrayList<Block> blockList){
  7.         super(x, y, width, height, cellSize);
  8.    
  9.         this.blockList = blockList;
  10.        
  11.         init();
  12.     }
  13.    
  14.     private void init(){
  15.         map = new Block[getWidth()][getHeight()];
  16.         map = initMap(map);
  17.         map = switchBlock(map);
  18.     }
  19.    
  20.     // Generate Random Block at x and y positions
  21.     private Block[][] initMap(Block[][] map){
  22.         int x, y;
  23.        
  24.         Random random = new Random();
  25.        
  26.         for(x = 0; x < getWidth(); x++){
  27.             for(y = 0; y < getHeight(); y++){
  28.                 map[x][y] = blockList.get(random.nextInt(blockList.size()));
  29.             }
  30.         }
  31.         return map;
  32.     }
  33.    
  34.     // For every block check adjacent blocks and swith them to grass if current block is water
  35.     private Block[][] switchBlock(Block[][] map){
  36.         int currentX, currentY;
  37.  
  38.         for(currentX = 0; currentX < getWidth(); currentX++){
  39.             for(currentY = 0; currentY < getHeight(); currentY++){
  40.  
  41.                 if(map[currentX][currentY].getType() == BlockType.WATER){
  42.                    
  43.                     int finalX, finalY;
  44.                     int tempX = -1;
  45.                     int tempY = -1;
  46.                    
  47.                     if(currentX == 0) tempX = 0;
  48.                     if(currentY == 0) tempY = 0;
  49.                    
  50.                     for(finalX = tempX; finalX <= 1; finalX++){
  51.                         for(finalY = tempY; finalY <= 1; finalY++){
  52.                            
  53.                             if(map[finalX][finalY].getType() != BlockType.WATER){
  54.                                 if(map[finalX][finalY].getType() != BlockType.GRASS_1){
  55.                                     map[finalX][finalY] = blockList.get(1);
  56.                                 }
  57.                             }
  58.                         }
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.         return map;
  64.     }
  65.  
  66.     // Draw blocks on screen
  67.     public void render(Graphics g){
  68.         int x, y;
  69.         int posX = getPosX();
  70.         int posY = getPosY();
  71.        
  72.         for(x = 0; x < getWidth(); x++){
  73.             if(x != 0) posX = posX + getCellSize();
  74.            
  75.             for(y = 0; y < getHeight(); y++){
  76.                 posY = posY + getCellSize();
  77.                
  78.                 if(y == (getHeight() - 1)) posY = getPosY();
  79.  
  80.                 g.drawImage(map[x][y].getImage(), posX, posY, null);
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment