forked from Dason-IsopodOverseer/Wrong-Galaxy-Beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTileMap.java
170 lines (143 loc) · 5.04 KB
/
TileMap.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
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
/* Program: Wrong Galaxy
* Date: November 7, 2021
* Authors: Dason Wang, Carter Cranston, Emily Wang
* Purpose: defines the attributes and behaviour of tilemaps used for the game
*
*/
import java.io.*;
import java.util.ArrayList; // import the ArrayList class
public class TileMap {
private Sprite[][] tiles;
public ArrayList<String> tileConfig = new ArrayList(); // stores the configuration of different tiles after being read by mapReader
private Sprite tileSprite[] = new Sprite[10];
private Game game;
private int height = 0; // stores the height of the map
private int width = 0; // stores the width of the map
private final int TILESIZE = 50; // the height and width of each tile in px
public TileMap(String tileFile, Game g) {
// read the txt tileFile and populate the empty tileConfig arraylist
// also gets width and height
mapReader(tileFile);
game = g;
tiles = new Sprite[width][height];
// set all tile sprites to corresponding tile images
for (int i = 0; i < tileSprite.length; i++) {
tileSprite[i] = (SpriteStore.get()).getSprite("sprites/" + i + ".png");
}
fillMap();
}
public int getWidth() {
return tiles.length;
}
public int getHeight() {
return tiles[0].length;
}
// get width - tiles.length
// get height - tiles[0].length
public Sprite getTile(int x, int y) {
return tiles[x][y];
}
public void setTile(int x, int y, Sprite tile) {
tiles[x][y] = tile;
}
// fills sprite array with appropriate tiles (based on string [] input)
private Sprite[][] fillMap() {
// begin to parse
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
String line = tileConfig.get(y);
char ch = line.charAt(x);
// check if the char represents tile A, B, c, etc.
switch (ch) {
case '@':
tiles[x][y] = tileSprite[0];
break;
case 'A':
tiles[x][y] = tileSprite[1];
break;
case 'B':
tiles[x][y] = tileSprite[2];
break;
case 'C':
tiles[x][y] = tileSprite[3];
break;
case 'D':
tiles[x][y] = tileSprite[4];
break;
case 'E':
tiles[x][y] = tileSprite[5];
break;
case 'F':
tiles[x][y] = tileSprite[6];
break;
case 'G':
tiles[x][y] = tileSprite[7];
break;
case 'H':
tiles[x][y] = tileSprite[8];
break;
case 'J':
tiles[x][y] = tileSprite[9];
break;
case 'k':
tiles[x][y] = null;
game.entities.add(new KlingonEntity(game, "kling", (x * TILESIZE), (y * TILESIZE)));
break;
case 'm':
tiles[x][y] = null;
game.entities.add(new KlingonEntity(game, "master", (x * TILESIZE), (y * TILESIZE)));
break;
case 'b':
tiles[x][y] = null;
game.entities.add(new BorgEntity(game, "borg", (x * TILESIZE), (y * TILESIZE)));
break;
case 'q':
tiles[x][y] = null;
game.entities.add(new BorgEntity(game, "queen", (x * TILESIZE), (y * TILESIZE)));
default:
tiles[x][y] = null;
} // switch
} // for
} // for
return tiles;
}
// reads the tileFile ad stores it in tileConfig
private void mapReader(String tileFile) {
// try to retrieve file contents
try {
// input
String folderName = "maps/";
String resource = tileFile;
// this is the path within the jar file
InputStream input = TileMap.class.getResourceAsStream(folderName + resource);
if (input == null) {
// this is how we load file within editor (eg eclipse)
input = TileMap.class.getClassLoader().getResourceAsStream(resource);
}
BufferedReader in = new BufferedReader(new InputStreamReader(input));
in.mark(Short.MAX_VALUE); // see api
while (true) {
String line = in.readLine();
// no more lines to read
if (line == null) {
in.close();
break;
}
// add every line except for comments
if (!line.startsWith("#")) {
// * makes an entire row empty
if (line.startsWith("*")) {
line = " "; // 28 spaces
}
tileConfig.add(line);
// set the width
width = Math.max(width, line.length());
}
}
// set the height
height = tileConfig.size();
} catch (Exception e) {
System.out.println("File Input Error");
} // end of try catch
}
}