-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level.java
291 lines (257 loc) · 9.75 KB
/
Level.java
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
/********************************************************************************************************************
Author: Chad Cromwell
Date: November 1st, 2017
Assignment: 1
Program: Level.java
Description: A class that loads a level for the UFO game, only used to load a single level for now. Can be adapted to load more levels in the future quite easily.
Methods:
tick() method - What is executed each frame
reload(Enemy a) method - Reloads the enemy. Accepts an Enemy object
updateArrays() method - Updates the enemy arrays
enemiesAttack() method - Handles how enemies attack
playerAttack() method - Handles player firing laser
animate() method - Animates the sprites
render() method - Renders everything
********************************************************************************************************************/
import java.awt.image.*;
import java.io.*;
import java.awt.*;
import javax.imageio.*;
public class Level {
//Finals
static final double GROUNDHEIGHT = (double)Game.HEIGHT-96; //Ground height, used to place things on the ground
static final int TILEPIX = 1; //Size of tiles, go pixel for pixel as it gives highest resolution
//Level variables
int width; //Width of screen
int height; //Height of screen
boolean change = true; //Used to determine if an array list change has taken place
static Tile[][] borderTiles; //Array of borderTiles for holding border tiles
String levelNumber; //Holds path to level png
LevelTexture levelTexture; //Map textures
//Lists to hold sprites
java.util.List<Enemy> enemies;
java.util.List<Missile> missiles;
java.util.List<Laser> lasers;
java.util.List<Explosion> explosions;
java.util.List<Enemy> normalIndex;
java.util.List<Enemy> coordinatedIndex;
java.util.List<Enemy> stupidIndex;
java.util.List<Enemy> evasiveIndex;
java.util.List<Enemy> smartIndex;
//Level constructor - Accepts an int to determine what level to load
public Level(int levelNumber) {
this.levelNumber = Integer.toString(levelNumber); //Capture levelNumber and assign it to the level object
//Initialize Lists
enemies = new java.util.ArrayList<>(); //Enemies list
missiles = new java.util.ArrayList<>(); //Missiles list
lasers = new java.util.ArrayList<>(); //Lasers list
explosions = new java.util.ArrayList<>(); //Lasers list
normalIndex = new java.util.ArrayList<>(); //Normal enemies list
coordinatedIndex = new java.util.ArrayList<>(); //Coordinated enemies list
stupidIndex = new java.util.ArrayList<>(); //Stupid enemies list
evasiveIndex = new java.util.ArrayList<>(); //Evasive enemies list
smartIndex = new java.util.ArrayList<>(); //Smart enemies list
levelTexture = new LevelTexture("img/level" + levelNumber + "texture.png"); //Initialize the map texture
try {
BufferedImage map = ImageIO.read(getClass().getResource("img/level" + levelNumber + ".png")); //Open image and assign to map
this.width = map.getWidth(); //Get the width of the map image, assign it to width
this.height = map.getHeight(); //Get the height of the map image, assign it to height
int[] pixels = new int[width*height]; //Array to hold pixels
borderTiles = new Tile[width][height]; //Create new Tile object array, assign it to borderTiles
map.getRGB(0, 0, width, height, pixels, 0, width); //Get RGB component of pixels from the map and assign them to the pixel array
//Iterate through each element in pixel array by 1*1px resolution (So that level is rendered in a 1:1 resolution)
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
int val = pixels[x + (y*width)];
//If pixel is white, it's a boundary
if(val == 0xFFFFFFFF) {
borderTiles[x][y] = new Tile(x*TILEPIX, y*TILEPIX, true); //Create a tile in its location in the Tile array
}
}
}
//Add enemies to level (lists)
enemies.add(new Enemy(100, GROUNDHEIGHT, "stupid"));
enemies.add(new Enemy(200, GROUNDHEIGHT, "coordinated"));
enemies.add(new Enemy(300, GROUNDHEIGHT, "coordinated"));
enemies.add(new Enemy(400, GROUNDHEIGHT, "evasive"));
enemies.add(new Enemy(500, GROUNDHEIGHT, "smart"));
}
catch (IOException e) {
e.printStackTrace(); //For debugging
}
}
//tick() method - What is executed each frame
public void tick() {
animate(); //Animate enemies
updateArrays(); //Update the index arrays
enemiesAttack(); //Call enemiesAttack
playerAttack(); //Call playerAttack();
}
//reload(Enemy a) method - Reloads the enemy. Accepts an Enemy object
public void reload(Enemy a) {
//If the enemy is not loaded
if(!a.loaded) {
//If the amount of time to reload has passed
if(a.reloadTimer >= a.reloadTime) {
a.loaded = true; //Now loaded
a.reloadTimer = 0; //Reset reloadTimer
}
else {
++a.reloadTimer; //Increment reloadTimer
}
}
}
//fire(Enemy a) method - Fires the enemy weapon. Accepts an Enemy object
public void fire(Enemy a, String type) {
//If the enemy can fire and is loaded
if(a.canFire && a.loaded) {
a.loaded = false; //It's no longer loaded
a.canFire = false; //It cannot fire anymore
missiles.add(new Missile(a, type)); //Create a missile object (fire a missile)
}
}
//attack(Enemy a) method - Causes the enemy to attack, calls reload, then fire. Accepts an Enemy object
public void attack(Enemy a, String type) {
reload(a); //Reload the enemy
fire(a, type); //Fire a missile of the parameter type
}
//updateArrays() method - Updates the enemy arrays
public void updateArrays() {
//If a change in the number of enemies has taken place
if(change) {
//Clear the lists
normalIndex.clear();
coordinatedIndex.clear();
stupidIndex.clear();
evasiveIndex.clear();
smartIndex.clear();
//For each enemy in the enemy array
for(int i = 0; i < enemies.size(); i++) {
//If it's a normal type
if(enemies.get(i).type == "normal") {
normalIndex.add(enemies.get(i)); //Add it to the normal index array
}
//If it's a coordinated type
if(enemies.get(i).type == "coordinated") {
coordinatedIndex.add(enemies.get(i)); //Add it to the coordinated index array
}
//If it's a stupid type
if(enemies.get(i).type == "stupid") {
stupidIndex.add(enemies.get(i)); //Add it to the normal index array
}
//If it's a evasive type
if(enemies.get(i).type == "evasive") {
evasiveIndex.add(enemies.get(i)); //Add it to the normal index array
}
//If it's a smart type
if(enemies.get(i).type == "smart") {
smartIndex.add(enemies.get(i)); //Add it to the normal index array
}
}
change = false; //We're done account for the change in enemies
}
}
//enemiesAttack() method - Handles how enemies attack
public void enemiesAttack() {
//If there are stupid enemies
if(stupidIndex.size() > 0) {
//For each stupid enemy in the stupidIndex
for(int i = 0; i < stupidIndex.size(); i++) {
attack(stupidIndex.get(i), "normal"); //Call attack for each one
}
}
//If there are more than 2 normal enemies
if(coordinatedIndex.size() > 1) {
enemies.get(0).coordinatedAttack(coordinatedIndex.get(coordinatedIndex.size()-2), coordinatedIndex.get(coordinatedIndex.size()-1)); //Call coordinated attack for 2 last enemies
//For each normal enemy in the normalIndex, except for the last two
for(int i = 0; i < coordinatedIndex.size()-2; i ++) {
attack(coordinatedIndex.get(i), "coordinated"); //Call attack for each one
}
}
//If there is only 1 coordinated enemy
if(coordinatedIndex.size() == 1) {
coordinatedIndex.get(0).type = "normal";
normalIndex.add(coordinatedIndex.get(0));
coordinatedIndex.clear();
}
if(normalIndex.size() > 0) {
for(int i = 0; i < normalIndex.size(); i++) {
attack(normalIndex.get(i), "normal");
}
}
//If there are evasive enemies
if(evasiveIndex.size() > 0) {
//For each evasive enemy in the evasiveIndex
for(int i = 0; i < evasiveIndex.size(); i++) {
attack(evasiveIndex.get(i), "normal"); //Call attack for each one
}
}
//If there are smart enemies
if(smartIndex.size() > 0) {
//For each smart enemy in the smartIndex
for(int i = 0; i < smartIndex.size(); i++) {
attack(smartIndex.get(i), "smart"); //Call attack for each one
}
}
}
//playerAttack() method - Handles player firing laser
public void playerAttack() {
//If player isn't loaded
if(!Game.player.loaded) {
//If the reload time is reached
if(Game.player.reloadTimer >= Game.player.reloadTime) {
Game.player.reloadTimer = 0; //Reset the reload timer to 0
Game.player.loaded = true; //Loaded is true
}
//Otherwise, count until the reload timer is met
else {
++Game.player.reloadTimer; //Increment reload timer
}
}
//If the player is loaded and space is pressed
if(Game.player.loaded && Game.player.space) {
lasers.add(new Laser(Game.player)); //Fire a laser
Game.player.loaded = false; //The player is no longer loaded
}
}
//animate() method - Animates the sprites
public void animate() {
//Animate each enemy
for(int i = 0; i < enemies.size(); i++) {
enemies.get(i).tick();
}
//Animate each missile
for(int i = 0; i < missiles.size(); i++) {
missiles.get(i).tick();
}
//Animate each laser
for(int i = 0; i < lasers.size(); i++) {
lasers.get(i).tick();
}
//Animate each explosion
for(int i = 0; i < explosions.size(); i++) {
explosions.get(i).tick();
}
}
//render() method - Renders everything
public void render(Graphics g) {
//Render map textures
levelTexture.render(g);
//Render enemies
for(int i = 0; i < enemies.size(); i++) {
enemies.get(i).render(g);
}
//Render missiles
for(int i = 0; i < missiles.size(); i++) {
missiles.get(i).render(g);
}
//Render lasers
for(int i = 0; i < lasers.size(); i++) {
lasers.get(i).render(g);
}
//Animate each explosion
for(int i = 0; i < explosions.size(); i++) {
explosions.get(i).render(g);
}
}
}