-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.java
111 lines (84 loc) · 3.23 KB
/
Map.java
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
import javax.swing.ImageIcon;
import java.awt.Image;
import java.util.stream.Stream;
public class Map {
ImageIcon tile = new ImageIcon("Assets\\tileBlockNormal.png");
Image tileImg = tile.getImage().getScaledInstance(GameFrame.scaleFactor, GameFrame.scaleFactor, Image.SCALE_DEFAULT);
static int[] dc = {0, -1, 0, 1, 0};
static int[] dr = {-1, 0, 1, 0, 0};
static int[] starBounds = new int[4];
static int r = 2, c = 4;
static int direct = 4;
static int blockCount;
static boolean firstGame;
public char[][] map;
public boolean[][] map_move;
public Map() { }
/* loads the active map into an instantiated Map for future use */
public Map(int mapIndex) {
loadSave(Integer.toString(mapIndex));
}
// public Map(char[][] map) {
// this.map = map;
// map_move = new boolean[7][11];
// firstGame = false;
// blockCount = 0;
// for (int i = 1; i < 6; i++) {
// for (int j = 1; j < 10; j++) {
// if (map[i][j] != ' ' && map[i][j] != 'p' && map[i][j] != 'x') blockCount++;
// }
// }
// }
/**
* finds the player coordinates for player initialization.
*
* @param map provides the map that the player is being loaded into
* @return int[] returns {row, column} data
*/
public int[] loadPlayer(char[][] map) {
int[] pCoords = new int[2];
for(int r = 0; r < map.length; r++) {
for(int c = 0; c < map[1].length; c++)
if (map[r][c] == 'p'){
pCoords[0] = r;
pCoords[1] = c;
}
}
return pCoords;
}
public void loadSave(String id) {
String[] save = Bson.extractClassInfo(Bson.getClass("Map" + id, "Saves\\Levels.txt"));
if(save.length > 0) {
int col = Integer.parseInt(save[4]);
int row = Integer.parseInt(save[5]);
String mapString = save[6];
map = new char[row][col];
for(int i = 0; i < col*row; i++)
map[i / col][i % col] = mapString.charAt(i);
map_move = new boolean[row][col];
starBounds = Stream.of(save[7].split(", ")).mapToInt(Integer::parseInt).toArray();
blockCount = Integer.parseInt(save[8]);
firstGame = save[2].contains("true");
} else {
GameFrame.exitGame();
}
}
/**
* rewrites the map class structure data based on the class that
* is being updated
*
* @param starsAchived provides the field "starsAchived"s data
* @param id provides the class structure that needs updating
* @return String returns the updated class structure
*/
public static String[] rewriteMapData(boolean isWin, int starsAchived, String id) {
String[] save = Bson.extractClassInfo(Bson.getClass("Map" + id, "Saves\\Levels.txt"));
save[0] = id;
save[1] = "level " + id;
// save[2] = (isWin) ? "false" : "true";
save[2] = String.valueOf(!isWin && Boolean.valueOf(save[2]));
// save[2] = String.valueOf(isWin);
save[3] = Integer.toString(Math.max(starsAchived, Integer.parseInt(save[3])));
return Bson.formatContent(save);
}
}