1
+ /*
2
+ * FXGL - JavaFX Game Library. The MIT License (MIT).
3
+ * Copyright (c) AlmasB (almaslvl@gmail.com).
4
+ * See LICENSE for details.
5
+ */
6
+
7
+ package com .almasb .fxgl .core .collection .grid ;
8
+
9
+ import java .util .Random ;
10
+ import static java .lang .Math .abs ;
11
+ import static java .lang .Math .sqrt ;
12
+
13
+
14
+ public class Dungeon {
15
+
16
+ // Create tilemap of given height and width
17
+ private DungeonCell [][] tileMap ;
18
+
19
+ private int [][] roomPositions = new int [20 ][2 ];
20
+
21
+ public void GenerateDungeon (int dungeonWidth , int dungeonHeight ){
22
+
23
+ Random rand = new Random ();
24
+ tileMap = new DungeonCell [dungeonWidth ][dungeonHeight ];
25
+
26
+ // Setup empty dungeon
27
+ for (int i = 0 ; i < tileMap .length ; i ++){
28
+ for (int j = 0 ; j < tileMap [0 ].length ; j ++){
29
+ tileMap [i ][j ] = new DungeonCell (i , j );
30
+ }
31
+ }
32
+
33
+ // Pick initial room positions
34
+ for (int i = 0 ; i < roomPositions .length ; i ++){
35
+ roomPositions [i ][0 ] = rand .nextInt (dungeonWidth );
36
+ roomPositions [i ][1 ] = rand .nextInt (dungeonHeight );
37
+ }
38
+
39
+ // Clear out rooms
40
+ for (int i = 0 ; i < roomPositions .length ; i ++){
41
+ int subRooms = rand .nextInt (1 ) + 1 ;
42
+
43
+ // Clear Circle
44
+ if (rand .nextInt (3 ) == 0 ) {
45
+ ClearCircle (roomPositions [i ][0 ], roomPositions [i ][1 ], rand .nextInt (3 ) + 1 );
46
+ }
47
+
48
+ // Clear Rect
49
+ else {
50
+ ClearRect (roomPositions [i ][0 ], roomPositions [i ][1 ], rand .nextInt (3 ) + 4 , rand .nextInt (3 ) + 4 );
51
+ }
52
+
53
+ // Connect Rooms
54
+ int connectRoom = rand .nextInt (roomPositions .length );
55
+
56
+ ClearPath (roomPositions [i ][0 ], roomPositions [i ][1 ], roomPositions [connectRoom ][0 ], roomPositions [connectRoom ][1 ]);
57
+
58
+ }
59
+ }
60
+
61
+ void ClearRect (int xPos , int yPos , int width , int height ){
62
+ for (int i = 0 ; i < tileMap .length ; i ++){
63
+ for (int j = 0 ; j < tileMap [0 ].length ; j ++){
64
+ int xDis = abs (i - xPos );
65
+ int yDis = abs (j - yPos );
66
+
67
+ if (xDis <= width /2 && yDis <= height /2 ) { tileMap [i ][j ].SetType (0 ); }
68
+ }
69
+ }
70
+ }
71
+
72
+ void ClearCircle (int xPos , int yPos , int radius ){
73
+ for (int i = 0 ; i < tileMap .length ; i ++){
74
+ for (int j = 0 ; j < tileMap [0 ].length ; j ++){
75
+ int xDis = abs (i - xPos );
76
+ int yDis = abs (j - yPos );
77
+
78
+ double tileDis = sqrt (xDis *xDis + yDis *yDis );
79
+ if (tileDis <= radius ) { tileMap [i ][j ].SetType (0 ); }
80
+ }
81
+ }
82
+ }
83
+
84
+ void ClearPath (int xStart , int yStart , int xEnd , int yEnd ){
85
+ int [] clearPos = new int [2 ];
86
+ clearPos [0 ] = xStart ;
87
+ clearPos [1 ] = yStart ;
88
+
89
+ while (clearPos [0 ] != xEnd || clearPos [1 ] != yEnd ){
90
+ if (clearPos [0 ] < xEnd ) clearPos [0 ]++;
91
+ else if (clearPos [0 ] > xEnd ) clearPos [0 ]--;
92
+ else if (clearPos [1 ] < yEnd ) clearPos [1 ]++;
93
+ else if (clearPos [1 ] > yEnd ) clearPos [1 ]--;
94
+
95
+ tileMap [clearPos [0 ]][clearPos [1 ]].SetType (0 );
96
+ }
97
+ }
98
+ }
0 commit comments