From fa27ef18695e007deeaf8d2a372e85c13d54764a Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sat, 2 Jun 2012 21:45:40 -0400 Subject: Replaced basic dungeon generator with a much cooler one Also added support for more than two types of tiles, which are represented by characters. Later I'll add the functionality for different coloring of characters, because the bright Xs are kind of annoying. --- src/com/fourisland/frigidearth/Functions.java | 21 + .../fourisland/frigidearth/MapViewGameState.java | 493 +++++++++++++++++---- src/com/fourisland/frigidearth/Tile.java | 104 +++++ 3 files changed, 537 insertions(+), 81 deletions(-) create mode 100644 src/com/fourisland/frigidearth/Functions.java create mode 100644 src/com/fourisland/frigidearth/Tile.java diff --git a/src/com/fourisland/frigidearth/Functions.java b/src/com/fourisland/frigidearth/Functions.java new file mode 100644 index 0000000..cb57086 --- /dev/null +++ b/src/com/fourisland/frigidearth/Functions.java @@ -0,0 +1,21 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.frigidearth; + +import java.util.Random; + +/** + * + * @author hatkirby + */ +public class Functions +{ + public static int random(int min, int max) + { + Random r = new Random(); + + return r.nextInt(max - min + 1) + min; + } +} diff --git a/src/com/fourisland/frigidearth/MapViewGameState.java b/src/com/fourisland/frigidearth/MapViewGameState.java index e8da885..f378d5a 100644 --- a/src/com/fourisland/frigidearth/MapViewGameState.java +++ b/src/com/fourisland/frigidearth/MapViewGameState.java @@ -4,11 +4,8 @@ */ package com.fourisland.frigidearth; -import java.awt.Color; import java.awt.Graphics2D; -import java.awt.Rectangle; import java.awt.event.KeyEvent; -import java.util.Random; /** * @@ -22,10 +19,13 @@ public class MapViewGameState implements GameState private final int GAME_HEIGHT = 100; private final int VIEWPORT_WIDTH = Main.GAME_WIDTH / TILE_WIDTH; private final int VIEWPORT_HEIGHT = Main.GAME_HEIGHT / TILE_HEIGHT; - private final int ROOM_MAX_SIZE = 10; - private final int ROOM_MIN_SIZE = 6; - private final int MAX_ROOMS = 30; - private boolean[][] grid; + private final int MAX_ROOM_WIDTH = 13; + private final int MIN_ROOM_WIDTH = 7; + private final int MAX_ROOM_HEIGHT = 13; + private final int MIN_ROOM_HEIGHT = 7; + private final int MAX_CORRIDOR_LENGTH = 6; + private final int MIN_CORRIDOR_LENGTH = 2; + private Tile[][] grid; private int playerx = 4; private int playery = 4; private int viewportx = 0; @@ -33,109 +33,447 @@ public class MapViewGameState implements GameState public MapViewGameState() { - grid = new boolean[GAME_WIDTH][GAME_HEIGHT]; + grid = new Tile[GAME_WIDTH][GAME_HEIGHT]; for (int x=0; x -1) + { + if (grid[newx][newy+1] == Tile.Door) + { + validTile = -1; + } else if (grid[newx-1][newy] == Tile.Door) + { + validTile = -1; + } else if (grid[newx][newy-1] == Tile.Door) + { + validTile = -1; + } else if (grid[newx+1][newy] == Tile.Door) + { + validTile = -1; + } + } - if (r.nextBoolean()) + if (validTile > -1) { - createHTunnel(prevX, newX, prevY); - createVTunnel(prevY, newY, newX); - } else { - createVTunnel(prevY, newY, prevX); - createHTunnel(prevX, newX, newY); + break; } } - - rooms[numRooms++] = newRoom; } - } - } - - private void createRoom(Rectangle room) - { - for (int x=room.x+1; x -1) { - grid[x][y] = false; + if (Functions.random(0, 100) <= 75) + { + if (makeRoom(newx+xmod, newy+ymod, validTile)) + { + currentFeatures++; + grid[newx][newy] = Tile.Door; + grid[newx+xmod][newy+ymod] = Tile.DirtFloor; + } + } else { + if (makeCorridor(newx+xmod, newy+ymod, validTile)) + { + currentFeatures++; + grid[newx][newy] = Tile.Door; + } + } } } } - private void createHTunnel(int x1, int x2, int y) + private boolean makeRoom(int x, int y, int direction) { - if (x2 < x1) + int width = Functions.random(MIN_ROOM_WIDTH, MAX_ROOM_WIDTH); + int height = Functions.random(MIN_ROOM_HEIGHT, MAX_ROOM_HEIGHT); + Tile floor = Tile.DirtFloor; + Tile wall = Tile.DirtWall; + int dir = 0; + + if ((direction > 0) && (direction < 4)) { - int temp = x1; - x1 = x2; - x2 = temp; + dir = direction; } - for (int x=x1; x (y-height); ytemp--) + { + if ((ytemp < 0) || (ytemp > GAME_HEIGHT)) + { + return false; + } + + for (int xtemp=(x-width/2); xtemp < (x+(width+1)/2); xtemp++) + { + if ((xtemp < 0) || (xtemp > GAME_WIDTH)) + { + return false; + } + + if (grid[xtemp][ytemp] != Tile.Unused) + { + return false; + } + } + } + + for (int ytemp = y; ytemp > (y-height); ytemp--) + { + for (int xtemp = (x-width/2); xtemp < (x+(width+1)/2); xtemp++) + { + if (xtemp == (x-width/2)) + { + grid[xtemp][ytemp] = wall; + } else if (xtemp == (x+(width-1)/2)) + { + grid[xtemp][ytemp] = wall; + } else if (ytemp == y) + { + grid[xtemp][ytemp] = wall; + } else if (ytemp == (y-height+1)) + { + grid[xtemp][ytemp] = wall; + } else { + grid[xtemp][ytemp] = floor; + } + } + } + + break; + + case 1: // East + for (int ytemp=(y-height/2); ytemp < (y+(height+1)/2); ytemp++) + { + if ((ytemp < 0) || (ytemp > GAME_HEIGHT)) + { + return false; + } + + for (int xtemp=x; xtemp < (x+width); xtemp++) + { + if ((xtemp < 0) || (xtemp > GAME_WIDTH)) + { + return false; + } + + if (grid[xtemp][ytemp] != Tile.Unused) + { + return false; + } + } + } + + for (int ytemp=(y-height/2); ytemp < (y+(height+1)/2); ytemp++) + { + for (int xtemp=x; xtemp < (x+width); xtemp++) + { + if (xtemp == x) + { + grid[xtemp][ytemp] = wall; + } else if (xtemp == (x+width-1)) + { + grid[xtemp][ytemp] = wall; + } else if (ytemp == (y-height/2)) + { + grid[xtemp][ytemp] = wall; + } else if (ytemp == (y+(height-1)/2)) + { + grid[xtemp][ytemp] = wall; + } else { + grid[xtemp][ytemp] = floor; + } + } + } + + break; + + case 2: // South + for (int ytemp=y; ytemp < (y+height); ytemp++) + { + if ((ytemp < 0) || (ytemp > GAME_HEIGHT)) + { + return false; + } + + for (int xtemp=(x-width/2); xtemp < (x+(width+1)/2); xtemp++) + { + if ((xtemp < 0) || (xtemp > GAME_WIDTH)) + { + return false; + } + + if (grid[xtemp][ytemp] != Tile.Unused) + { + return false; + } + } + } + + for (int ytemp=y; ytemp < (y+height); ytemp++) + { + for (int xtemp = (x-width/2); xtemp < (x+(width+1)/2); xtemp++) + { + if (xtemp == (x-width/2)) + { + grid[xtemp][ytemp] = wall; + } else if (xtemp == (x+(width-1)/2)) + { + grid[xtemp][ytemp] = wall; + } else if (ytemp == y) + { + grid[xtemp][ytemp] = wall; + } else if (ytemp == (y+height-1)) + { + grid[xtemp][ytemp] = wall; + } else { + grid[xtemp][ytemp] = floor; + } + } + } + + break; + + case 3: // West + for (int ytemp=(y-height/2); ytemp < (y+(height+1)/2); ytemp++) + { + if ((ytemp < 0) || (ytemp > GAME_HEIGHT)) + { + return false; + } + + for (int xtemp=x; xtemp > (x-width); xtemp--) + { + if ((xtemp < 0) || (xtemp > GAME_WIDTH)) + { + return false; + } + + if (grid[xtemp][ytemp] != Tile.Unused) + { + return false; + } + } + } + + for (int ytemp=(y-height/2); ytemp < (y+(height+1)/2); ytemp++) + { + for (int xtemp=x; xtemp > (x-width); xtemp--) + { + if (xtemp == x) + { + grid[xtemp][ytemp] = wall; + } else if (xtemp == (x-width+1)) + { + grid[xtemp][ytemp] = wall; + } else if (ytemp == (y-height/2)) + { + grid[xtemp][ytemp] = wall; + } else if (ytemp == (y+(height-1)/2)) + { + grid[xtemp][ytemp] = wall; + } else { + grid[xtemp][ytemp] = floor; + } + } + } + + break; } + + return true; } - private void createVTunnel(int y1, int y2, int x) + private boolean makeCorridor(int x, int y, int direction) { - if (y2 < y1) + int length = Functions.random(MIN_CORRIDOR_LENGTH, MAX_CORRIDOR_LENGTH); + Tile floor = Tile.Corridor; + int dir = 0; + if ((direction > 0) && (direction < 4)) { - int temp = y1; - y1 = y2; - y2 = temp; + dir = direction; } - for (int y=y1; y GAME_WIDTH)) + { + return false; + } else { + xtemp = x; + } + + for (ytemp = y; ytemp > (y-length); ytemp--) + { + if ((ytemp < 0) || (ytemp > GAME_HEIGHT)) + { + return false; + } + + if (grid[xtemp][ytemp] != Tile.Unused) + { + return false; + } + } + + for (ytemp = y; ytemp > (y-length); ytemp--) + { + grid[xtemp][ytemp] = floor; + } + + break; + + case 1: // East + if ((y < 0) || (y > GAME_HEIGHT)) + { + return false; + } else { + ytemp = y; + } + + for (xtemp = x; xtemp < (x+length); xtemp++) + { + if ((xtemp < 0) || (xtemp > GAME_WIDTH)) + { + return false; + } + + if (grid[xtemp][ytemp] != Tile.Unused) + { + return false; + } + } + + for (xtemp = x; xtemp < (x+length); xtemp++) + { + grid[xtemp][ytemp] = floor; + } + + break; + + case 2: // South + if ((x < 0) || (x > GAME_WIDTH)) + { + return false; + } else { + xtemp = x; + } + + for (ytemp = y; ytemp < (y+length); ytemp++) + { + if ((ytemp < 0) || (ytemp > GAME_HEIGHT)) + { + return false; + } + + if (grid[xtemp][ytemp] != Tile.Unused) + { + return false; + } + } + + for (ytemp = y; ytemp < (y+length); ytemp++) + { + grid[xtemp][ytemp] = floor; + } + + break; + + case 3: // West + if ((y < 0) || (y > GAME_HEIGHT)) + { + return false; + } else { + ytemp = y; + } + + for (xtemp = x; xtemp > (x-length); xtemp--) + { + if ((xtemp < 0) || (xtemp > GAME_WIDTH)) + { + return false; + } + + if (grid[xtemp][ytemp] != Tile.Unused) + { + return false; + } + } + + for (xtemp = x; xtemp > (x-length); xtemp--) + { + grid[xtemp][ytemp] = floor; + } + + break; } + + return true; } public void render(Graphics2D g) @@ -145,14 +483,7 @@ public class MapViewGameState implements GameState { for (int y=viewporty; y 0) && (!grid[playerx-1][playery])) + if ((playerx > 0) && (!grid[playerx-1][playery].isBlocked())) { playerx--; } } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { - if ((playerx < GAME_WIDTH - 1) && (!grid[playerx+1][playery])) + if ((playerx < GAME_WIDTH - 1) && (!grid[playerx+1][playery].isBlocked())) { playerx++; } } else if (e.getKeyCode() == KeyEvent.VK_UP) { - if ((playery > 0) && (!grid[playerx][playery-1])) + if ((playery > 0) && (!grid[playerx][playery-1].isBlocked())) { playery--; } } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { - if ((playery < GAME_HEIGHT - 1) && (!grid[playerx][playery+1])) + if ((playery < GAME_HEIGHT - 1) && (!grid[playerx][playery+1].isBlocked())) { playery++; } diff --git a/src/com/fourisland/frigidearth/Tile.java b/src/com/fourisland/frigidearth/Tile.java new file mode 100644 index 0000000..e25cedd --- /dev/null +++ b/src/com/fourisland/frigidearth/Tile.java @@ -0,0 +1,104 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.fourisland.frigidearth; + +/** + * + * @author hatkirby + */ +public enum Tile +{ + Unused { + public boolean isBlocked() + { + return true; + } + + public char getDisplayCharacter() + { + return 'X'; + } + }, + DirtWall { + public boolean isBlocked() + { + return true; + } + + public char getDisplayCharacter() + { + return '#'; + } + }, + DirtFloor { + public boolean isBlocked() + { + return false; + } + + public char getDisplayCharacter() + { + return ' '; + } + }, + StoneWall { + public boolean isBlocked() + { + return true; + } + + public char getDisplayCharacter() + { + return 'X'; + } + }, + Corridor { + public boolean isBlocked() + { + return false; + } + + public char getDisplayCharacter() + { + return '.'; + } + }, + Door { + public boolean isBlocked() + { + return false; + } + + public char getDisplayCharacter() + { + return 'D'; + } + }, + UpStairs { + public boolean isBlocked() + { + return false; + } + + public char getDisplayCharacter() + { + return '>'; + } + }, + DownStairs { + public boolean isBlocked() + { + return false; + } + + public char getDisplayCharacter() + { + return '<'; + } + }; + + public abstract boolean isBlocked(); + public abstract char getDisplayCharacter(); +} -- cgit 1.4.1