diff options
| author | Starla Insigna <hatkirby@fourisland.com> | 2012-06-02 19:37:27 -0400 |
|---|---|---|
| committer | Starla Insigna <hatkirby@fourisland.com> | 2012-06-02 19:37:27 -0400 |
| commit | df65f21501cf8e3de51d01fee4eab1d488bd568c (patch) | |
| tree | 8ef518b1cc3abd2093eb6d8c77ea519e67933802 /src/com/fourisland | |
| parent | 7597ae93f38aaa32b099b19a3b65b96196f735e3 (diff) | |
| download | frigidearth-df65f21501cf8e3de51d01fee4eab1d488bd568c.tar.gz frigidearth-df65f21501cf8e3de51d01fee4eab1d488bd568c.tar.bz2 frigidearth-df65f21501cf8e3de51d01fee4eab1d488bd568c.zip | |
Added basic random dungeon generator and moving viewport
Diffstat (limited to 'src/com/fourisland')
| -rw-r--r-- | src/com/fourisland/frigidearth/Main.java | 4 | ||||
| -rw-r--r-- | src/com/fourisland/frigidearth/MapViewGameState.java | 168 |
2 files changed, 147 insertions, 25 deletions
| diff --git a/src/com/fourisland/frigidearth/Main.java b/src/com/fourisland/frigidearth/Main.java index ed35f9c..40a10f7 100644 --- a/src/com/fourisland/frigidearth/Main.java +++ b/src/com/fourisland/frigidearth/Main.java | |||
| @@ -28,8 +28,8 @@ import javax.swing.JFrame; | |||
| 28 | */ | 28 | */ |
| 29 | public class Main | 29 | public class Main |
| 30 | { | 30 | { |
| 31 | public static final int GAME_WIDTH = 320; | 31 | public static final int GAME_WIDTH = 640; |
| 32 | public static final int GAME_HEIGHT = 240; | 32 | public static final int GAME_HEIGHT = 480; |
| 33 | public static final int FPS = (1000 / 60); // 60 fps | 33 | public static final int FPS = (1000 / 60); // 60 fps |
| 34 | 34 | ||
| 35 | private static JFrame mainWindow; | 35 | private static JFrame mainWindow; |
| diff --git a/src/com/fourisland/frigidearth/MapViewGameState.java b/src/com/fourisland/frigidearth/MapViewGameState.java index 2ecaeb1..e8da885 100644 --- a/src/com/fourisland/frigidearth/MapViewGameState.java +++ b/src/com/fourisland/frigidearth/MapViewGameState.java | |||
| @@ -5,8 +5,8 @@ | |||
| 5 | package com.fourisland.frigidearth; | 5 | package com.fourisland.frigidearth; |
| 6 | 6 | ||
| 7 | import java.awt.Color; | 7 | import java.awt.Color; |
| 8 | import java.awt.Font; | ||
| 9 | import java.awt.Graphics2D; | 8 | import java.awt.Graphics2D; |
| 9 | import java.awt.Rectangle; | ||
| 10 | import java.awt.event.KeyEvent; | 10 | import java.awt.event.KeyEvent; |
| 11 | import java.util.Random; | 11 | import java.util.Random; |
| 12 | 12 | ||
| @@ -18,65 +18,171 @@ public class MapViewGameState implements GameState | |||
| 18 | { | 18 | { |
| 19 | private final int TILE_WIDTH = 16; | 19 | private final int TILE_WIDTH = 16; |
| 20 | private final int TILE_HEIGHT = 16; | 20 | private final int TILE_HEIGHT = 16; |
| 21 | private final int GAME_WIDTH = Main.GAME_WIDTH / TILE_WIDTH; | 21 | private final int GAME_WIDTH = 100; |
| 22 | private final int GAME_HEIGHT = Main.GAME_HEIGHT / TILE_HEIGHT; | 22 | private final int GAME_HEIGHT = 100; |
| 23 | private Color[][] grid; | 23 | private final int VIEWPORT_WIDTH = Main.GAME_WIDTH / TILE_WIDTH; |
| 24 | private final int VIEWPORT_HEIGHT = Main.GAME_HEIGHT / TILE_HEIGHT; | ||
| 25 | private final int ROOM_MAX_SIZE = 10; | ||
| 26 | private final int ROOM_MIN_SIZE = 6; | ||
| 27 | private final int MAX_ROOMS = 30; | ||
| 28 | private boolean[][] grid; | ||
| 24 | private int playerx = 4; | 29 | private int playerx = 4; |
| 25 | private int playery = 4; | 30 | private int playery = 4; |
| 31 | private int viewportx = 0; | ||
| 32 | private int viewporty = 0; | ||
| 26 | 33 | ||
| 27 | public MapViewGameState() | 34 | public MapViewGameState() |
| 28 | { | 35 | { |
| 29 | Random r = new Random(); | 36 | grid = new boolean[GAME_WIDTH][GAME_HEIGHT]; |
| 30 | grid = new Color[GAME_WIDTH][GAME_HEIGHT]; | ||
| 31 | 37 | ||
| 32 | for (int x=0; x<GAME_WIDTH; x++) | 38 | for (int x=0; x<GAME_WIDTH; x++) |
| 33 | { | 39 | { |
| 34 | for (int y=0; y<GAME_HEIGHT; y++) | 40 | for (int y=0; y<GAME_HEIGHT; y++) |
| 35 | { | 41 | { |
| 36 | grid[x][y] = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)); | 42 | grid[x][y] = true; |
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | Rectangle[] rooms = new Rectangle[MAX_ROOMS]; | ||
| 47 | int numRooms = 0; | ||
| 48 | Random r = new Random(); | ||
| 49 | |||
| 50 | for (int i=0; i<MAX_ROOMS; i++) | ||
| 51 | { | ||
| 52 | int w = r.nextInt(ROOM_MAX_SIZE - ROOM_MIN_SIZE + 1) + ROOM_MIN_SIZE; | ||
| 53 | int h = r.nextInt(ROOM_MAX_SIZE - ROOM_MIN_SIZE + 1) + ROOM_MIN_SIZE; | ||
| 54 | int x = r.nextInt(GAME_WIDTH - w - 1); | ||
| 55 | int y = r.nextInt(GAME_HEIGHT - h - 1); | ||
| 56 | Rectangle newRoom = new Rectangle(x, y, w, h); | ||
| 57 | boolean failed = false; | ||
| 58 | |||
| 59 | for (Rectangle room : rooms) | ||
| 60 | { | ||
| 61 | if ((room != null) && (newRoom.intersects(room))) | ||
| 62 | { | ||
| 63 | failed = true; | ||
| 64 | break; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | if (!failed) | ||
| 69 | { | ||
| 70 | createRoom(newRoom); | ||
| 71 | |||
| 72 | int newX = (int) newRoom.getCenterX(); | ||
| 73 | int newY = (int) newRoom.getCenterY(); | ||
| 74 | |||
| 75 | if (numRooms == 0) | ||
| 76 | { | ||
| 77 | playerx = newX; | ||
| 78 | playery = newY; | ||
| 79 | |||
| 80 | adjustViewport(); | ||
| 81 | } else { | ||
| 82 | int prevX = (int) rooms[numRooms-1].getCenterX(); | ||
| 83 | int prevY = (int) rooms[numRooms-1].getCenterY(); | ||
| 84 | |||
| 85 | if (r.nextBoolean()) | ||
| 86 | { | ||
| 87 | createHTunnel(prevX, newX, prevY); | ||
| 88 | createVTunnel(prevY, newY, newX); | ||
| 89 | } else { | ||
| 90 | createVTunnel(prevY, newY, prevX); | ||
| 91 | createHTunnel(prevX, newX, newY); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | rooms[numRooms++] = newRoom; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | private void createRoom(Rectangle room) | ||
| 101 | { | ||
| 102 | for (int x=room.x+1; x<room.x+room.width; x++) | ||
| 103 | { | ||
| 104 | for (int y=room.y+1; y<room.y+room.height; y++) | ||
| 105 | { | ||
| 106 | grid[x][y] = false; | ||
| 37 | } | 107 | } |
| 38 | } | 108 | } |
| 39 | } | 109 | } |
| 110 | |||
| 111 | private void createHTunnel(int x1, int x2, int y) | ||
| 112 | { | ||
| 113 | if (x2 < x1) | ||
| 114 | { | ||
| 115 | int temp = x1; | ||
| 116 | x1 = x2; | ||
| 117 | x2 = temp; | ||
| 118 | } | ||
| 119 | |||
| 120 | for (int x=x1; x<x2+1; x++) | ||
| 121 | { | ||
| 122 | grid[x][y] = false; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | private void createVTunnel(int y1, int y2, int x) | ||
| 127 | { | ||
| 128 | if (y2 < y1) | ||
| 129 | { | ||
| 130 | int temp = y1; | ||
| 131 | y1 = y2; | ||
| 132 | y2 = temp; | ||
| 133 | } | ||
| 134 | |||
| 135 | for (int y=y1; y<y2+1; y++) | ||
| 136 | { | ||
| 137 | grid[x][y] = false; | ||
| 138 | } | ||
| 139 | } | ||
| 40 | 140 | ||
| 41 | public void render(Graphics2D g) | 141 | public void render(Graphics2D g) |
| 42 | { | 142 | { |
| 43 | // Render tiles | 143 | // Render tiles |
| 44 | for (int x=0; x<GAME_WIDTH; x++) | 144 | for (int x=viewportx; x<viewportx+VIEWPORT_WIDTH; x++) |
| 45 | { | 145 | { |
| 46 | for (int y=0; y<GAME_HEIGHT; y++) | 146 | for (int y=viewporty; y<viewporty+VIEWPORT_HEIGHT; y++) |
| 47 | { | 147 | { |
| 48 | g.setColor(grid[x][y]); | 148 | if (grid[x][y]) |
| 49 | g.fillRect(x*TILE_WIDTH, y*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); | 149 | { |
| 150 | g.setColor(Color.GRAY); | ||
| 151 | } else { | ||
| 152 | g.setColor(Color.BLACK); | ||
| 153 | } | ||
| 154 | |||
| 155 | g.fillRect((x-viewportx)*TILE_WIDTH, (y-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); | ||
| 50 | } | 156 | } |
| 51 | } | 157 | } |
| 52 | 158 | ||
| 53 | // Render player | 159 | // Render player |
| 54 | g.drawImage(SystemFont.getCharacter('@'), playerx*TILE_WIDTH, playery*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); | 160 | g.drawImage(SystemFont.getCharacter('@'), (playerx-viewportx)*TILE_WIDTH, (playery-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null); |
| 55 | } | 161 | } |
| 56 | 162 | ||
| 57 | public void processInput(KeyEvent e) | 163 | public void processInput(KeyEvent e) |
| 58 | { | 164 | { |
| 59 | if (e.getKeyCode() == KeyEvent.VK_LEFT) | 165 | if (e.getKeyCode() == KeyEvent.VK_LEFT) |
| 60 | { | 166 | { |
| 61 | if (playerx > 0) | 167 | if ((playerx > 0) && (!grid[playerx-1][playery])) |
| 62 | { | 168 | { |
| 63 | playerx--; | 169 | playerx--; |
| 64 | } | 170 | } |
| 65 | } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) | 171 | } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) |
| 66 | { | 172 | { |
| 67 | if (playerx < GAME_WIDTH - 1) | 173 | if ((playerx < GAME_WIDTH - 1) && (!grid[playerx+1][playery])) |
| 68 | { | 174 | { |
| 69 | playerx++; | 175 | playerx++; |
| 70 | } | 176 | } |
| 71 | } else if (e.getKeyCode() == KeyEvent.VK_UP) | 177 | } else if (e.getKeyCode() == KeyEvent.VK_UP) |
| 72 | { | 178 | { |
| 73 | if (playery > 0) | 179 | if ((playery > 0) && (!grid[playerx][playery-1])) |
| 74 | { | 180 | { |
| 75 | playery--; | 181 | playery--; |
| 76 | } | 182 | } |
| 77 | } else if (e.getKeyCode() == KeyEvent.VK_DOWN) | 183 | } else if (e.getKeyCode() == KeyEvent.VK_DOWN) |
| 78 | { | 184 | { |
| 79 | if (playery < GAME_HEIGHT - 1) | 185 | if ((playery < GAME_HEIGHT - 1) && (!grid[playerx][playery+1])) |
| 80 | { | 186 | { |
| 81 | playery++; | 187 | playery++; |
| 82 | } | 188 | } |
| @@ -84,17 +190,33 @@ public class MapViewGameState implements GameState | |||
| 84 | return; | 190 | return; |
| 85 | } | 191 | } |
| 86 | 192 | ||
| 87 | Random r = new Random(); | 193 | adjustViewport(); |
| 194 | } | ||
| 195 | |||
| 196 | private void adjustViewport() | ||
| 197 | { | ||
| 198 | if (playerx > (VIEWPORT_WIDTH/2)) | ||
| 199 | { | ||
| 200 | if (playerx < (GAME_WIDTH - (VIEWPORT_WIDTH/2-1))) | ||
| 201 | { | ||
| 202 | viewportx = playerx - (VIEWPORT_WIDTH/2); | ||
| 203 | } else { | ||
| 204 | viewportx = GAME_WIDTH - VIEWPORT_WIDTH; | ||
| 205 | } | ||
| 206 | } else { | ||
| 207 | viewportx = 0; | ||
| 208 | } | ||
| 88 | 209 | ||
| 89 | if (r.nextBoolean()) | 210 | if (playery > (VIEWPORT_HEIGHT/2)) |
| 90 | { | 211 | { |
| 91 | for (int x=0; x<GAME_WIDTH; x++) | 212 | if (playery < (GAME_HEIGHT - (VIEWPORT_HEIGHT/2-1))) |
| 92 | { | 213 | { |
| 93 | for (int y=0; y<GAME_HEIGHT; y++) | 214 | viewporty = playery - (VIEWPORT_HEIGHT/2); |
| 94 | { | 215 | } else { |
| 95 | grid[x][y] = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256)); | 216 | viewporty = GAME_HEIGHT - VIEWPORT_HEIGHT; |
| 96 | } | ||
| 97 | } | 217 | } |
| 218 | } else { | ||
| 219 | viewporty = 0; | ||
| 98 | } | 220 | } |
| 99 | } | 221 | } |
| 100 | } | 222 | } |
