-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoomBuilder.java
More file actions
224 lines (197 loc) · 5.21 KB
/
Copy pathRoomBuilder.java
File metadata and controls
224 lines (197 loc) · 5.21 KB
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
import java.util.ArrayList;
public class RoomBuilder{
private Room room;
public RoomBuilder(){
room = new Room();
room.isEnemyHere = false;
room.isExplored = false;
room.isPlayerHere = false;
}
public RoomBuilder(Position pos,int row, int column){
room = new Room();
room.position = pos;
createTiles(row,column);
}
/**
* Reset the room inside the builder
*
* @param rowMax row count of the new room
* @param columnMax column count of the new room
*/
public void resetBuilder(){
room = new Room();
room.isEnemyHere = false;
room.isExplored = false;
room.isPlayerHere = false;
}
/**
* Sets the position of the room
*
* @param position Position of the room
*/
public void setPosition(Position position){
room.position = position;
}
/**
* Creates the initial empty tiles with walls around it
*
* @param roomTiles Represents the tiles of the whole room
* @param rowMax Indicates the row of the room
* @param columnMax Indicates the column of the room
*/
public void createTiles(int rowMax, int columnMax) {
ArrayList<ArrayList<Tile>> roomTiles = new ArrayList<ArrayList<Tile>>();
for (int i = 0; i < columnMax; i++)
{
ArrayList<Tile> row = new ArrayList<Tile>();
for (int j = 0; j < rowMax; j++)
{
if(i == 0 || j == 0 || i == columnMax-1 || j == rowMax-1){
row.add(new Tile(TileType.wall,new Position(i,j)));
}else{
row.add(new Tile(TileType.empty,new Position(i,j)));
}
}
roomTiles.add(row);
}
room.tiles = roomTiles;
}
/**
* Adds door to one of the four directions of the room
*
* @param direction Direction given in char (l,r,u,d) (left, right, up, down)
* @param door Door entity that is added to the room
*/
public void addDoor(char direction, Door door){
ArrayList<ArrayList<Tile>> roomTiles = room.tiles;
TileType type = TileType.openDoor;
if(door.isLocked){
type = TileType.lockedDoor;
}
int xMiddle = roomTiles.size()/2;
int yMiddle = roomTiles.get(0).size()/2;
int doorPosX = 0;
int doorPosY = 0;
switch(direction){
case 'l':
doorPosX = xMiddle;
doorPosY = 0;
break;
case 'r':
doorPosX = xMiddle;
doorPosY = roomTiles.get(0).size()-1;
break;
case 'u':
doorPosX = 0;
doorPosY = yMiddle;
break;
case 'd':
doorPosX = roomTiles.size()-1;
doorPosY = yMiddle;
break;
}
Position pos = new Position(doorPosX,doorPosY);
Tile doorTile = new Tile(type,pos,door);
roomTiles.get(doorPosX).set(doorPosY,doorTile);
room.tiles = roomTiles;
}
/**
* Adds random design according to the given characther
* Design follows the shape of the given characther
* Not all characthers are allowed
* Allowed characthers: T,U,J,L,C
*
* @param design Indicates the design of the room (with extra walls)
*/
public void addDesign(char design){
ArrayList<ArrayList<Tile>> roomTiles = room.tiles;
ArrayList<Position> fillCordinates = new ArrayList<Position>();
int columnMax = roomTiles.size();
int rowMax = roomTiles.get(0).size();
switch(design){
case 'T':
for (int i = 0; i < columnMax; i++)
{
for (int j = 0; j < rowMax; j++)
{
if(i<2) fillCordinates.add(new Position(i,j));
else if(i==2 && j%2==1) fillCordinates.add(new Position(i,j));
}
}
break;
case 'U':
for (int i = 0; i < columnMax; i++)
{
for (int j = 0; j < rowMax; j++)
{
if(i>columnMax-3) fillCordinates.add(new Position(i,j));
if(j==1 || j==rowMax-2) fillCordinates.add(new Position(i,j));
}
}
break;
case 'J':
for (int i = 0; i < columnMax; i++)
{
for (int j = 0; j < rowMax; j++)
{
if(j==rowMax-2) fillCordinates.add(new Position(i,j));
if(i==1 && j>rowMax/2) fillCordinates.add(new Position(i,j));
if(i==columnMax-2 && j>rowMax/2) fillCordinates.add(new Position(i,j));
}
}
break;
case 'L':
for (int i = 0; i < columnMax; i++)
{
for (int j = 0; j < rowMax; j++)
{
if(i>j+1) fillCordinates.add(new Position(i,j));
}
}
break;
case 'C':
for (int i = 0; i < columnMax; i++)
{
for (int j = 0; j < rowMax; j++)
{
if(i>j+1) fillCordinates.add(new Position(i,j));
if(columnMax-i>j+2) fillCordinates.add(new Position(i,j));
}
}
break;
}
for (int i = 0; i < fillCordinates.size(); i++)
{
Position pos = fillCordinates.get(i);
roomTiles.get(pos.x).set(pos.y,new Tile(TileType.wall,pos));
}
room.tiles = roomTiles;
}
/**
* Adds entity to the desired position in the room
*
* @param entity The entity object that needs to be added
* @param position The position of the entity that will be added
*/
public void addEntity(Entity entity, Position position){
ArrayList<ArrayList<Tile>> roomTiles = room.tiles;
roomTiles.get(position.x).get(position.y).setEntity(entity);
if(entity instanceof LivingBeing lb){
if(lb.name.equals("Player")){
room.isPlayerHere = true;
room.isExplored = true;
}else if(lb.name.equals("Enemy")){
room.isEnemyHere = true;
}
}
room.tiles = roomTiles;
}
/**
* Returns the final builded room
*
* @return Final builded room
*/
public Room buildRoom(){
return room;
}
}