-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoom.java
More file actions
158 lines (145 loc) · 6.06 KB
/
Room.java
File metadata and controls
158 lines (145 loc) · 6.06 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
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import ansi_terminal.Terminal;
/** Creates and manages the layout, enemies, boxes, and player in the first room
*
*/
public class Room {
// the grid holds the room geometry
private String[] grid;
// the size of the room
private int rows = 30;
private int cols = 60;
public Room() {
grid = new String[] {
"################## ###################### ",
"## ## ## i ## ",
"## @ ########### ## * ## ",
"## ## ## ## ",
"## ####### ## ################## ## ",
"## ## ## ## ## ## ",
"################## ## ################## ## ## ",
" ## ## ## ## ",
" ## * i ## ## ## ",
" ## ## ## ## ",
" ############## ###### ## ## ",
" ## ## ## ## ",
" ## ## ## ## ",
" ############ ############### ######",
" ## ##",
" ## * ##",
" ##################### * ##",
" ## ##",
" ## ################# i ##",
" ## ## ## ##",
" ## ## ################# ##################",
" ## ## ## ## ",
" ## ## ## ## ",
" ## ## ####### ####### ",
" ## ## ## ## ",
"###### #### ## i * ## ",
"## ## ## & ## ",
"## i * ## ################ ",
"## ## ",
"############ "
};
}
// returns the player's starting location in this room
public Position getPlayerStart() {
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if ( grid[row].charAt(col) == '@') {
return new Position(row, col);
}
}
}
return null;
}
// returns a set of item boxes for this map, this is here because it depends on
// the room geometry for where the boxes make sense to be
public ArrayList<Box> getBoxes() {
ArrayList<Box> boxes = new ArrayList<>();
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (grid[row].charAt(col) == 'i') {
boxes.add(new Box(row, col, ItemGenerator.instance().generate()));
}
}
}
return boxes;
}
// returns a set of enemies from this map, similarly to the boxes above
public ArrayList<Enemy> getEnemies() {
ArrayList<Enemy> enemies = new ArrayList<>();
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (grid[row].charAt(col) == '*') {
enemies.add(EnemyGenerator.instance().generate(row, col));
}
}
}
return enemies;
}
// returns the warp point
public ArrayList<Warp> getWarp() {
ArrayList<Warp> warps = new ArrayList<>();
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (grid[row].charAt(col) == '&') {
warps.add(new Warp(row, col));
}
}
}
return warps;
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
// draws the map to the screen
public void draw() {
Terminal.clear();
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
char cell = grid[row].charAt(col);
if (cell == '#') {
// a unicode block symbol
System.out.print('\u2588');
} else {
// whatever else, just draw a blank (we DON'T draw starting items from map)
System.out.print(' ');
}
}
System.out.print("\n\r");
}
}
// returns if a given cell in the map is walkable or not
public boolean canGo(int row, int col) {
return grid[row].charAt(col) != '#';
}
/** Writes the current room layout of Room 1 to the save file
*
* @param out the printwriter used to write data to a file
*/
public void save(PrintWriter out) {
for (String s : grid) {
out.println(s);
}
}
/** A constructor used for reading in the room layout of Room 1 from the save file
*
* @param in the scanner used to read in data from the file
*/
public Room(Scanner in) {
// we must initialize a new grid here prior to loading
grid = new String[30];
while (in.hasNext()) {
for (int i = 0; i < grid.length; i++) {
grid[i] = in.nextLine();
}
}
}
}