-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoom.java
334 lines (277 loc) · 10.8 KB
/
Room.java
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.Arrays;
/**
* A Class that contains the subclasses for the specific Rooms
* a rectangle room is the standart shape
*/
public class Room extends metaTile
{
int tileWidth = getDungeon().tileWidth;
int tileHeight = getDungeon().tileHeight;
int doorCount; //number of Exits this room will have
int doorMinimum; // Minimum number of Doors
boolean[] blockedWalls; //the blocked Walls of this room
int[] doorCoordinates; //int array for the Coordinates of the Doors
int[] doorDirections; //int array for the direction of each door
//0 = top 1 = right 2 = bottom 3 = left
int[] nextDoor; //Coords for the Entrance of these rooms
boolean colliding = false; //used for collisions of generated rooms
//For handling them over to addedToWorld()
int curX;
int curY;
int curWidth;
int curHeight;
int[] curEntrance;
/**
* Initialize. Handle the parameters over for use in addedToWorld()
*/
public Room(int x, int y, int width, int height, int...entrance)
{
curX = x;
curY = y;
curWidth = width;
curHeight = height;
curEntrance = entrance;
}
/**
* Generate the room itself and goes on with more Rooms
*/
@Override
protected void addedToWorld(World world)
{
//Create the Walls and the Floor
addSquare(new Wall(), curX, curY, curWidth, curHeight, false);
addSquare(new Floor(), curX + tileWidth, curY + tileHeight, curWidth - 2, curHeight - 2, true);
//if a entrance is given create the door
if(curEntrance.length == 2)
{
remove(getDungeon().getObjectsAt(curEntrance[0], curEntrance[1], Tile.class)); //remove the Tiles where the new Door is gonna take place and place it
add(new Door(), curEntrance[0], curEntrance[1], 0);
}
specificContent();
blockedWalls = blockedWalls();
setDoorCount();
if(doorCount == 0) //if the doorCount hasnt been set already
{
int limit = 0;
for(boolean curBlocked: blockedWalls)
{
if(!curBlocked)limit++; //increase the limit by every not blocked Wall
}
doorCount = Greenfoot.getRandomNumber(2*limit); //now randomize it
}
if(doorCount < doorMinimum)doorCount = doorMinimum;
doorCoordinates = new int[doorCount * 2]; //int array for the Coordinates of the Doors
doorDirections = new int[doorCount]; //int array for the direction of each door
//0 = top 1 = right 2 = bottom 3 = left
nextDoor = new int[doorCount * 2]; //Coords for the Entrance of these rooms
setDoors(); //Delete Walls and replace them with Doors Spawn another Room if possible
}
/**
* Subclasses can override this to place Deco, Mobs, etc.
*/
public void specificContent()
{
//For subclasses to Override
}
/**
* Act - do whatever the Rooms wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
}
/**
* This Function will set Doors randomly over the outer Walls
* Blocked Walls are excluded and for each Door it will spawn a following Room
*/
public void setDoors()
{
for(int i = 0; i<doorCount; i++)
{
setDoor(i);
}
}
/**
* Sets a random Door over non blocked Walls
*/
public void setDoor(int i)
{
boolean tooCloseDoor = false;
do
{
colliding = false;
tooCloseDoor = false;
//randomly picks a Wall and redos if its blocked
int random;
do random = Greenfoot.getRandomNumber(2) + 1;while(blockedWalls[random]);
doorDirections[i] = random;
//set the coordinates for the Door and the following entrance
// if(doorDirections[i] == 0)
// {
// doorCoordinates[2*i] = innerX + Greenfoot.getRandomNumber(curWidth - 2) * tileWidth;
// doorCoordinates[2*i + 1] = outerCoordinates[1];
//
// nextDoor[2*i] = doorCoordinates[2*i];
// nextDoor[2*i + 1] = doorCoordinates[2*i + 1] - tileHeight;
//
// }
if(doorDirections[i] == 1)
{
doorCoordinates[2*i] = getOtherX();
doorCoordinates[2*i + 1] = curY + tileHeight + Greenfoot.getRandomNumber(curHeight - 2) * tileHeight;
nextDoor[2*i] = doorCoordinates[2*i] + tileWidth;
nextDoor[2*i + 1] = doorCoordinates[2*i + 1];
}
else if(doorDirections[i] == 2)
{
doorCoordinates[2*i] = curY + tileWidth + Greenfoot.getRandomNumber(curWidth - 2) * tileWidth;
doorCoordinates[2*i + 1] = getOtherY();
nextDoor[2*i] = doorCoordinates[2*i];
nextDoor[2*i + 1] = doorCoordinates[2*i + 1] + tileHeight;
}
// else if(doorDirections[i] == 3)
// {
// doorCoordinates[2*i] = outerCoordinates[0];
// doorCoordinates[2*i + 1] = innerY + Greenfoot.getRandomNumber(curHeight - 2) * tileHeight;
//
// nextDoor[2*i] = doorCoordinates[2*i] - tileWidth;
// nextDoor[2*i + 1] = doorCoordinates[2*i + 1];
//
// }
//Get the Walls that will be
List<Tile> wallsToRemove = getDungeon().getObjectsAt(doorCoordinates[2*i], doorCoordinates[2*i + 1], Tile.class);
for(Tile currWall:wallsToRemove)
{
if(currWall.doorsInRange() || currWall.getClass() == Door.class) tooCloseDoor = true;
else
{
remove(currWall);
add(new Door(), doorCoordinates[2*i], doorCoordinates[2*i + 1],0);
}
}
/*
* Rufe die Funktion am besten von Hand auf... i ist die Numer der Tür, an der ein neuer Raum erzeugt werden soll
* Momentan läuft das ganze noch per Hand. Wenn ich die Abstandserkennung implementiert habe,dann rufe ich sie so auf
*/
//randomRoom(i);
}while(tooCloseDoor || colliding);
/*
* Rufe die Funktion am besten von Hand auf... i ist die Numer der Tür, an der ein neuer Raum erzeugt werden soll
* Momentan läuft das ganze noch per Hand. Wenn ich die Abstandserkennung implementiert habe,dann rufe ich sie so auf
*/
//randomRoom(i);
}
/**
* Checks which are blocked by the Worldedges
* Returns a boolean Array
*/
public boolean[] blockedWalls()
{
boolean[] blocked = new boolean[4]; //4 int 0 = top 1 = right 2 = bottom 3 = left; true = blocked false = available
if(getX() < 10 * tileWidth) //TODO: make this rely on the difficulty
{
blocked[0] = true; //top
}
if(getY() < 10 * tileHeight)
{
blocked[3] = true; //left
}
if(getOtherX() > getDungeon().frameWidth - 10 * tileWidth)
{
blocked[1] = true; //right
}
if(getOtherY() > getDungeon().frameHeight - 10 * tileHeight)
{
blocked[2] = true; //bottom
}
return blocked;
}
/**
* Creates a new Room at the given Door.
* It's possible that a bossRoom will spawn. In most cases it will be a standardRoom
*/
public void randomRoom(int number)
{
//TODO: -Metatiles
// -Fix Bossroomchance
int[] entrance = new int[2];
entrance[0] = nextDoor[2*number];
entrance[1] = nextDoor[2*number + 1];
int nextWidth = Greenfoot.getRandomNumber(7) + 6;
int nextHeight = Greenfoot.getRandomNumber(7) + 6;
int nextX = entrance[0];
int nextY = entrance[1];
if(doorDirections[number] == 0 || doorDirections[number] == 2)
{
nextX = nextX - nextWidth/2 * tileWidth;// + getDungeon().getOffset();
}
else if(doorDirections[number] == 1 || doorDirections[number] == 3)
{
nextY = nextY - nextHeight/2 * tileHeight; //+ getDungeon().getOffset();
}
//create new Rooms that will be added later
bossRoom nextBossRoom = new bossRoom(nextX, nextY, 9, 9,entrance);
standardRoom nextStandardRoom = new standardRoom(nextX, nextY, nextWidth, nextHeight,entrance);
if(!getDungeon().bossRoomSpawned)
{
if(Greenfoot.getRandomNumber(100) <= getDungeon().bossRoomChance)
{
getDungeon().addObject(nextBossRoom, nextX, nextY);
getDungeon().bossRoomSpawned = true;
//remove it if it would collide with anything else
if(nextBossRoom.colliding())
{
colliding = true;
nextBossRoom.remove();
}
}
else
{
getDungeon().addObject(nextStandardRoom, nextX, nextY);
getDungeon().bossRoomChance++;
//remove it if it would collide with anything else
if(nextStandardRoom.colliding())
{
colliding = true;
nextStandardRoom.remove();
}
}
}
else
{
getDungeon().addObject(nextStandardRoom, nextX, nextY);
getDungeon().bossRoomChance++;
//remove it if it would collide with anything else
if(nextStandardRoom.colliding())
{
colliding = true;
nextStandardRoom.remove();
}
}
}
/**
* Returns the X Value of the right side
*/
public int getOtherX()
{
return curX + (curWidth - 1)*tileWidth;
}
/**
* Returns the Y Value of the bottom side
*/
public int getOtherY()
{
return curY + (curHeight - 1)*tileHeight;
}
/**
* Sets the doorCount and minimum doorCount
* Supposed to be overwridden by subclasses
*/
public void setDoorCount()
{
doorCount = 0;
doorMinimum = 0;
}
}