-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.java
More file actions
125 lines (118 loc) · 4.38 KB
/
Map.java
File metadata and controls
125 lines (118 loc) · 4.38 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
import java.io.*;
import java.util.*;
public class Map {
private int[][] layout;
private int[][] heightMap;
private double playerStartXPos; //this is along width
private double playerStartYPos; //this is along length
private double playerStartZPos; //this is along height
/*
Original Map Size = 15x15
'0' - air
'1' - tile
'2' - walltile
*/
public Map(String path) {
try {
readMapData(path);
} catch (IOException e) {
e.printStackTrace();
}
}
private void readMapData(String pathName) throws IOException {
String section = "";
File file = new File("MapFiles/"+pathName+".txt");
Scanner sc = new Scanner(file);
int w = 0, l = 0;
int index_r = 0, index_c = 0;
boolean useHeightMap = true;
while(sc.hasNextLine()) {
String word = sc.next();
String[] keyWords = {"width", "length", "playerStartXPos", "playerStartYPos",
"playerStartZPos", "layout", "useHeightMap", "overallMapHeight", "heightMap", "end"};
for(int i = 0; i < keyWords.length; i++) { //updates section
if(word.contains(keyWords[i]) && Math.abs(word.length() - keyWords[i].length()) < 3) { //avoids ':'
section = keyWords[i];
word = section;
}
}
if(section != word) {
switch(section) {
case "width":
w = Integer.parseInt(word);
break;
case "length":
l = Integer.parseInt(word);
break;
case "playerStartXPos":
layout = new int[l][w]; //width and length have just been retrieved
heightMap = new int[l][w];
playerStartXPos = Double.parseDouble(word);
break;
case "playerStartYPos":
playerStartYPos = Double.parseDouble(word);
break;
case "playerStartZPos":
playerStartZPos = Double.parseDouble(word);
break;
case "layout":
layout[index_r][index_c] = Integer.parseInt(word);
index_c++;
if(index_c == w) {
index_c = 0;
index_r++;
}
break;
case "useHeightMap":
index_r = 0;
index_c = 0;
if(word.equals("false"))
useHeightMap = false;
break;
case "overallMapHeight":
if(!useHeightMap)
for(int r = 0; r < heightMap.length; r++)
for(int c = 0; c < heightMap[r].length; c++)
heightMap[r][c] = Integer.parseInt(word);
break;
case "heightMap":
if(useHeightMap) {
heightMap[index_r][index_c] = Integer.parseInt(word);
index_c++;
if(index_c == w) {
index_c = 0;
index_r++;
}
}
break;
case "end":
sc.close();
break;
default:
break;
}
}
}
}
public int[][] getLayout() {
return layout;
}
public int[][] getHeightMap() {
return heightMap;
}
public int getWidth() {
return layout[0].length;
}
public int getLength() {
return layout.length;
}
public double getPlayerStartXPos() {
return playerStartXPos;
}
public double getPlayerStartYPos() {
return playerStartYPos;
}
public double getPlayerStartZPos() {
return playerStartZPos;
}
}