forked from JacobDanielsF/FinalProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoss.js
301 lines (262 loc) · 10.6 KB
/
Boss.js
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
// ARTG/CMPM 120 Final Project
// Tomb of the Ancients
// Boss.js
// Boss prefab
var bossSpawned = false;
// boss class
function Boss(game, posX, posY, type, roomtoggle, sprite, frame){
Phaser.Sprite.call(this, game, posX, posY, sprite, frame);
var scale = 1;
this.scale.x = scale;
this.scale.y = scale;
this.anchor.set(0.5);
this.type = type; // type of boss to spawn
this.room = roomtoggle;
game.physics.enable(this);
this.body.collideWorldBounds = false;
// checks boss type
if (type == "default"||type == "triple"){
this.nextfire = 4; // in seconds
this.firecooldown = 1; // time that must pass before the boss can shoot again
this.walkspeed = 100;
this.seekrange = 400; // If the player is in range, then the boss will begin to attack and move towards them.
}
if (type == "rapid"){
this.nextfire = ((game.time.now)/1000)+0.1;
this.firecooldown = 0.2;
this.walkspeed = 0.5;
this.seekrange = 400;
}
if (type == "turret"){
this.nextfire = 1;
this.firecooldown = 0.3;
this.nextWave = 2;
this.waveFire = 1;
this.walkspeed = 0.5; // 100% not creepy.
this.seekrange = 400;
}
game.add.existing(this);
this.poison = false; // status effect from Scorpion Dagger
this.body.immovable = true;
this.health = 30;
this.gemcount = 20; // gems dropped from the boss
healthBoss = game.add.text(game.camera.width/2, 32, 'Boss health: ' + (Math.floor(this.health)), { font: MAIN_FONT, fontStyle: MAIN_STYLE, fontSize: '20px', fill: '#ffffff' });
healthBoss.anchor.x = 0.5;
healthBoss.anchor.y = 0.5;
healthBoss.bringToTop();
healthBoss.fixedToCamera = true;
// adding this because the shots only go from the anchor the first time, then they come from the top left.
this.fireOnce = false;
this.animations.add('idle', Phaser.Animation.generateFrameNames('boss', 1, 12), 9, true);
this.animations.play('idle');
}
var healthBoss
Boss.prototype = Object.create(Phaser.Sprite.prototype);
Boss.prototype.constructor = Boss;
Boss.prototype.update = function() {
var time = (game.time.now)/1000;
healthBoss.setText('Boss health: ' + (Math.floor(this.health)));
if (InRange(player.body.x, player.body.y, this.body.x, this.body.y, this.seekrange) == true) { // if the player is within range.
// damage player if they hit an boss and give them invincibility frames (iframes)
// do not damage the player if they have invincibility frames
var bossHitWall = game.physics.arcade.collide(this, walls);
var bossHitCurrentWall = game.physics.arcade.collide(this, currentwalls);
var playerHitBoss = game.physics.arcade.collide(player, this);
if (playerHitBoss == true && iframes <= 0){
PLAYER_PROPERTIES.HEALTH--;
iframes = 20;
}
// how does they boss move? do they fire a projectile? if so, what kind?
// boss type determines actions here.
if (this.type == "default"){
var dirX = game.math.clamp((player.body.x - this.body.x)/128, -1, 1);
var dirY = game.math.clamp((player.body.y - this.body.y)/128, -1, 1);
this.body.velocity.x = dirX * this.walkspeed;
this.body.velocity.y = dirY * this.walkspeed;
if (time > this.nextfire){ // The way firerate is handled
var bullet = new BossProjectile(this.body.x + 8, this.body.y + 8, player.body.x, player.body.y, "default", 'enemyproj', 'sprite1');
enemybulletgroup.add(bullet);
this.nextfire = time + this.firecooldown; // this is the bullet rate of the weapon
bullet.body.velocity.x = dirX*bullet.speed;
bullet.body.velocity.y = dirY*bullet.speed;
this.animations.add('anim', Phaser.Animation.generateFrameNames('sprite', 1, 2), 8, true);
this.animations.play('anim');
}
}
if (this.type == "rapid"){ // this is also a turret.
var dirX = game.math.clamp((player.body.x - this.body.x)/128, -1, 1);
var dirY = game.math.clamp((player.body.y - this.body.y)/128, -1, 1);
this.body.velocity.x = dirX * this.walkspeed;
this.body.velocity.y = dirY * this.walkspeed;
if (time > this.nextfire){
var numProjectiles = 6
// var angleRandom = (Math.floor(Math.random()*3))/4 // occasionally shifts angle
for (var i=0;i<numProjectiles;i++){
var bullet = new BossProjectile(this.body.x + 64, this.body.y + 75, player.body.x, player.body.y, "rapid",'enemyproj', 'sprite1');
bullet.scale.setTo(1.5,1.5);
enemybulletgroup.add(bullet);
this.nextfire = time + this.firecooldown; // this is the bullet rate of the weapon
var fixY = Math.abs(dirY*bullet.speed);
var fixX = Math.abs(dirX*bullet.speed);
var fix
if(fixX>fixY){
fix = fixX;
}else{
fix = fixY;
}
var angleRandom = (Math.floor(Math.random()*3))/4 // shoots everywhere
var angle = game.physics.arcade.angleToXY(bullet, player.x,player.y) + ((-2*(Math.PI/numProjectiles)*(numProjectiles/2)) +(2*(Math.PI/numProjectiles)*i))+angleRandom;
var targetX = bullet.body.x+(Math.cos(angle)*70);
var targetY = bullet.body.y+(Math.sin(angle)*70);
game.physics.arcade.moveToXY(bullet, targetX, targetY, fix);
}
// this.animations.add('anim', Phaser.Animation.generateFrameNames('sprite', 1, 2), 8, true);
// this.animations.play('anim');
}
}
// Fires 3 projectiles towards the player
if (this.type == "triple"){
var dirX = [];
var dirY = [];
// change to 5
for (var i=0;i<3;i++){
var offset = -40+ (i*40);
dirX[i] = game.math.clamp((player.body.x+offset - this.body.x)/128, -1, 1);
dirY[i] = game.math.clamp((player.body.y+offset - this.body.y)/128, -1, 1);
}
this.body.velocity.x = dirX[1] * this.walkspeed;
this.body.velocity.y = dirY[1] * this.walkspeed;
if (time > this.nextfire){
var bullet = []
for (var i = 0;i<3;i++){
bullet[i] = new BossProjectile(this.body.x + 8, this.body.y + 8, player.body.x, player.body.y, "triple", 'enemyproj', 'sprite1',i);
enemybulletgroup.add(bullet[i]);
this.animations.add('anim', Phaser.Animation.generateFrameNames('sprite', 1, 2), 8, true);
this.animations.play('anim');
}
// rotation for bullets still needs to be adjusted here
this.nextfire = time + this.firecooldown; // this is the bullet rate of the weapon
for (var i = 0;i<3;i++){
bullet[i].body.velocity.x = dirX[i]*bullet[i].speed;
bullet[i].body.velocity.y = dirY[i]*bullet[i].speed;
}
}
}
if (this.type == "turret"){ // does not move (much).
var dirX = game.math.clamp((player.body.x - (this.body.x+64))/128, -1, 1);
var dirY = game.math.clamp((player.body.y - (this.body.y+75))/128, -1, 1);
this.body.velocity.x = dirX * this.walkspeed;
this.body.velocity.y = dirY * this.walkspeed;
if (time > this.nextfire){
if(this.fireOnce==false){ // this was a duct tape fix, as the first shot was always offset from the boss.
var bullet = new BossProjectile(this.body.x, this.body.y, player.body.x, player.body.y, "default",'enemyproj', 'sprite1');
}else{
var bullet = new BossProjectile(this.body.x + 64, this.body.y + 75, player.body.x, player.body.y, "default",'enemyproj', 'sprite1');
}
enemybulletgroup.add(bullet);
this.nextfire = time + this.firecooldown; // this is the bullet rate of the weapon
var fixY = Math.abs(dirY*bullet.speed);
var fixX = Math.abs(dirX*bullet.speed);
var fix
if(fixX>fixY){
fix = fixX;
}else{
fix = fixY;
}
game.physics.arcade.moveToObject(bullet,player, fix); // bullet moves towards player at the speed indicated by "fix"
}
if (time > this.nextWave){// the 4 projectiles that shoot out to the corners
var wave = []
for (var i = 0; i<4; i++){
// Basically a duct tape fix. Problem described above the initial this.fireOnce.
if(this.fireOnce==false){
wave[i] = new BossProjectile(this.body.x, this.body.y, player.body.x, player.body.y, "default",'enemyproj', 'sprite1',i);
if(i>2){
this.fireOnce=true;
}
}else{
wave[i] = new BossProjectile(this.body.x + 64, this.body.y + 75, player.body.x, player.body.y, "default",'enemyproj', 'sprite1',i);
}
enemybulletgroup.add(wave[i]);
}
// rotation for bullets still needs to be adjusted here
this.nextWave = time + this.waveFire; // this is the bullet rate of the weapon
// These each fire projectiles at a corner. 4 in total.
wave[0].body.velocity.x = -1*wave[0].speed;
wave[0].body.velocity.y = -1*wave[0].speed;
wave[1].body.velocity.x = -1*wave[1].speed;
wave[1].body.velocity.y = 1*wave[1].speed;
wave[2].body.velocity.x = 1*wave[2].speed;
wave[2].body.velocity.y = -1*wave[2].speed;
wave[3].body.velocity.x = 1*wave[3].speed;
wave[3].body.velocity.y = 1*wave[3].speed;
}
}
} else { // if the player is not in range
// boss idle
this.body.velocity.x = 0;
this.body.velocity.y = 0;
}
// Checks for collision between each player bullet and the boss.
playerbulletgroup.forEach(function(bullet) {
if (bullet != null){
var bulletHitBoss = game.physics.arcade.collide(bullet, this);
// delete the bullet if it hits an boss, damaging the boss
if (bulletHitBoss == true){
if (bullet.type == "Scorpion Dagger" && this.poison == false){
this.poison = true;
this.walkspeed /= 2;
}
if (bullet.type == "Wooden Crossbow" || bullet.type == "Short Bow" || bullet.type == "Composite Crossbow"){
rangedenemyhitfx.play();
} else {
enemyhitfx.play();
}
bullet.kill();
bullet.destroy();
// boss is damaged, delete boss if it dies
this.health -= bullet.damage;
if (this.health < 0) {
healthBoss.setText("");
bosscomplete = true;
SpawnGems(this.gemcount, this.body.x + 64, this.body.y + 75, 60, 90);
this.kill();
this.destroy();
}
}
}
}, this);
var slash = playerslash;
if (slash != null){
// checks to see if any slash hitboxes from melee weapons have hit the boss.
for (var k = 0; k < slash.hitboxes.length; k++){
var box = slash.hitboxes[k];
// check for bullet-boss collision
var boxHitBoss = game.physics.arcade.collide(box, this);
// delete the bullet if it hits an boss and damage the boss
if (boxHitBoss == true){
// boss is damaged, delete boss if it dies
this.health -= slash.damage;
if (this.health < 0) {
healthBoss.setText("");
bosscomplete = true;
SpawnGems(this.gemcount, this.body.x, this.body.y, 60, 90);
this.kill();
this.destroy();
}
}
}
}
// poison effect
if (this.poison == true){
this.health -= 0.1; // damage over time
if (this.health < 0){
healthBoss.setText("");
bosscomplete = true;
SpawnGems(this.gemcount, this.body.x, this.body.y, 70, 110);
// removes the boss
this.kill();
this.destroy();
}
}
}