-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileMapManager.java
More file actions
101 lines (89 loc) · 2.88 KB
/
TileMapManager.java
File metadata and controls
101 lines (89 loc) · 2.88 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
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import javax.swing.JFrame;
/**
The TileMapManager class loads and manages tile maps and map files.
*/
public class TileMapManager {
private JFrame window;
private String tilesetPath;
private int defaultWidth = 800;
private int defaultHeight = 600;
/**
* Creates a new TileMapManager
*/
public TileMapManager(JFrame window, String tilesetPath) {
this.window = window;
this.tilesetPath = tilesetPath;
}
/**
* Set the window reference if it wasn't available at construction time
*/
public void setWindow(JFrame window) {
this.window = window;
}
/**
* Loads a map from a file
*/
public TileMap loadMap(String filename) throws IOException {
ArrayList<String> lines = new ArrayList<String>();
int mapWidth = 0;
int mapHeight = 0;
BufferedReader reader = new BufferedReader(new FileReader(filename));
while (true) {
String line = reader.readLine();
if (line == null) {
reader.close();
break;
}
if (!line.startsWith("#")) {
lines.add(line);
mapWidth = Math.max(mapWidth, line.length());
}
}
mapHeight = lines.size();
TileMap newMap;
if (window != null) {
newMap = new TileMap(window, mapWidth, mapHeight);
} else {
newMap = new TileMap(mapWidth, mapHeight, defaultWidth, defaultHeight);
}
newMap.loadTileImages(tilesetPath);
for (int y = 0; y < mapHeight; y++) {
String line = lines.get(y);
for (int x = 0; x < line.length(); x++) {
char ch = line.charAt(x);
int tileIndex = getTileIndexFromChar(ch);
if (tileIndex >= 0) {
Image tileImage = newMap.getTileImage(tileIndex);
if (tileImage != null) {
newMap.setTile(x, y, tileImage);
}
}
}
}
return newMap;
}
private int getTileIndexFromChar(char ch) {
if (Character.isDigit(ch)) {
if (ch >= '1' && ch <= '8') {
return ch - '1';
}
return ch - '0';
}
if (ch >= 'A' && ch <= 'Z') {
return 10 + (ch - 'A');
}
if (ch >= 'a' && ch <= 'z') {
return 36 + (ch - 'a');
}
switch (ch) {
case '.': return -1; // Empty space
case '#': return 0; // Solid block
case '=': return 1; // Platform
case '^': return 6; // Sky
default: return -1;
}
}
}