Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Layer extends Terrain{
- public Block[][] map;
- public ArrayList<Block> blockList = new ArrayList<Block>();
- public Layer(int x, int y, int width, int height, int cellSize, ArrayList<Block> blockList){
- super(x, y, width, height, cellSize);
- this.blockList = blockList;
- init();
- }
- private void init(){
- map = new Block[getWidth()][getHeight()];
- map = initMap(map);
- map = switchBlock(map);
- }
- // Generate Random Block at x and y positions
- private Block[][] initMap(Block[][] map){
- int x, y;
- Random random = new Random();
- for(x = 0; x < getWidth(); x++){
- for(y = 0; y < getHeight(); y++){
- map[x][y] = blockList.get(random.nextInt(blockList.size()));
- }
- }
- return map;
- }
- // For every block check adjacent blocks and swith them to grass if current block is water
- private Block[][] switchBlock(Block[][] map){
- int currentX, currentY;
- for(currentX = 0; currentX < getWidth(); currentX++){
- for(currentY = 0; currentY < getHeight(); currentY++){
- if(map[currentX][currentY].getType() == BlockType.WATER){
- int finalX, finalY;
- int tempX = -1;
- int tempY = -1;
- if(currentX == 0) tempX = 0;
- if(currentY == 0) tempY = 0;
- for(finalX = tempX; finalX <= 1; finalX++){
- for(finalY = tempY; finalY <= 1; finalY++){
- if(map[finalX][finalY].getType() != BlockType.WATER){
- if(map[finalX][finalY].getType() != BlockType.GRASS_1){
- map[finalX][finalY] = blockList.get(1);
- }
- }
- }
- }
- }
- }
- }
- return map;
- }
- // Draw blocks on screen
- public void render(Graphics g){
- int x, y;
- int posX = getPosX();
- int posY = getPosY();
- for(x = 0; x < getWidth(); x++){
- if(x != 0) posX = posX + getCellSize();
- for(y = 0; y < getHeight(); y++){
- posY = posY + getCellSize();
- if(y == (getHeight() - 1)) posY = getPosY();
- g.drawImage(map[x][y].getImage(), posX, posY, null);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment