-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileMap.java
More file actions
271 lines (227 loc) · 7.69 KB
/
TileMap.java
File metadata and controls
271 lines (227 loc) · 7.69 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import java.awt.Image;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.util.LinkedList;
import java.util.Iterator;
import javax.swing.JFrame;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class TileMap {
private static final int TILE_SIZE = 64;
private static final int TILE_SIZE_BITS = 6;
private Image[][] tiles;
private int screenWidth, screenHeight;
private int mapWidth, mapHeight;
private int offsetY;
private LinkedList<Sprite> sprites;
private Car player;
private double cameraX = 0;
private double cameraY = 0;
private double movementFactor = 2;
private Image[] tileImages;
/**
Creates a new TileMap with the specified width and
height (in number of tiles) of the map.
*/
public TileMap(int width, int height, int screenWidth, int screenHeight) {
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
mapWidth = width;
mapHeight = height;
offsetY = screenHeight - tilesToPixels(mapHeight);
System.out.println("offsetY: " + offsetY);
tiles = new Image[mapWidth][mapHeight];
sprites = new LinkedList<Sprite>();
}
public TileMap(JFrame window, int mapWidth, int mapHeight) {
this(mapWidth, mapHeight, window.getWidth(), window.getHeight());
}
/**
* Set the player reference
*/
public void setPlayer(Car player) {
this.player = player;
}
public void loadTileImages(String tilesetPath) {
try {
BufferedImage tilesetImage = ImageIO.read(new File(tilesetPath));
int tilesetWidth = tilesetImage.getWidth() / TILE_SIZE;
int tilesetHeight = tilesetImage.getHeight() / TILE_SIZE;
int totalTiles = tilesetWidth * tilesetHeight;
tileImages = new Image[totalTiles];
int tileIndex = 0;
for (int y = 0; y < tilesetHeight; y++) {
for (int x = 0; x < tilesetWidth; x++) {
if (tileIndex < totalTiles) {
tileImages[tileIndex] = tilesetImage.getSubimage(
x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE
);
tileIndex++;
}
}
}
System.out.println("Loaded " + tileIndex + " tiles from tileset");
} catch (IOException e) {
System.err.println("Error loading tileset: " + e.getMessage());
e.printStackTrace();
}
}
public Image getTileImage(int index) {
if (index >= 0 && index < tileImages.length) {
return tileImages[index];
}
return null;
}
/**
Gets the width of this TileMap (number of pixels across).
*/
public int getWidthPixels() {
return tilesToPixels(mapWidth);
}
/**
Gets the width of this TileMap (number of tiles across).
*/
public int getWidth() {
return mapWidth;
}
/**
Gets the height of this TileMap (number of tiles down).
*/
public int getHeight() {
return mapHeight;
}
/**
Gets the tile at the specified location. Returns null if
no tile is at the location or if the location is out of
bounds.
*/
public Image getTile(int x, int y) {
if (x < 0 || x >= mapWidth ||
y < 0 || y >= mapHeight)
{
return null;
}
else {
return tiles[x][y];
}
}
/**
Sets the tile at the specified location.
*/
public void setTile(int x, int y, Image tile) {
tiles[x][y] = tile;
}
/**
Class method to convert a pixel position to a tile position.
*/
public static int pixelsToTiles(float pixels) {
return pixelsToTiles(Math.round(pixels));
}
/**
Class method to convert a pixel position to a tile position.
*/
public static int pixelsToTiles(int pixels) {
return (int)Math.floor((float)pixels / TILE_SIZE);
}
/**
Class method to convert a tile position to a pixel position.
*/
public static int tilesToPixels(int numTiles) {
return numTiles * TILE_SIZE;
}
/**
* Move the map based on player velocity
*/
public void moveMap(double velX, double velY) {
cameraX -= velX * movementFactor;
cameraY -= velY * movementFactor;
boundCamera();
}
/**
* Ensure camera stays within map bounds
*/
private void boundCamera() {
if (cameraX < 0) cameraX = 0;
if (cameraY < 0) cameraY = 0;
int maxX = Math.max(0, mapWidth * TILE_SIZE - screenWidth);
int maxY = Math.max(0, mapHeight * TILE_SIZE - screenHeight);
if (cameraX > maxX) cameraX = maxX;
if (cameraY > maxY) cameraY = maxY;
}
/**
* Reset the camera position
*/
public void reset() {
cameraX = 0;
cameraY = 0;
}
public void draw(Graphics2D g2) {
int startX = pixelsToTiles((int)cameraX);
int startY = pixelsToTiles((int)cameraY);
int tilesWide = (int)Math.ceil((double)screenWidth / TILE_SIZE);
int tilesHigh = (int)Math.ceil((double)screenHeight / TILE_SIZE);
int endX = Math.min(startX + tilesWide + 2, mapWidth);
int endY = Math.min(startY + tilesHigh + 2, mapHeight);
startX = Math.max(0, startX);
startY = Math.max(0, startY);
for (int y = startY; y < endY; y++) {
for (int x = startX; x < endX; x++) {
Image image = getTile(x, y);
if (image != null) {
g2.drawImage(image,
tilesToPixels(x) - (int)cameraX,
tilesToPixels(y) - (int)cameraY,
TILE_SIZE, TILE_SIZE,
null);
}
}
}
}
public void addSprite(Sprite sprite) {
sprites.add(sprite);
}
public double getCameraY() {
return cameraY;
}
public double getCameraX() {
return cameraX;
}
public void setCameraPosition(double x, double y) {
this.cameraX = x;
this.cameraY = y;
boundCamera();
}
public void centerOn(int x, int y) {
cameraX = x - (screenWidth / 2);
cameraY = y - (screenHeight / 2);
boundCamera();
}
public int[] findRunwayCenter() {
for (int x = 0; x < mapWidth - 1; x++) {
for (int y = 0; y < mapHeight; y++) {
Image left = getTile(x, y);
Image right = getTile(x + 1, y);
if (left == getTileImage(5) && right == getTileImage(6)) {
int startY = y;
int endY = y;
while (endY + 1 < mapHeight &&
getTile(x, endY + 1) == getTileImage(5) &&
getTile(x + 1, endY + 1) == getTileImage(6)) {
endY++;
}
int centerX = x + 0;
return new int[]{centerX, startY, endY};
}
}
}
return null;
}
public int getScreenWidth() {
return screenWidth;
}
public int getScreenHeight() {
return screenHeight;
}
}