-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileManager.java
More file actions
95 lines (59 loc) · 2.26 KB
/
TileManager.java
File metadata and controls
95 lines (59 loc) · 2.26 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
package tile;
import main.GamePanel;
import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class TileManager {
GamePanel gp;
ArrayList< ArrayList<Tile> > tiles = new ArrayList<>();
int[][] mapTileNum;
public TileManager(GamePanel gp) {
this.gp = gp;
mapTileNum = new int[gp.maxScreenCol][gp.maxScreenRow];
getTileImage();
loadMap("/maps/map03.txt");
}
public void getTileImage() {
tiles.add(Tile.getTiles("/tiles/grass_dirt"));
// tiles.add(Tile.getTiles("/tiles/concrete_path"));
}
public void loadMap(String filePath) {
try {
// Reading the map file
InputStream inputStream = getClass().getResourceAsStream(filePath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
int row = 0;
String line;
while (( line = bufferedReader.readLine() )!= null) {
String[] column = line.split(" ");
for (int col = 0; col < gp.maxScreenCol; col++) {
String[] code = column[col].split("-");
int tileTypeId = Integer.parseInt(code[0]);
int tileIndex = Integer.parseInt(code[1]);
mapTileNum[col][row] = tileTypeId * 10 + tileIndex;
}
row++;
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void draw(Graphics2D g2) {
for (int row = 0; row < gp.maxScreenRow; row++) {
for (int col = 0; col < gp.maxScreenCol; col++) {
int tileCode = mapTileNum[col][row];
int tileTypeId = tileCode / 10;
int tileIndex = tileCode % 10;
if (tileTypeId < tiles.size()) {
ArrayList<Tile> tileSet = tiles.get(tileTypeId);
if(tileIndex < tileSet.size()) {
g2.drawImage(tileSet.get(tileIndex).image, col * gp.tileSize, row * gp.tileSize, gp.tileSize, gp.tileSize, null);
}
}
}
}
}
}