about summary refs log tree commit diff stats
path: root/src/com/fourisland/frigidearth/MapViewGameState.java
blob: f378d5adca60851785caa414481098bcfd7a19da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fourisland.frigidearth;

import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

/**
 *
 * @author hatkirby
 */
public class MapViewGameState implements GameState
{
    private final int TILE_WIDTH = 16;
    private final int TILE_HEIGHT = 16;
    private final int GAME_WIDTH = 100;
    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 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;
    private int viewporty = 0;
    
    public MapViewGameState()
    {
        grid = new Tile[GAME_WIDTH][GAME_HEIGHT];
        
        for (int x=0; x<GAME_WIDTH; x++)
        {
            for (int y=0; y<GAME_HEIGHT; y++)
            {
                if ((x == 0) || (x == GAME_WIDTH-1) || (y == 0) || (y == GAME_HEIGHT-1))
                {
                    grid[x][y] = Tile.StoneWall;
                } else {
                    grid[x][y] = Tile.Unused;
                }
            }
        }
        
        makeRoom(GAME_WIDTH/2, GAME_HEIGHT/2, Functions.random(0, 3));
        playerx = GAME_WIDTH/2;
        playery = GAME_HEIGHT/2;
        
        int currentFeatures = 1;
        int objects = 300;
        
        for (int countingTries = 0; countingTries < 1000; countingTries++)
        {
            if (currentFeatures == objects)
            {
                break;
            }
            
            int newx = 0;
            int xmod = 0;
            int newy = 0;
            int ymod = 0;
            int validTile = -1;
            for (int testing = 0; testing < 1000; testing++)
            {
                newx = Functions.random(1, GAME_WIDTH-1);
                newy = Functions.random(1, GAME_HEIGHT-1);
                validTile = -1;
                
                if ((grid[newx][newy] == Tile.DirtWall) || (grid[newx][newy] == Tile.Corridor))
                {
                    if ((grid[newx][newy+1] == Tile.DirtFloor) || (grid[newx][newy+1] == Tile.Corridor))
                    {
                        validTile = 0;
                        xmod = 0;
                        ymod = -1;
                    } else if ((grid[newx-1][newy] == Tile.DirtFloor) || (grid[newx-1][newy] == Tile.Corridor))
                    {
                        validTile = 1;
                        xmod = 1;
                        ymod = 0;
                    } else if ((grid[newx][newy-1] == Tile.DirtFloor) || (grid[newx][newy-1] == Tile.Corridor))
                    {
                        validTile = 2;
                        xmod = 0;
                        ymod = 1;
                    } else if ((grid[newx+1][newy] == Tile.DirtFloor) || (grid[newx+1][newy] == Tile.Corridor))
                    {
                        validTile = 3;
                        xmod = -1;
                        ymod = 0;
                    }
                    
                    if (validTile > -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 (validTile > -1)
                    {
                        break;
                    }
                }
            }
            
            if (validTile > -1)
            {
                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 boolean makeRoom(int x, int y, int direction)
    {
        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))
        {
            dir = direction;
        }
        
        switch (dir)
        {
            case 0: // North
                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 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 boolean makeCorridor(int x, int y, int direction)
    {
        int length = Functions.random(MIN_CORRIDOR_LENGTH, MAX_CORRIDOR_LENGTH);
        Tile floor = Tile.Corridor;
        int dir = 0;
        if ((direction > 0) && (direction < 4))
        {
            dir = direction;
        }
        
        int xtemp = 0;
        int ytemp = 0;
        
        switch (dir)
        {
            case 0: // North
                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 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)
    {
        // Render tiles
        for (int x=viewportx; x<viewportx+VIEWPORT_WIDTH; x++)
        {
            for (int y=viewporty; y<viewporty+VIEWPORT_HEIGHT; y++)
            {
                g.drawImage(SystemFont.getCharacter(grid[x][y].getDisplayCharacter()), (x-viewportx)*TILE_WIDTH, (y-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null);
            }
        }
        
        // Render player
        g.drawImage(SystemFont.getCharacter('@'), (playerx-viewportx)*TILE_WIDTH, (playery-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null);
    }
    
    public void processInput(KeyEvent e)
    {
        if (e.getKeyCode() == KeyEvent.VK_LEFT)
        {
            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].isBlocked()))
            {
                playerx++;
            }
        } else if (e.getKeyCode() == KeyEvent.VK_UP)
        {
            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].isBlocked()))
            {
                playery++;
            }
        } else {
            return;
        }
        
        adjustViewport();
    }
    
    private void adjustViewport()
    {
        if (playerx > (VIEWPORT_WIDTH/2))
        {
            if (playerx < (GAME_WIDTH - (VIEWPORT_WIDTH/2-1)))
            {
                viewportx = playerx - (VIEWPORT_WIDTH/2);
            } else {
                viewportx = GAME_WIDTH - VIEWPORT_WIDTH;
            }
        } else {
            viewportx = 0;
        }
        
        if (playery > (VIEWPORT_HEIGHT/2))
        {
            if (playery < (GAME_HEIGHT - (VIEWPORT_HEIGHT/2-1)))
            {
                viewporty = playery - (VIEWPORT_HEIGHT/2);
            } else {
                viewporty = GAME_HEIGHT - VIEWPORT_HEIGHT;
            }
        } else {
            viewporty = 0;
        }
    }
}