Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////
- ////////////////////
- TileMap Class:
- ////////////////////
- ////////////////////
- import java.util.ArrayList;
- import java.util.Random;
- import org.newdawn.slick.GameContainer;
- import org.newdawn.slick.Graphics;
- import com.pickens.util.Constants;
- import com.pickens.util.Viewport;
- public class TileMap {
- Random r = new Random();
- ArrayList<Room> rooms = new ArrayList<Room>();
- private int w, h;
- private Tile[][] tile;
- private int[][] map;
- private int numberOfRooms;
- public TileMap(int w, int h, int numberOfRooms) {
- this.w = w;
- this.h = h;
- this.numberOfRooms = numberOfRooms;
- generateMap(w, h);
- tile = new Tile[w][h];
- for(int y = 0; y < h; y++) {
- for(int x = 0; x < w; x++) {
- tile[x][y] = new Tile(x*Constants.TILE_SIZE, y*Constants.TILE_SIZE, map[x][y]);
- }
- }
- }
- public void render(int ox, int oy, Graphics g) {
- for(int y = 0; y < h; y++) {
- for(int x = 0; x < w; x++) {
- tile[x][y].render(ox, oy, g);
- }
- }
- }
- public void update(GameContainer gc, int delta) {
- }
- int i = 0;
- Room p = null;
- public void generateMap(int w, int h) {
- map = new int[w][h];
- for(int y = 0; y < h; y++) {
- for(int x = 0; x < w; x++) { // Makes array of empty tiles
- map[x][y] = Constants.EMPTY;
- }
- }
- createRoom(w/2, h/2); // Create the starting room in the center of the map and branch off from there
- // Create Rooms
- for(i = 0; i < numberOfRooms; i++) {
- createRoom(); // Create the rest of the rooms randomly on the map
- }
- // Add pretty tiles
- // Add walls to floors
- for(int y = 0; y < h; y++) {
- for(int x = 0; x < w; x++) {
- if(map[x][y] == Constants.FLOOR) {
- if(x > 0 && map[x-1][y] == Constants.EMPTY) {
- map[x-1][y] = Constants.WALL;
- }
- if(x < w && map[x+1][y] == Constants.EMPTY) {
- map[x+1][y] = Constants.WALL;
- }
- if(y > 0 && map[x][y-1] == Constants.EMPTY) {
- map[x][y-1] = Constants.WALL;
- }
- if(y < h && map[x][y+1] == Constants.EMPTY) {
- map[x][y+1] = Constants.WALL;
- }
- }
- }
- }
- }
- public void createRoom() {
- /*int ww = w/numberOfRooms/2 + r.nextInt(w/numberOfRooms/2);
- int hh = h/numberOfRooms/2 + r.nextInt(h/numberOfRooms/2);*/
- int ww = 5 + r.nextInt(5);
- int hh = 5 + r.nextInt(5);
- int x = r.nextInt(w - ww);
- int y = r.nextInt(h - hh);
- Room newRoom = new Room(x, y, ww, hh);
- boolean failed = false;
- for(int ii = 0; ii < rooms.size(); ii++) {
- Room otherRoom = rooms.get(ii);
- if(newRoom.intersects(otherRoom)) {
- failed = true;
- i--;
- break;
- }
- }
- if(!failed) {
- rooms.add(newRoom);
- if(p != null) {
- if(r.nextInt(2) == 0) {
- hCorridor(p.center.x, newRoom.center.x, p.center.y);
- vCorridor(p.center.y, newRoom.center.y, newRoom.center.x);
- } else {
- vCorridor(p.center.y, newRoom.center.y, p.center.x);
- hCorridor(p.center.x, newRoom.center.x, newRoom.center.y);
- }
- }
- p = newRoom;
- carveRoom(newRoom);
- }
- }
- public void createRoom(int xx, int yy) {
- // Random widths and heights
- int ww = 5 + r.nextInt(5);
- int hh = 5 + r.nextInt(5);
- // Getting the x and y of the room
- int x = xx-ww/2;
- int y = yy-hh/2;
- Room newRoom = new Room(x, y, ww, hh);
- boolean failed = false;
- // Loops through all rooms and checks to see if the current room intersects any of them
- for(int ii = 0; ii < rooms.size(); ii++) {
- Room otherRoom = rooms.get(ii);
- if(newRoom.intersects(otherRoom)) {
- failed = true;
- i--;
- break;
- }
- }
- if(!failed) {
- // If there is no intersection
- rooms.add(newRoom);
- // Make corridor connecting this room to last based on the center position of
- // each room.
- if(p != null) {
- if(r.nextInt(2) == 0) {
- hCorridor(p.center.x, newRoom.center.x, p.center.y);
- vCorridor(p.center.y, newRoom.center.y, newRoom.center.x);
- } else {
- vCorridor(p.center.y, newRoom.center.y, p.center.x);
- hCorridor(p.center.x, newRoom.center.x, newRoom.center.y);
- }
- }
- p = newRoom;
- // Actually add the room into the tile map
- carveRoom(newRoom);
- }
- }
- public void carveRoom(Room r) {
- // Pretty much merges the 'arrays'
- for(int y = r.y1; y < r.y1+r.h; y++) {
- for(int x = r.x1; x < r.x1+r.w; x++) {
- map[x][y] = Constants.FLOOR;
- }
- }
- }
- public void hCorridor(int x1, int x2, int y) {
- for(int x = Math.min(x1, x2); x < Math.max(x1, x2)+1; x++) {
- map[x][y] = Constants.FLOOR;
- }
- }
- public void vCorridor(int y1, int y2, int x) {
- for(int y = Math.min(y1, y2); y < Math.max(y1, y2)+1; y++) {
- map[x][y] = Constants.FLOOR;
- }
- }
- public int getWidth() {
- return w;
- }
- public int getHeight() {
- return h;
- }
- public int[][] getRawData() {
- return map;
- }
- }
- ////////////////////
- ////////////////////
- Room Class:
- ////////////////////
- ////////////////////
- import com.pickens.util.Point;
- public class Room {
- private static final int ROOM_PADDING = 2;
- public int x1, x2, y1, y2, w, h;
- public Point center = new Point(0, 0);
- public Room(int x, int y, int w, int h) {
- this.x1 = x;
- this.y1 = y;
- this.x2 = x + w;
- this.y2 = y + h;
- this.w = w;
- this.h = h;
- center.x = (x1+x2)/2;
- center.y = (y1+y2)/2;
- }
- public boolean intersects(Room room) {
- return (x1-ROOM_PADDING <= room.x2+ROOM_PADDING && x2+ROOM_PADDING >= room.x1-ROOM_PADDING &&
- y1-ROOM_PADDING <= room.y2+ROOM_PADDING && room.y2-ROOM_PADDING >= room.y1+ROOM_PADDING);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement