-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFireBound.java
More file actions
281 lines (212 loc) · 8.67 KB
/
FireBound.java
File metadata and controls
281 lines (212 loc) · 8.67 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
import java.awt.Color;
import java.util.HashSet;
import java.util.Set;
public class FireBound extends ScrollingGame{
public static final String BACKGROUND_IMAGE = "game_assets/dungeon.gif";
public static final String MUSIC = "sound_effects/intro.wav";
public static final String SPLASH_IMAGE = "game_assets/FireBoundSplash.gif";;
// Initialize Player
public static final int SPEED_UP_INTERVAL = 1000;
public static final int INC_CONS_SPEED = 1;
public static final int INIT_SPEED = 60;
public static final int GAME_SPEED_CHECK_VAL= 240;
public static final int TARGET_SPAWN_INTERVAL = 1000;
protected static final int SCORE_TO_WIN = 2000;
protected static final int TARGET_SCORE_TO_WIN = 4;
protected static final int GROWTH_TO_WIN = 100;
protected static final int GROWTH_CHANGE = 40;
protected static final int CHANGE_INTERVAL = 20;
private int gu= 0;
protected Set<Projectile> shootSet = new HashSet<Projectile>();
public FireBound(){
super();
}
public FireBound(int gameWidth, int gameHeight){
super(gameWidth, gameHeight);
}
@Override
protected void pregame(){
this.setBackgroundColor(Color.black);
this.setBackgroundImage(BACKGROUND_IMAGE);
this.setSplashImage(SPLASH_IMAGE);
player = new Fugi(STARTING_PLAYER_X, STARTING_PLAYER_Y);
PlayMusic.RunMusic(MUSIC);
displayList.add(player);
score = 0;
}
protected void incUnivSpeed(){
Enemy.setUnivScrollSpeed(Enemy.getUnivScrollSpeed()+1);
Log.setUnivScrollSpeed(Log.getUnivScrollSpeed()+1);
Coal.setUnivScrollSpeed(Coal.getUnivScrollSpeed()+1);
}
@Override
protected void updateGame(){
//scroll all scrollable Entities on the game board
scrollEntities();
//Spawn new entities only at a certain interval
if (super.getTicksElapsed() % SPAWN_INTERVAL == 0){
spawnEntities();
}
if(super.getTicksElapsed() % (RAREGET_SPAWN_INTERVAL) == 0){
handleOverlap(this.spawnRareGet());
}
if(super.getTicksElapsed() % (TARGET_SPAWN_INTERVAL) == 0){
handleOverlap(spawnTarget());
}
if(super.getTicksElapsed() % (SPEED_UP_INTERVAL) == 0){
incUnivSpeed();
}
if(gu >= GROWTH_CHANGE && player.getImageName() !=Fugi.FUGI_IMAGE_DAMAGE2){
((Entity)player).setImageName(Fugi.FUGI_IMAGE_FILE2);
}
else if(gu < GROWTH_CHANGE && player.getImageName() !=Fugi.FUGI_IMAGE_DAMAGE){
((Entity)player).setImageName(Fugi.FUGI_IMAGE_FILE);
}
if(super.getTicksElapsed() % CHANGE_INTERVAL == 0 && player.getImageName() == Fugi.FUGI_IMAGE_DAMAGE)
player.setImageName(Fugi.FUGI_IMAGE_FILE);
if(super.getTicksElapsed() % CHANGE_INTERVAL== 0 && player.getImageName() == Fugi.FUGI_IMAGE_DAMAGE2)
player.setImageName(Fugi.FUGI_IMAGE_FILE2);
for(Entity ent : displayList){
if(ent instanceof Consumable){
handlePlayerCollision((Consumable)ent);
handleProjectileCollision((Entity)ent);
}
}
//Update the title text on the top of the window
setTitleText("HP: " + player.getHP() + " | Ammo: " + ((Fugi)player).getAmmo()+ " | Score: " + score + " | Growth Units: " + gu + " | Targets Shooten: " + ((Fugi)player).getTargs() );
}
protected Entity spawnTarget(){
int x = getWindowWidth();
int y = genRandY();
Target target = new Target(x,y);
displayList.add(target);
return target;
}
//Handleing overlaps
protected void handleProjectileCollision(Entity collidedWith){
for(Projectile shoot : shootSet)
if (shoot == null)
return;
else if(collidedWith instanceof Target && shoot.isCollidingWith(collidedWith)){
((Fugi)player).setTargs(((Fugi)player).getTargs()+1);
toBeGC.add(collidedWith);
}
else if(collidedWith instanceof Avoid && shoot.isCollidingWith(collidedWith)){
toBeGC.add(collidedWith);
}
}
@Override
protected void handlePlayerCollision(Consumable collidedWith) {
if(player.isCollidingWith((Entity)collidedWith)){
player.setHP(player.getHP() + collidedWith.getDamage());
score += collidedWith.getPoints();
changeSkin(collidedWith);
checkRareGetCol(collidedWith);
checkGetCol(collidedWith);
checkAvoidCol(collidedWith);
toBeGC.add((Entity)collidedWith);
}
}
@Override
protected void spawnEntities() {
for(int i =0; i<rand.nextInt(4);i++){
handleOverlap(this.spawnAvoid());
handleOverlap(this.spawnGet());
}
}
@Override
protected Entity spawnAvoid() {
int x = getWindowWidth();
int y = genRandY();
Enemy nextEnemy = new Enemy(x,y);
displayList.add(nextEnemy);
return nextEnemy;
}
@Override
protected Entity spawnGet() {
int x = getWindowWidth();
int y = genRandY();
Log nextLog = new Log(x,y);
displayList.add(nextLog);
return nextLog;
}
@Override
protected Entity spawnRareGet(){
int x = getWindowWidth();
int y = genRandY();
Coal nextCoal = new Coal(x,y);
displayList.add(nextCoal);
return nextCoal;
}
protected void checkAvoidCol(Consumable collidedWith){
if (collidedWith instanceof Avoid && (player.getHeight()>50 && player.getWidth()>30)){
player.setHeight(player.getHeight()-5);
player.setWidth(player.getWidth()-5);
gu-=2;
}
}
protected void checkRareGetCol(Consumable collidedWith){
if (collidedWith instanceof RareGet && gu<100){
((Fugi)player).setAmmo(((Fugi)player).getAmmo()+3);
player.setHeight(player.getHeight()+5);
player.setWidth(player.getWidth()+5);
gu+=2;
}
else if (collidedWith instanceof RareGet)
((Fugi)player).setAmmo(((Fugi)player).getAmmo()+3);
}
protected void checkGetCol(Consumable collidedWith){
if (collidedWith instanceof Get && gu<100){
((Fugi)player).setAmmo(((Fugi)player).getAmmo()+1);
player.setHeight(player.getHeight()+1);
player.setWidth(player.getWidth()+1);
gu++;
}
else if (collidedWith instanceof Get)
((Fugi)player).setAmmo(((Fugi)player).getAmmo()+1);
}
protected void changeSkin(Consumable collidedWith){
if(collidedWith instanceof Avoid && player.getImageName() == Fugi.FUGI_IMAGE_FILE)
player.setImageName(Fugi.FUGI_IMAGE_DAMAGE);
if(collidedWith instanceof Avoid && player.getImageName() == Fugi.FUGI_IMAGE_FILE2)
player.setImageName(Fugi.FUGI_IMAGE_DAMAGE2);
}
@Override
public void playerMovement(int key){
if(key == UP_KEY && player.getY() > 0 )
player.setY(player.getY()- player.getMovementSpeed());
else if(key == DOWN_KEY && player.getY() < super.getWindowHeight()-(Fugi.FUGI_HEIGHT+ player.getMovementSpeed()))
player.setY(player.getY()+ player.getMovementSpeed());
else if(key == LEFT_KEY && player.getX() > 0)
player.setX(player.getX()-player.getMovementSpeed());
else if(key == RIGHT_KEY && player.getX() < super.getWindowWidth()- (Fugi.FUGI_WIDTH+ player.getMovementSpeed()))
player.setX(player.getX()+player.getMovementSpeed());
else if(key == KEY_SHOOT && ((Fugi)player).getAmmo() > 0){
Projectile shoot = new Projectile(player.getX(), player.getY());
this.shootSet.add(shoot);
displayList.add(shoot);
((Fugi)player).setAmmo(((Fugi)player).getAmmo()-1);
}
}
@Override
protected boolean isGameOver() {
if(score >= SCORE_TO_WIN && ((Fugi)player).getTargs() >= TARGET_SCORE_TO_WIN && gu >= GROWTH_TO_WIN)
return true;
else if(player.getHP() == 0)
return true;
return false;
}
@Override
protected void scrollEntities() {
for(Entity ent : displayList){
if (ent instanceof Scrollable)
((Scrollable)ent).scroll();
if(ent.getX()< -100)
toBeGC.add(ent);
if((ent instanceof Projectile && ent.getX() > getWindowWidth())){
toBeGC.add(ent);
shootSet.remove(ent);
}
}
}
}