-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.java
More file actions
119 lines (99 loc) · 2.96 KB
/
Copy pathWorld.java
File metadata and controls
119 lines (99 loc) · 2.96 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
import java.util.ArrayList;
public class World{
public static class Printer{
public static final String RESET = "\u001B[0m";
public static final String PURPLE = "\u001B[35m";
// prints the current room and the mini room side-by-side
public static void printGame(ArrayList<Room> rooms){
Room currentRoom = new Room();
for (int i = 0; i < rooms.size(); i++)
{
if(rooms.get(i).isPlayerHere) currentRoom = rooms.get(i);
}
ArrayList<ArrayList<Tile>> mapTiles = currentRoom.tiles;
clearScreen();
for (int i = 0; i < mapTiles.size(); i++)
{
ArrayList<Tile> row = mapTiles.get(i);
for (int j = 0; j < row.size(); j++)
{
row.get(j).printTile();
if(j==row.size()-1){
System.out.print("\t");
for (int k = 0; k < row.size(); k++)
{
printMiniRoom(rooms,i,k);
System.out.print(" ");
}
}
}
System.out.println();
}
Position playerPos = Room.Movement.findPlayer(currentRoom.tiles);
if(currentRoom.tiles.get(playerPos.x).get(playerPos.y).entity instanceof LivingBeing player && player.name.equals("Player")){
String itemName = "Empty";
if(player.inventory != null) itemName = player.inventory.name;
System.out.println("Inventory: " + itemName);
}
}
// prints the miniRoom if it is explored
public static void printMiniRoom(ArrayList<Room> rooms, int x, int y){
for (int i = 0; i < rooms.size(); i++)
{
Room r = rooms.get(i);
if(r.position.x == x && r.position.y == y){
if(r.isPlayerHere) System.out.print(PURPLE);
if(!r.isExplored){
System.out.print(" ");
return;
}
System.out.print("O" + RESET);
return;
}
}
System.out.print(" ");
}
// clears the screnn
public static void clearScreen() {
try {
if (System.getProperty("os.name").contains("Windows")) {
// "cmd /c cls" runs the cls command and then terminates the cmd process
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} else {
// For Linux/macOS
new ProcessBuilder("clear").inheritIO().start().waitFor();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public World(){
rooms = new ArrayList<Room>();
}
public ArrayList<Room> rooms;
// return the current room in which the player is
public Room getCurrentRoom(){
Room currentRoom = new Room();
for (int i = 0; i < rooms.size(); i++)
{
if(rooms.get(i).isPlayerHere) currentRoom = rooms.get(i);
}
return currentRoom;
}
// runs the simulation until the player dies
public void runSimulation(){
while(true){
World.Printer.printGame(rooms);
if(getCurrentRoom().isEnemyHere) loseGame();
Room.Movement.Move(getCurrentRoom().tiles);
}
}
// activates when losing the game
// shows message and terminates the program
public void loseGame(){
System.out.println("You have encountered an enemy inside this room.");
System.out.println("GAME OVER!");
System.exit(0);
}
}