-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoom.java
More file actions
236 lines (203 loc) · 6.49 KB
/
Copy pathRoom.java
File metadata and controls
236 lines (203 loc) · 6.49 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
225
226
227
228
229
230
231
232
233
234
235
236
import java.util.ArrayList;
import java.io.IOException;
public class Room extends Entity{
public static class Movement{
public static final int UP = 119; // w
public static final int DOWN = 115; // s
public static final int LEFT = 97; // a
public static final int RIGHT = 100; // d
public static final int INTERACT = 101; // e
public static final int DROP = 113; // q
public static final int EXIT = 200; // p
// for getting input from the terminal
// unfortunatelly only works in Windows
public static char getKey() throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder(
"cmd", "/c", "choice /c WASDEQP /n"
);
pb.inheritIO();
Process p = pb.start();
int exitCode = p.waitFor();
switch (exitCode) {
case 1:
return UP;
case 2:
return LEFT;
case 3:
return DOWN;
case 4:
return RIGHT;
case 5:
return INTERACT;
case 6:
return DROP;
case 7:
return EXIT;
default:
return '?';
}
}
// checks if the player can move to the given position
// returns true if the tile is empty or open door
private static boolean canMoveTo(ArrayList<ArrayList<Tile>> moveTiles, Position pos){
Tile tile = moveTiles.get(pos.x).get(pos.y);
if (tile.tileType == TileType.empty || tile.tileType == TileType.openDoor) return true;
return false;
}
// finds player's position in the tiles
public static Position findPlayer(ArrayList<ArrayList<Tile>> moveTiles){
for (int i = 0; i < moveTiles.size(); i++)
{
for (int j = 0; j < moveTiles.get(i).size(); j++)
{
Tile tile = moveTiles.get(i).get(j);
if(tile.tileType.equals(TileType.player)) return tile.position;
}
}
return null;
}
// gets the first found entity tile around the player
private static Tile getSurroundingEntityTile(ArrayList<ArrayList<Tile>> moveTiles){
Position playerPos = findPlayer(moveTiles);
ArrayList<Position> checkPos = new ArrayList<Position>();
checkPos.add(new Position(playerPos.x+1,playerPos.y));
checkPos.add(new Position(playerPos.x-1,playerPos.y));
checkPos.add(new Position(playerPos.x,playerPos.y+1));
checkPos.add(new Position(playerPos.x,playerPos.y-1));
for (int i = 0; i < checkPos.size(); i++)
{
int x = checkPos.get(i).x;
int y = checkPos.get(i).y;
Tile t = moveTiles.get(x).get(y);
if(t.entity instanceof Activatable || t.entity instanceof Item){
return t;
}
}
return null;
}
// gets the first found empty tile around the player (used for drop item)
private static Tile getSurroundingEmptyTile(ArrayList<ArrayList<Tile>> moveTiles){
Position playerPos = findPlayer(moveTiles);
ArrayList<Position> checkPos = new ArrayList<Position>();
checkPos.add(new Position(playerPos.x+1,playerPos.y));
checkPos.add(new Position(playerPos.x-1,playerPos.y));
checkPos.add(new Position(playerPos.x,playerPos.y+1));
checkPos.add(new Position(playerPos.x,playerPos.y-1));
for (int i = 0; i < checkPos.size(); i++)
{
int x = checkPos.get(i).x;
int y = checkPos.get(i).y;
Tile t = moveTiles.get(x).get(y);
if(t.tileType == TileType.empty){
return t;
}
}
return null;
}
// controls the whole movement,interact and drop system
public static void Move(ArrayList<ArrayList<Tile>> moveTiles){
int key = UP;
try{
key = Movement.getKey();
}catch(Exception ex){
System.out.println(ex);
}
Position playerPos = findPlayer(moveTiles);
Position newPos = new Position(playerPos.x, playerPos.y);
switch(key){
case UP:
newPos.x -= 1;
break;
case LEFT:
newPos.y -= 1;
break;
case DOWN:
newPos.x += 1;
break;
case RIGHT:
newPos.y += 1;
break;
case INTERACT:
Tile eTile = getSurroundingEntityTile(moveTiles);
if(eTile != null && eTile.entity != null){
Tile player = moveTiles.get(playerPos.x).get(playerPos.y);
if(eTile.entity instanceof Activatable e){
if(player.entity instanceof LivingBeing lb){
e.activate(lb.inventory);
}
}else if(eTile.entity instanceof Item i){
if(player.entity instanceof LivingBeing lb && lb.name.equals("Player")){
eTile.setEntity(lb.drop());
lb.pickUp(i);
}
}
return;
}
break;
case DROP:
Tile emptyTile = getSurroundingEmptyTile(moveTiles);
Tile player = moveTiles.get(playerPos.x).get(playerPos.y);
if(emptyTile != null){
if(player.entity instanceof LivingBeing lb && lb.name.equals("Player")){
emptyTile.setEntity(lb.drop());
}
}
break;
case EXIT:
System.out.println("You have exited the game...");
System.exit(0);
break;
}
int columnMax = moveTiles.size();
int rowMax = moveTiles.get(0).size();
Room initialRoom = null;
Room targetRoom = null;
if (canMoveTo(moveTiles, newPos)){
if(moveTiles.get(newPos.x).get(newPos.y).entity instanceof Door d && !d.isLocked){
if(d.room1.isPlayerHere){
initialRoom = d.room1;
targetRoom = d.room2;
}else{
initialRoom = d.room2;
targetRoom = d.room1;
}
if(targetRoom.name != null && targetRoom.name.equals("WIN")){
System.out.println("\nWell done. You have completed");
System.out.println("the labrynith...");
System.exit(0);
}
if(newPos.x == 0){ // go upper door
newPos.x = columnMax-2;
}else if(newPos.x == columnMax-1){ // go lower door
newPos.x = 1;
}else if(newPos.y == 0){ // go left door
newPos.y = rowMax-2;
}else if(newPos.y == rowMax-1){ // go right door
newPos.y = 1;
}
initialRoom.isPlayerHere = false;
targetRoom.isPlayerHere = true;
targetRoom.isExplored = true;
}
if(initialRoom != null){
if(moveTiles.get(playerPos.x).get(playerPos.y).entity instanceof LivingBeing lb && lb.name.equals("Player")){
initialRoom.tiles.get(playerPos.x).get(playerPos.y).setEntity(null);
targetRoom.tiles.get(newPos.x).get(newPos.y).setEntity(lb);
}
return;
}
Tile oldTile = moveTiles.get(playerPos.x).get(playerPos.y);
Tile newTile = moveTiles.get(newPos.x).get(newPos.y);
Entity player = oldTile.entity;
oldTile.setEntity(null);
newTile.setEntity(player);
}
}
}
public Position position;
public ArrayList<ArrayList<Tile>> tiles;
public boolean isPlayerHere;
public boolean isExplored;
public boolean isEnemyHere;
public void Update(){}
}