-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileMapManager.java
More file actions
218 lines (180 loc) · 6.22 KB
/
Copy pathTileMapManager.java
File metadata and controls
218 lines (180 loc) · 6.22 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
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.io.*;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
/**
The ResourceManager class loads and manages tile Images and
"host" Sprites used in the game. Game Sprites are cloned from
"host" Sprites.
*/
public class TileMapManager {
private ArrayList<Image> tiles;
private int currentMap = 0;
private JFrame window;
/*
private GraphicsConfiguration gc;
// host sprites used for cloning
private Sprite playerSprite;
private Sprite musicSprite;
private Sprite coinSprite;
private Sprite goalSprite;
private Sprite grubSprite;
private Sprite flySprite;
*/
public TileMapManager(JFrame window) {
this.window = window;
loadTileImages();
//loadCreatureSprites();
//loadPowerUpSprites();
}
public TileMap loadMap(String filename)
throws IOException
{
ArrayList<String> lines = new ArrayList<String>();
int mapWidth = 0;
int mapHeight = 0;
// read every line in the text file into the list
BufferedReader reader = new BufferedReader(
new FileReader(filename));
while (true) {
String line = reader.readLine();
// no more lines to read
if (line == null) {
reader.close();
break;
}
// add every line except for comments
if (!line.startsWith("#")) {
lines.add(line);
mapWidth = Math.max(mapWidth, line.length());
}
}
// parse the lines to create a TileMap
mapHeight = lines.size();
TileMap newMap = new TileMap(window, mapWidth, mapHeight);
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);
// check if the char represents tile A, B, C etc.
int tile = ch - 'A';
if (tile >= 0 && tile < tiles.size()) {
newMap.setTile(x, y, tiles.get(tile));
}
/*
// check if the char represents a sprite
else if (ch == 'o') {
addSprite(newMap, coinSprite, x, y);
}
else if (ch == '!') {
addSprite(newMap, musicSprite, x, y);
}
else if (ch == '*') {
addSprite(newMap, goalSprite, x, y);
}
else if (ch == '1') {
addSprite(newMap, grubSprite, x, y);
}
else if (ch == '2') {
addSprite(newMap, flySprite, x, y);
}
*/
}
}
return newMap;
}
/*
private void addSprite(TileMap map,
Sprite hostSprite, int tileX, int tileY)
{
if (hostSprite != null) {
// clone the sprite from the "host"
Sprite sprite = (Sprite)hostSprite.clone();
// center the sprite
sprite.setX(
TileMapRenderer.tilesToPixels(tileX) +
(TileMapRenderer.tilesToPixels(1) -
sprite.getWidth()) / 2);
// bottom-justify the sprite
sprite.setY(
TileMapRenderer.tilesToPixels(tileY + 1) -
sprite.getHeight());
// add it to the map
map.addSprite(sprite);
}
}
*/
// -----------------------------------------------------------
// code for loading sprites and images
// -----------------------------------------------------------
public void loadTileImages() {
// keep looking for tile A,B,C, etc. this makes it
// easy to drop new tiles in the images/ folder
File file;
System.out.println("loadTileImages called.");
tiles = new ArrayList<Image>();
char ch = 'A';
while (true) {
String filename = "images/tile_" + ch + ".png";
file = new File(filename);
if (!file.exists()) {
System.out.println("Image file could not be opened: " + filename);
break;
}
else
System.out.println("Image file opened: " + filename);
Image tileImage = new ImageIcon(filename).getImage();
tiles.add(tileImage);
ch++;
}
}
/*
public void loadCreatureSprites() {
Image[][] images = new Image[4][];
// load left-facing images
images[0] = new Image[] {
loadImage("player1.png"),
loadImage("player2.png"),
loadImage("player3.png"),
loadImage("fly1.png"),
loadImage("fly2.png"),
loadImage("fly3.png"),
loadImage("grub1.png"),
loadImage("grub2.png"),
};
images[1] = new Image[images[0].length];
images[2] = new Image[images[0].length];
images[3] = new Image[images[0].length];
for (int i=0; i<images[0].length; i++) {
// right-facing images
images[1][i] = getMirrorImage(images[0][i]);
// left-facing "dead" images
images[2][i] = getFlippedImage(images[0][i]);
// right-facing "dead" images
images[3][i] = getFlippedImage(images[1][i]);
}
// create creature animations
Animation[] playerAnim = new Animation[4];
Animation[] flyAnim = new Animation[4];
Animation[] grubAnim = new Animation[4];
for (int i=0; i<4; i++) {
playerAnim[i] = createPlayerAnim(
images[i][0], images[i][1], images[i][2]);
flyAnim[i] = createFlyAnim(
images[i][3], images[i][4], images[i][5]);
grubAnim[i] = createGrubAnim(
images[i][6], images[i][7]);
}
// create creature sprites
playerSprite = new Player(playerAnim[0], playerAnim[1],
playerAnim[2], playerAnim[3]);
flySprite = new Fly(flyAnim[0], flyAnim[1],
flyAnim[2], flyAnim[3]);
grubSprite = new Grub(grubAnim[0], grubAnim[1],
grubAnim[2], grubAnim[3]);
System.out.println("loadCreatureSprites successfully executed.");
}
*/
}