about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/com/fourisland/frigidearth/Direction.java31
-rw-r--r--src/com/fourisland/frigidearth/MapViewGameState.java278
-rw-r--r--src/com/fourisland/frigidearth/Room.java52
3 files changed, 159 insertions, 202 deletions
diff --git a/src/com/fourisland/frigidearth/Direction.java b/src/com/fourisland/frigidearth/Direction.java new file mode 100644 index 0000000..3c24a89 --- /dev/null +++ b/src/com/fourisland/frigidearth/Direction.java
@@ -0,0 +1,31 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.fourisland.frigidearth;
6
7/**
8 *
9 * @author hatkirby
10 */
11public enum Direction
12{
13 North,
14 East,
15 South,
16 West;
17
18 public static Direction getRandomDirection()
19 {
20 int r = Functions.random(0, 3);
21
22 switch (r)
23 {
24 case 0: return North;
25 case 1: return East;
26 case 2: return South;
27 case 3: return West;
28 default: return null; // This can't happen
29 }
30 }
31}
diff --git a/src/com/fourisland/frigidearth/MapViewGameState.java b/src/com/fourisland/frigidearth/MapViewGameState.java index aa489da..43d61b7 100644 --- a/src/com/fourisland/frigidearth/MapViewGameState.java +++ b/src/com/fourisland/frigidearth/MapViewGameState.java
@@ -7,6 +7,8 @@ package com.fourisland.frigidearth;
7import java.awt.Color; 7import java.awt.Color;
8import java.awt.Graphics2D; 8import java.awt.Graphics2D;
9import java.awt.event.KeyEvent; 9import java.awt.event.KeyEvent;
10import java.util.ArrayList;
11import java.util.List;
10 12
11/** 13/**
12 * 14 *
@@ -29,6 +31,7 @@ public class MapViewGameState implements GameState
29 private final int[][] OCTET_MULTIPLIERS = new int[][] {new int[] {1,0,0,-1,-1,0,0,1}, new int[] {0,1,-1,0,0,-1,1,0}, new int[] {0,1,1,0,0,-1,-1,0}, new int[] {1,0,0,1,-1,0,0,-1}}; 31 private final int[][] OCTET_MULTIPLIERS = new int[][] {new int[] {1,0,0,-1,-1,0,0,1}, new int[] {0,1,-1,0,0,-1,1,0}, new int[] {0,1,1,0,0,-1,-1,0}, new int[] {1,0,0,1,-1,0,0,-1}};
30 private Tile[][] grid; 32 private Tile[][] grid;
31 private boolean[][] gridLighting; 33 private boolean[][] gridLighting;
34 private List<Room> rooms = new ArrayList<Room>();
32 private int playerx = 4; 35 private int playerx = 4;
33 private int playery = 4; 36 private int playery = 4;
34 private int viewportx = 0; 37 private int viewportx = 0;
@@ -52,7 +55,7 @@ public class MapViewGameState implements GameState
52 } 55 }
53 } 56 }
54 57
55 makeRoom(GAME_WIDTH/2, GAME_HEIGHT/2, Functions.random(0, 3)); 58 makeRoom(GAME_WIDTH/2, GAME_HEIGHT/2, Direction.getRandomDirection());
56 playerx = GAME_WIDTH/2; 59 playerx = GAME_WIDTH/2;
57 playery = GAME_HEIGHT/2; 60 playery = GAME_HEIGHT/2;
58 61
@@ -70,62 +73,62 @@ public class MapViewGameState implements GameState
70 int xmod = 0; 73 int xmod = 0;
71 int newy = 0; 74 int newy = 0;
72 int ymod = 0; 75 int ymod = 0;
73 int validTile = -1; 76 Direction validTile = null;
74 for (int testing = 0; testing < 1000; testing++) 77 for (int testing = 0; testing < 1000; testing++)
75 { 78 {
76 newx = Functions.random(1, GAME_WIDTH-1); 79 newx = Functions.random(1, GAME_WIDTH-1);
77 newy = Functions.random(1, GAME_HEIGHT-1); 80 newy = Functions.random(1, GAME_HEIGHT-1);
78 validTile = -1; 81 validTile = null;
79 82
80 if ((grid[newx][newy] == Tile.DirtWall) || (grid[newx][newy] == Tile.Corridor)) 83 if ((grid[newx][newy] == Tile.DirtWall) || (grid[newx][newy] == Tile.Corridor))
81 { 84 {
82 if ((grid[newx][newy+1] == Tile.DirtFloor) || (grid[newx][newy+1] == Tile.Corridor)) 85 if ((grid[newx][newy+1] == Tile.DirtFloor) || (grid[newx][newy+1] == Tile.Corridor))
83 { 86 {
84 validTile = 0; 87 validTile = Direction.North;
85 xmod = 0; 88 xmod = 0;
86 ymod = -1; 89 ymod = -1;
87 } else if ((grid[newx-1][newy] == Tile.DirtFloor) || (grid[newx-1][newy] == Tile.Corridor)) 90 } else if ((grid[newx-1][newy] == Tile.DirtFloor) || (grid[newx-1][newy] == Tile.Corridor))
88 { 91 {
89 validTile = 1; 92 validTile = Direction.East;
90 xmod = 1; 93 xmod = 1;
91 ymod = 0; 94 ymod = 0;
92 } else if ((grid[newx][newy-1] == Tile.DirtFloor) || (grid[newx][newy-1] == Tile.Corridor)) 95 } else if ((grid[newx][newy-1] == Tile.DirtFloor) || (grid[newx][newy-1] == Tile.Corridor))
93 { 96 {
94 validTile = 2; 97 validTile = Direction.South;
95 xmod = 0; 98 xmod = 0;
96 ymod = 1; 99 ymod = 1;
97 } else if ((grid[newx+1][newy] == Tile.DirtFloor) || (grid[newx+1][newy] == Tile.Corridor)) 100 } else if ((grid[newx+1][newy] == Tile.DirtFloor) || (grid[newx+1][newy] == Tile.Corridor))
98 { 101 {
99 validTile = 3; 102 validTile = Direction.West;
100 xmod = -1; 103 xmod = -1;
101 ymod = 0; 104 ymod = 0;
102 } 105 }
103 106
104 if (validTile > -1) 107 if (validTile != null)
105 { 108 {
106 if (grid[newx][newy+1] == Tile.Door) 109 if (grid[newx][newy+1] == Tile.Door)
107 { 110 {
108 validTile = -1; 111 validTile = null;
109 } else if (grid[newx-1][newy] == Tile.Door) 112 } else if (grid[newx-1][newy] == Tile.Door)
110 { 113 {
111 validTile = -1; 114 validTile = null;
112 } else if (grid[newx][newy-1] == Tile.Door) 115 } else if (grid[newx][newy-1] == Tile.Door)
113 { 116 {
114 validTile = -1; 117 validTile = null;
115 } else if (grid[newx+1][newy] == Tile.Door) 118 } else if (grid[newx+1][newy] == Tile.Door)
116 { 119 {
117 validTile = -1; 120 validTile = null;
118 } 121 }
119 } 122 }
120 123
121 if (validTile > -1) 124 if (validTile != null)
122 { 125 {
123 break; 126 break;
124 } 127 }
125 } 128 }
126 } 129 }
127 130
128 if (validTile > -1) 131 if (validTile != null)
129 { 132 {
130 if (Functions.random(0, 100) <= 75) 133 if (Functions.random(0, 100) <= 75)
131 { 134 {
@@ -146,225 +149,96 @@ public class MapViewGameState implements GameState
146 } 149 }
147 } 150 }
148 151
149 private boolean makeRoom(int x, int y, int direction) 152 private boolean makeRoom(int x, int y, Direction direction)
150 { 153 {
151 int width = Functions.random(MIN_ROOM_WIDTH, MAX_ROOM_WIDTH); 154 int width = Functions.random(MIN_ROOM_WIDTH, MAX_ROOM_WIDTH);
152 int height = Functions.random(MIN_ROOM_HEIGHT, MAX_ROOM_HEIGHT); 155 int height = Functions.random(MIN_ROOM_HEIGHT, MAX_ROOM_HEIGHT);
153 Tile floor = Tile.DirtFloor; 156 Tile floor = Tile.DirtFloor;
154 Tile wall = Tile.DirtWall; 157 Tile wall = Tile.DirtWall;
155 int dir = 0; 158 Room room = null;
156 159
157 if ((direction > 0) && (direction < 4)) 160 switch (direction)
158 { 161 {
159 dir = direction; 162 case North:
160 } 163 room = new Room(x-width/2, y-height, width+1, height+1, true);
161
162 switch (dir)
163 {
164 case 0: // North
165 for (int ytemp=y; ytemp > (y-height); ytemp--)
166 {
167 if ((ytemp < 0) || (ytemp > GAME_HEIGHT))
168 {
169 return false;
170 }
171
172 for (int xtemp=(x-width/2); xtemp < (x+(width+1)/2); xtemp++)
173 {
174 if ((xtemp < 0) || (xtemp > GAME_WIDTH))
175 {
176 return false;
177 }
178
179 if (grid[xtemp][ytemp] != Tile.Unused)
180 {
181 return false;
182 }
183 }
184 }
185
186 for (int ytemp = y; ytemp > (y-height); ytemp--)
187 {
188 for (int xtemp = (x-width/2); xtemp < (x+(width+1)/2); xtemp++)
189 {
190 if (xtemp == (x-width/2))
191 {
192 grid[xtemp][ytemp] = wall;
193 } else if (xtemp == (x+(width-1)/2))
194 {
195 grid[xtemp][ytemp] = wall;
196 } else if (ytemp == y)
197 {
198 grid[xtemp][ytemp] = wall;
199 } else if (ytemp == (y-height+1))
200 {
201 grid[xtemp][ytemp] = wall;
202 } else {
203 grid[xtemp][ytemp] = floor;
204 }
205 }
206 }
207 164
208 break; 165 break;
209 166
210 case 1: // East 167 case East:
211 for (int ytemp=(y-height/2); ytemp < (y+(height+1)/2); ytemp++) 168 room = new Room(x, y-height/2, width+1, height+1, true);
212 {
213 if ((ytemp < 0) || (ytemp > GAME_HEIGHT))
214 {
215 return false;
216 }
217
218 for (int xtemp=x; xtemp < (x+width); xtemp++)
219 {
220 if ((xtemp < 0) || (xtemp > GAME_WIDTH))
221 {
222 return false;
223 }
224
225 if (grid[xtemp][ytemp] != Tile.Unused)
226 {
227 return false;
228 }
229 }
230 }
231
232 for (int ytemp=(y-height/2); ytemp < (y+(height+1)/2); ytemp++)
233 {
234 for (int xtemp=x; xtemp < (x+width); xtemp++)
235 {
236 if (xtemp == x)
237 {
238 grid[xtemp][ytemp] = wall;
239 } else if (xtemp == (x+width-1))
240 {
241 grid[xtemp][ytemp] = wall;
242 } else if (ytemp == (y-height/2))
243 {
244 grid[xtemp][ytemp] = wall;
245 } else if (ytemp == (y+(height-1)/2))
246 {
247 grid[xtemp][ytemp] = wall;
248 } else {
249 grid[xtemp][ytemp] = floor;
250 }
251 }
252 }
253 169
254 break; 170 break;
255 171
256 case 2: // South 172 case South:
257 for (int ytemp=y; ytemp < (y+height); ytemp++) 173 room = new Room(x-width/2, y, width+1, height+1, true);
258 {
259 if ((ytemp < 0) || (ytemp > GAME_HEIGHT))
260 {
261 return false;
262 }
263
264 for (int xtemp=(x-width/2); xtemp < (x+(width+1)/2); xtemp++)
265 {
266 if ((xtemp < 0) || (xtemp > GAME_WIDTH))
267 {
268 return false;
269 }
270
271 if (grid[xtemp][ytemp] != Tile.Unused)
272 {
273 return false;
274 }
275 }
276 }
277
278 for (int ytemp=y; ytemp < (y+height); ytemp++)
279 {
280 for (int xtemp = (x-width/2); xtemp < (x+(width+1)/2); xtemp++)
281 {
282 if (xtemp == (x-width/2))
283 {
284 grid[xtemp][ytemp] = wall;
285 } else if (xtemp == (x+(width-1)/2))
286 {
287 grid[xtemp][ytemp] = wall;
288 } else if (ytemp == y)
289 {
290 grid[xtemp][ytemp] = wall;
291 } else if (ytemp == (y+height-1))
292 {
293 grid[xtemp][ytemp] = wall;
294 } else {
295 grid[xtemp][ytemp] = floor;
296 }
297 }
298 }
299 174
300 break; 175 break;
301 176
302 case 3: // West 177 case West:
303 for (int ytemp=(y-height/2); ytemp < (y+(height+1)/2); ytemp++) 178 room = new Room(x-width, y-height/2, width+1, height+1, true);
179
180 break;
181 }
182
183 for (int ytemp=room.getY(); ytemp < room.getY()+room.getHeight(); ytemp++)
184 {
185 if ((ytemp < 0) || (ytemp > GAME_HEIGHT))
186 {
187 return false;
188 }
189
190 for (int xtemp=room.getX(); xtemp < room.getX()+room.getWidth(); xtemp++)
191 {
192 if ((xtemp < 0) || (xtemp > GAME_WIDTH))
304 { 193 {
305 if ((ytemp < 0) || (ytemp > GAME_HEIGHT)) 194 return false;
306 { 195 }
307 return false;
308 }
309
310 for (int xtemp=x; xtemp > (x-width); xtemp--)
311 {
312 if ((xtemp < 0) || (xtemp > GAME_WIDTH))
313 {
314 return false;
315 }
316 196
317 if (grid[xtemp][ytemp] != Tile.Unused) 197 if (grid[xtemp][ytemp] != Tile.Unused)
318 { 198 {
319 return false; 199 return false;
320 }
321 }
322 } 200 }
323 201 }
324 for (int ytemp=(y-height/2); ytemp < (y+(height+1)/2); ytemp++) 202 }
203
204 for (int ytemp=room.getY(); ytemp < room.getY()+room.getHeight(); ytemp++)
205 {
206 for (int xtemp=room.getX(); xtemp < room.getX()+room.getWidth(); xtemp++)
207 {
208 if (xtemp == room.getX())
325 { 209 {
326 for (int xtemp=x; xtemp > (x-width); xtemp--) 210 grid[xtemp][ytemp] = wall;
327 { 211 } else if (xtemp == room.getX()+room.getWidth()-1)
328 if (xtemp == x) 212 {
329 { 213 grid[xtemp][ytemp] = wall;
330 grid[xtemp][ytemp] = wall; 214 } else if (ytemp == room.getY())
331 } else if (xtemp == (x-width+1)) 215 {
332 { 216 grid[xtemp][ytemp] = wall;
333 grid[xtemp][ytemp] = wall; 217 } else if (ytemp == room.getY()+room.getHeight()-1)
334 } else if (ytemp == (y-height/2)) 218 {
335 { 219 grid[xtemp][ytemp] = wall;
336 grid[xtemp][ytemp] = wall; 220 } else {
337 } else if (ytemp == (y+(height-1)/2)) 221 grid[xtemp][ytemp] = floor;
338 {
339 grid[xtemp][ytemp] = wall;
340 } else {
341 grid[xtemp][ytemp] = floor;
342 }
343 }
344 } 222 }
345 223 }
346 break;
347 } 224 }
348 225
226 rooms.add(room);
227
349 return true; 228 return true;
350 } 229 }
351 230
352 private boolean makeCorridor(int x, int y, int direction) 231 private boolean makeCorridor(int x, int y, Direction direction)
353 { 232 {
354 int length = Functions.random(MIN_CORRIDOR_LENGTH, MAX_CORRIDOR_LENGTH); 233 int length = Functions.random(MIN_CORRIDOR_LENGTH, MAX_CORRIDOR_LENGTH);
355 Tile floor = Tile.Corridor; 234 Tile floor = Tile.Corridor;
356 int dir = 0;
357 if ((direction > 0) && (direction < 4))
358 {
359 dir = direction;
360 }
361 235
362 int xtemp = 0; 236 int xtemp = 0;
363 int ytemp = 0; 237 int ytemp = 0;
364 238
365 switch (dir) 239 switch (direction)
366 { 240 {
367 case 0: // North 241 case North:
368 if ((x < 0) || (x > GAME_WIDTH)) 242 if ((x < 0) || (x > GAME_WIDTH))
369 { 243 {
370 return false; 244 return false;
@@ -392,7 +266,7 @@ public class MapViewGameState implements GameState
392 266
393 break; 267 break;
394 268
395 case 1: // East 269 case East:
396 if ((y < 0) || (y > GAME_HEIGHT)) 270 if ((y < 0) || (y > GAME_HEIGHT))
397 { 271 {
398 return false; 272 return false;
@@ -420,7 +294,7 @@ public class MapViewGameState implements GameState
420 294
421 break; 295 break;
422 296
423 case 2: // South 297 case South:
424 if ((x < 0) || (x > GAME_WIDTH)) 298 if ((x < 0) || (x > GAME_WIDTH))
425 { 299 {
426 return false; 300 return false;
@@ -448,7 +322,7 @@ public class MapViewGameState implements GameState
448 322
449 break; 323 break;
450 324
451 case 3: // West 325 case West:
452 if ((y < 0) || (y > GAME_HEIGHT)) 326 if ((y < 0) || (y > GAME_HEIGHT))
453 { 327 {
454 return false; 328 return false;
diff --git a/src/com/fourisland/frigidearth/Room.java b/src/com/fourisland/frigidearth/Room.java new file mode 100644 index 0000000..78f06f5 --- /dev/null +++ b/src/com/fourisland/frigidearth/Room.java
@@ -0,0 +1,52 @@
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.fourisland.frigidearth;
6
7/**
8 *
9 * @author hatkirby
10 */
11public class Room
12{
13 private int x;
14 private int y;
15 private int width;
16 private int height;
17 private boolean generateMonsters;
18
19 public Room(int x, int y, int width, int height, boolean generateMonsters)
20 {
21 this.x = x;
22 this.y = y;
23 this.width = width;
24 this.height = height;
25 this.generateMonsters = generateMonsters;
26 }
27
28 public int getX()
29 {
30 return x;
31 }
32
33 public int getY()
34 {
35 return y;
36 }
37
38 public int getWidth()
39 {
40 return width;
41 }
42
43 public int getHeight()
44 {
45 return height;
46 }
47
48 public boolean canGenerateMonsters()
49 {
50 return generateMonsters;
51 }
52}