forked from mcyapan/CSCI205Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileManager.java
More file actions
114 lines (82 loc) · 3.46 KB
/
TileManager.java
File metadata and controls
114 lines (82 loc) · 3.46 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
import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class TileManager {
private SceneCanvas gc;
private DrawingObject[] tiles; // Array of tile objects
private int[][] mapTileNum; // Store which tile to use at each position
public TileManager(SceneCanvas gc) {
this.gc = gc;
// Create an array of tiles
tiles = new DrawingObject[10];
loadTiles();
// Example map: create a 2D array representing a grid of tile types
mapTileNum = new int[gc.getMaxWorldCol()][gc.getMaxWorldRow()];
loadMap();
}
public void loadTiles() {
tiles[0] = new SandTile(); // Tile type 0: sand
tiles[1] = new WallTile(); // Tile type 1: wall
tiles[2] = new WaterTile(); // Tile type 2: water
tiles[3] = new DogTile(); // Tile type 3: dog
tiles[4] = new PalmTreeTile(); // Tile type 4: palm tree
tiles[5] = new PathTile(); // Tile type 5: path
}
public void loadMap() {
try {
String map_name = "map1.txt";
InputStream is = getClass().getResourceAsStream(map_name);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
int col = 0;
int row = 0;
while(col < gc.getMaxWorldCol() && row < gc.getMaxWorldRow()) {
String line = br.readLine();
while(col < gc.getMaxWorldCol()) {
String numbers[] = line.split(" ");
int num = Integer.parseInt(numbers[col]);
mapTileNum[row][col] = num;
col++;
}
if(col == gc.getMaxWorldCol()) {
col = 0;
row++;
}
}
br.close();
} catch(Exception e) {
}
}
public void draw(Graphics2D g2d) {
int worldCol = 0;
int worldRow = 0;
while(worldCol < gc.getMaxWorldCol() && worldRow < gc.getMaxWorldRow()) {
int tileNum = mapTileNum[worldRow][worldCol];
DrawingObject tile = tiles[tileNum];
int worldX = worldCol * gc.getTileSize();
int worldY = worldRow * gc.getTileSize();
int screenX = worldX - gc.getPlayer().getWorldX() + gc.getPlayer().getScreenX();
int screenY = worldY - gc.getPlayer().getWorldY() + gc.getPlayer().getScreenY();
if (worldX > gc.getPlayer().getWorldX() - gc.getPlayer().getScreenX() - gc.getTileSize() &&
worldX < gc.getPlayer().getWorldX() + gc.getPlayer().getScreenX() + gc.getTileSize() &&
worldY > gc.getPlayer().getWorldY() - gc.getPlayer().getScreenY() - gc.getTileSize() &&
worldY < gc.getPlayer().getWorldY() + gc.getPlayer().getScreenY() + gc.getTileSize()) {
g2d.translate(screenX, screenY);
tile.drawTile(g2d);
g2d.translate(-screenX, -screenY);
}
// Move the tile to its correct position and draw it
worldCol++;
if (worldCol == gc.getMaxWorldCol()) {
worldCol = 0;
worldRow++;
}
}
}
public DrawingObject[] getTiles() {
return tiles;
}
public int[][] getMapTileNum() {
return mapTileNum;
}
}