Advertisement
Guest User

Random TileMap Dungeon Generation

a guest
Nov 11th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.56 KB | None | 0 0
  1. ////////////////////
  2. ////////////////////
  3.  
  4. TileMap Class:
  5.  
  6. ////////////////////
  7. ////////////////////
  8.  
  9. import java.util.ArrayList;
  10. import java.util.Random;
  11.  
  12. import org.newdawn.slick.GameContainer;
  13. import org.newdawn.slick.Graphics;
  14.  
  15. import com.pickens.util.Constants;
  16. import com.pickens.util.Viewport;
  17.  
  18. public class TileMap {
  19.  
  20.     Random r = new Random();
  21.    
  22.     ArrayList<Room> rooms = new ArrayList<Room>();
  23.    
  24.     private int w, h;
  25.     private Tile[][] tile;
  26.     private int[][] map;
  27.     private int numberOfRooms; 
  28.    
  29.     public TileMap(int w, int h, int numberOfRooms) {
  30.         this.w = w;
  31.         this.h = h;
  32.         this.numberOfRooms = numberOfRooms;
  33.         generateMap(w, h);
  34.         tile = new Tile[w][h];
  35.         for(int y = 0; y < h; y++) {
  36.             for(int x = 0; x < w; x++) {
  37.                 tile[x][y] = new Tile(x*Constants.TILE_SIZE, y*Constants.TILE_SIZE, map[x][y]);
  38.             }
  39.         }
  40.        
  41.     }
  42.    
  43.     public void render(int ox, int oy, Graphics g) {
  44.         for(int y = 0; y < h; y++) {
  45.             for(int x = 0; x < w; x++) {
  46.                 tile[x][y].render(ox, oy, g);
  47.             }
  48.         }
  49.     }
  50.    
  51.     public void update(GameContainer gc, int delta) {
  52.        
  53.     }
  54.    
  55.     int i = 0;
  56.     Room p = null;
  57.     public void generateMap(int w, int h) {
  58.         map = new int[w][h];
  59.         for(int y = 0; y < h; y++) {
  60.             for(int x = 0; x < w; x++) { // Makes array of empty tiles
  61.                 map[x][y] = Constants.EMPTY;
  62.             }
  63.         }
  64.        
  65.         createRoom(w/2, h/2); // Create the starting room in the center of the map and branch off from there
  66.        
  67.         // Create Rooms
  68.         for(i = 0; i < numberOfRooms; i++) {
  69.             createRoom(); // Create the rest of the rooms randomly on the map
  70.         }
  71.        
  72.         // Add pretty tiles
  73.         // Add walls to floors
  74.         for(int y = 0; y < h; y++) {
  75.             for(int x = 0; x < w; x++) {
  76.                 if(map[x][y] == Constants.FLOOR) {
  77.                     if(x > 0 && map[x-1][y] == Constants.EMPTY) {
  78.                         map[x-1][y] = Constants.WALL;
  79.                     }
  80.                     if(x < w && map[x+1][y] == Constants.EMPTY) {
  81.                         map[x+1][y] = Constants.WALL;              
  82.                     }
  83.                     if(y > 0 && map[x][y-1] == Constants.EMPTY) {
  84.                         map[x][y-1] = Constants.WALL;
  85.                     }
  86.                     if(y < h && map[x][y+1] == Constants.EMPTY) {
  87.                         map[x][y+1] = Constants.WALL;
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.     }
  93.    
  94.     public void createRoom() {
  95.         /*int ww = w/numberOfRooms/2 + r.nextInt(w/numberOfRooms/2);
  96.         int hh = h/numberOfRooms/2 + r.nextInt(h/numberOfRooms/2);*/
  97.         int ww = 5 + r.nextInt(5);
  98.         int hh = 5 + r.nextInt(5);
  99.         int x = r.nextInt(w - ww);
  100.         int y = r.nextInt(h - hh);
  101.         Room newRoom = new Room(x, y, ww, hh);
  102.         boolean failed = false;
  103.         for(int ii = 0; ii < rooms.size(); ii++) {
  104.             Room otherRoom = rooms.get(ii);
  105.             if(newRoom.intersects(otherRoom)) {
  106.                 failed = true;
  107.                 i--;
  108.                 break;
  109.             }
  110.         }
  111.         if(!failed) {
  112.             rooms.add(newRoom);
  113.            
  114.             if(p != null) {                
  115.                 if(r.nextInt(2) == 0) {
  116.                     hCorridor(p.center.x, newRoom.center.x, p.center.y);
  117.                     vCorridor(p.center.y, newRoom.center.y, newRoom.center.x);
  118.                 } else {
  119.                     vCorridor(p.center.y, newRoom.center.y, p.center.x);
  120.                     hCorridor(p.center.x, newRoom.center.x, newRoom.center.y);
  121.                 }
  122.             }
  123.            
  124.             p = newRoom;
  125.            
  126.             carveRoom(newRoom);
  127.         }
  128.     }
  129.    
  130.     public void createRoom(int xx, int yy) {
  131.         // Random widths and heights
  132.         int ww = 5 + r.nextInt(5);
  133.         int hh = 5 + r.nextInt(5);
  134.         // Getting the x and y of the room
  135.         int x = xx-ww/2;
  136.         int y = yy-hh/2;
  137.         Room newRoom = new Room(x, y, ww, hh);
  138.         boolean failed = false;
  139.         // Loops through all rooms and checks to see if the current room intersects any of them
  140.         for(int ii = 0; ii < rooms.size(); ii++) {
  141.             Room otherRoom = rooms.get(ii);
  142.             if(newRoom.intersects(otherRoom)) {
  143.                 failed = true;
  144.                 i--;
  145.                 break;
  146.             }
  147.         }
  148.         if(!failed) {
  149.             // If there is no intersection
  150.             rooms.add(newRoom);
  151.            
  152.             // Make corridor connecting this room to last based on the center position of
  153.             // each room.
  154.             if(p != null) {                
  155.                 if(r.nextInt(2) == 0) {
  156.                     hCorridor(p.center.x, newRoom.center.x, p.center.y);
  157.                     vCorridor(p.center.y, newRoom.center.y, newRoom.center.x);
  158.                 } else {
  159.                     vCorridor(p.center.y, newRoom.center.y, p.center.x);
  160.                     hCorridor(p.center.x, newRoom.center.x, newRoom.center.y);
  161.                 }
  162.             }
  163.            
  164.             p = newRoom;
  165.            
  166.             // Actually add the room into the tile map
  167.             carveRoom(newRoom);
  168.         }
  169.     }
  170.    
  171.     public void carveRoom(Room r) {
  172.         // Pretty much merges the 'arrays'
  173.         for(int y = r.y1; y < r.y1+r.h; y++) {
  174.             for(int x = r.x1; x < r.x1+r.w; x++) {
  175.                 map[x][y] = Constants.FLOOR;
  176.             }
  177.         }
  178.     }
  179.    
  180.     public void hCorridor(int x1, int x2, int y) {
  181.         for(int x = Math.min(x1, x2); x < Math.max(x1, x2)+1; x++) {
  182.             map[x][y] = Constants.FLOOR;
  183.         }
  184.     }
  185.    
  186.     public void vCorridor(int y1, int y2, int x) {
  187.         for(int y = Math.min(y1, y2); y < Math.max(y1, y2)+1; y++) {
  188.             map[x][y] = Constants.FLOOR;
  189.         }
  190.     }
  191.    
  192.     public int getWidth() {
  193.         return w;
  194.     }
  195.    
  196.     public int getHeight() {
  197.         return h;
  198.     }
  199.    
  200.     public int[][] getRawData() {
  201.         return map;
  202.     }
  203. }
  204. ////////////////////
  205. ////////////////////
  206.  
  207. Room Class:
  208.  
  209. ////////////////////
  210. ////////////////////
  211.  
  212. import com.pickens.util.Point;
  213.  
  214. public class Room {
  215.  
  216.     private static final int ROOM_PADDING = 2;
  217.    
  218.     public int x1, x2, y1, y2, w, h;
  219.     public Point center = new Point(0, 0);
  220.    
  221.     public Room(int x, int y, int w, int h) {
  222.         this.x1 = x;
  223.         this.y1 = y;
  224.         this.x2 = x + w;
  225.         this.y2 = y + h;
  226.         this.w = w;
  227.         this.h = h;
  228.         center.x = (x1+x2)/2;
  229.         center.y = (y1+y2)/2;
  230.     }
  231.    
  232.     public boolean intersects(Room room) {
  233.         return (x1-ROOM_PADDING <= room.x2+ROOM_PADDING && x2+ROOM_PADDING >= room.x1-ROOM_PADDING &&
  234.             y1-ROOM_PADDING <= room.y2+ROOM_PADDING && room.y2-ROOM_PADDING >= room.y1+ROOM_PADDING);
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement