-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel.java
More file actions
359 lines (315 loc) · 8.53 KB
/
Level.java
File metadata and controls
359 lines (315 loc) · 8.53 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import java.awt.Graphics;
import java.awt.Image;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* This class represents a level in the game.
*
* @author John Kurlak (kurlak)
* Weston Thayer (weston5)
* Panhavorn Hok (onehok1)
* @version 12.07.09
*/
public class Level
{
private static final int BLOCKS_PER_X = Global.WINDOW_WIDTH /
Global.BLOCK_SIZE;
private static final int BLOCKS_PER_Y = Global.WINDOW_HEIGHT /
Global.BLOCK_SIZE;
private static final int MAP_SPEED = 4;
private String fileName;
private ArrayList<ArrayList<String>> mapBlocks = new ArrayList<ArrayList
<String>>();
private ArrayList<Enemy> enemies = new ArrayList<Enemy>();
private ArrayList<PowerUp> powerUps = new ArrayList<PowerUp>();
private String name;
private String description;
private int currentY = 0;
private int blockYOffset = 0;
private Image image;
/**
* This constructor creates a new level.
*
* @param levelFile The relative file path of the level text file
*/
public Level(String levelFile)
{
fileName = levelFile.replace(".txt", "");
image = Utilities.loadImage(fileName + ".png");
readLevel();
}
/**
* This method returns the preview image for the level.
*
* @return A reference to the preview image for the level
*/
public Image getImage()
{
return image;
}
/**
* This method returns the name of the level.
*
* @return Returns the name of the level
*/
public String getName()
{
return name;
}
/**
* This method returns the description of the level.
*
* @return Returns the description of the level
*/
public String getDescription()
{
return description;
}
/**
* This method gets the filename corresponding to the level without any file
* extensions.
*
* @return Returns the filename of the level without any file extensions
*/
public String getFileName()
{
return fileName;
}
/**
* This method gets the current y position of the level.
*
* @return Returns the current y position of the level
*/
public int getCurrentY()
{
return currentY;
}
/**
* This method reads and parses a level text file
*/
public void readLevel()
{
try
{
Scanner sc = new Scanner(Utilities.loadFile(fileName + ".txt"));
name = sc.nextLine();
description = sc.nextLine();
int numRows = sc.nextInt();
int numEnemies = sc.nextInt();
sc.nextLine();
sc.nextLine();
//Clear the previous map and enemy matrix
mapBlocks.clear();
enemies.clear();
powerUps.clear();
//Add second dimension to array lists
for(int i = 0; i < numRows; i++)
{
mapBlocks.add(new ArrayList<String>());
}
String mapValue = "";
int numCols = 0;
for (int rowNum = 0; rowNum < numRows; rowNum++)
{
for (int colNum = 0; colNum < BLOCKS_PER_X; colNum++)
{
mapValue = sc.next();
++numCols;
if (numCols == BLOCKS_PER_Y)
{
numCols = 0;
}
mapBlocks.get(rowNum).add(mapValue);
}
}
currentY = numRows - BLOCKS_PER_Y;
sc.nextLine();
sc.nextLine();
for (int enemyNum = 0; enemyNum < numEnemies; enemyNum++)
{
String type = sc.next();
String xFunction = sc.next();
String yFunction = sc.next();
int x = sc.nextInt();
int yBlock = sc.nextInt() + BLOCKS_PER_Y;
int yOffset = sc.nextInt();
int health = sc.nextInt();
int fireInterval = sc.nextInt();
if (type.equals("02"))
{
ExtraBomb current = new ExtraBomb(x, yBlock, yOffset,
LevelManager.SPRITE_MAPPINGS.get(type), 0, 1, xFunction,
yFunction);
powerUps.add(current);
GamePanel.addSprite(current);
}
else if (type.equals("03"))
{
ExtraMissile current = new ExtraMissile(x, yBlock, yOffset,
LevelManager.SPRITE_MAPPINGS.get(type), 0, 1, xFunction,
yFunction);
powerUps.add(current);
GamePanel.addSprite(current);
}
else if (type.equals("05"))
{
ExtraHealth current = new ExtraHealth(x, yBlock, yOffset,
LevelManager.SPRITE_MAPPINGS.get(type), 0, 1, xFunction,
yFunction);
powerUps.add(current);
GamePanel.addSprite(current);
}
else if (type.equals("06"))
{
ExtraLife current = new ExtraLife(x, yBlock, yOffset,
LevelManager.SPRITE_MAPPINGS.get(type), 0, 1, xFunction,
yFunction);
powerUps.add(current);
GamePanel.addSprite(current);
}
else if (type.equals("07"))
{
Enemy current = new Enemy(x, yBlock, yOffset,
LevelManager.SPRITE_MAPPINGS.get(type), 0, 0, xFunction,
yFunction, health, fireInterval, 0, type);
enemies.add(current);
GamePanel.addSprite(current);
}
else
{
Enemy current = new Enemy(x, yBlock, yOffset,
LevelManager.SPRITE_MAPPINGS.get(type), 0, 0, xFunction,
yFunction, health, fireInterval,
LevelManager.ENEMY_CRASH_VALS.get(type), type);
enemies.add(current);
GamePanel.addSprite(current);
}
}
sc.close();
GamePanel.setLevelComplete(false);
}
catch (FileNotFoundException e)
{
System.out.println("a" + e.getMessage());
System.exit(1);
}
}
/**
* This method draws the current level, including its enemies and power-ups
*
* @param g The graphics object to which we can draw
*/
public void draw(Graphics g)
{
drawMap(g);
drawEnemies(g);
drawPowerUps(g);
}
/**
* This method draws the enemies onto the screen.
*
* @param g The graphics object to which we can draw
*/
private void drawEnemies(Graphics g)
{
ArrayList<Sprite> sprites = GamePanel.getSprites();
for (int i = 0; i < sprites.size(); i++)
{
if (sprites.get(i) instanceof Enemy)
{
Enemy enemy = (Enemy) sprites.get(i);
int currentYTop = currentY + BLOCKS_PER_Y - 1;
boolean visibleX = enemy.getX() + enemy.getWidth() >= 0 && enemy
.getX() <= Global.WINDOW_WIDTH;
boolean yOnScreen = enemy.getY() <= Global.WINDOW_HEIGHT + 100;
boolean visibleY = enemy.getYBlock() >= currentYTop &&
yOnScreen;
if (visibleX && visibleY && enemy.isAlive())
{
enemy.draw(g);
}
else if (!visibleX || !yOnScreen)
{
enemy.remove();
}
}
}
}
/**
* This method draws the power-ups onto the screen.
*
* @param g The graphics object to which we can draw
*/
private void drawPowerUps(Graphics g)
{
ArrayList<Sprite> sprites = GamePanel.getSprites();
for (int i = 0; i < sprites.size(); i++)
{
if (sprites.get(i) instanceof PowerUp)
{
PowerUp powerUp = (PowerUp) sprites.get(i);
int currentYTop = currentY + BLOCKS_PER_Y - 1;
boolean visibleX = powerUp.getX() + powerUp.getWidth() >= 0 &&
powerUp.getX() <= Global.WINDOW_WIDTH;
boolean yOnScreen = powerUp.getY() <= Global.WINDOW_HEIGHT +
100;
boolean visibleY = powerUp.getYBlock() >= currentYTop &&
yOnScreen;
if (visibleX && visibleY && powerUp.isAlive())
{
powerUp.draw(g);
}
else if (!yOnScreen)
{
powerUp.remove();
}
}
}
}
/**
* This method draws the map onto the screen.
*
* @param g The graphics object to which we can draw
*/
private void drawMap(Graphics g)
{
int drawX = 0;
int drawY = -Global.BLOCK_SIZE;
for (int row = currentY - 1; row <= currentY + BLOCKS_PER_Y; row++)
{
drawX = 0;
for (int col = 0; col < BLOCKS_PER_X; col++)
{
Image block = null;
if (row >= 0 && row < mapBlocks.size())
{
block = LevelManager.BLOCK_MAPPINGS.get(mapBlocks.get(row)
.get(col));
}
g.drawImage(block, drawX, drawY + blockYOffset, null);
drawX += Global.BLOCK_SIZE;
}
drawY += Global.BLOCK_SIZE;
}
// Don't move the map if the game is paused
if (currentY > 0 && GameState.getCurrentState() != GameState
.GAME_PAUSED)
{
blockYOffset += MAP_SPEED;
}
// Level done
else if (!GamePanel.isLevelComplete() && currentY == 0 && GamePanel
.getPlayer().isAlive() && !GameState.isLoading())
{
GamePanel.offsetNumLevelPoints(5000);
GamePanel.offsetNumPoints(GamePanel.getNumLevelPoints());
GamePanel.setLevelComplete(true);
}
if (blockYOffset == Global.BLOCK_SIZE)
{
blockYOffset = 0;
currentY--;
}
}
}