-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweapons.js
328 lines (213 loc) · 8.17 KB
/
weapons.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
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
var DEFAULT_FIRE_RATE = 450;
// Our core Bullet class
// This is a simple Sprite object that we set a few properties on
// It is fired by all of the Weapon classes
var Bullet = function (game, bulletType) {
Phaser.Sprite.call(this, game, 0, 0, 'bullet'+bulletType);
this.enableBody = true;
this.physicsBodyType = Phaser.Physics.P2JS;
game.physics.p2.enable(this);
this.anchor.set(0.5);
this.checkWorldBounds = true;
this.outOfBoundsKill = true;
this.exists = false;
this.tracking = false;
this.scaleSpeed = 0;
this.body.velocity.x = 0;
this.body.velocity.y = 0;
this.bulletType = bulletType;
};
Bullet.prototype = Object.create(Phaser.Sprite.prototype);
Bullet.prototype.constructor = Bullet;
Bullet.prototype.fire = function (x, y, angle, speed, gx, gy) {
if (this.bulletType == 0) {
this.body.setCollisionGroup(blackBulletGroup);
} else if (this.bulletType == 1) {
this.body.setCollisionGroup(whiteBulletGroup);
}
this.body.collides([playerCollisionGroup, shieldCollisionGroup]);
gx = gx || 0;
gy = gy || 0;
this.reset(x, y);
this.scale.set(1);
this.body.angle = angle;
var p = game.physics.arcade.velocityFromAngle(angle, speed);
this.body.velocity.x = p.x;
this.body.velocity.y = p.y;
this.angle = angle;
this.body.gravity.set(gx, gy);
};
Bullet.prototype.update = function () {
if (this.body.x < game.camera.x
|| this.body.x > game.camera.x + game.camera.width
|| this.body.y < game.camera.y
|| this.body.y > game.camera.y + game.camera.height){
this.kill();
}
if (this.tracking)
{
this.rotation = Math.atan2(this.body.velocity.y, this.body.velocity.x);
}
if (this.scaleSpeed > 0)
{
this.scale.x += this.scaleSpeed;
this.scale.y += this.scaleSpeed;
}
};
var Weapon = {};
function fireBullet(source, x, y, angle, speed, gx, gy){
var bullet = source.getFirstExists(false);
if(bullet === null){
return;
}
bullet.fire(x, y, angle, speed, gx, gy);
}
////////////////////////////////////////////////////
// A single bullet is fired in front of the ship //
////////////////////////////////////////////////////
Weapon.SingleBullet = function (game) {
Phaser.Group.call(this, game, game.world, 'Single Bullet', false, true, Phaser.Physics.ARCADE);
this.nextFire = 0;
this.bulletSpeed = 400;
this.fireRate = DEFAULT_FIRE_RATE;
for (var i = 0; i < 128; i++)
{
this.add(new Bullet(game, i % 2), true);
}
return this;
};
Weapon.SingleBullet.prototype = Object.create(Phaser.Group.prototype);
Weapon.SingleBullet.prototype.constructor = Weapon.SingleBullet;
Weapon.SingleBullet.prototype.fire = function (source) {
if (game.time.time < this.nextFire) { return; }
var x = source.x + 10;
var y = source.y + 10;
fireBullet(this, x,y,source.angle - 90, this.bulletSpeed, 0, 0);
this.nextFire = game.time.time + this.fireRate;
};
/////////////////////////////////////////////////////////
// A bullet is shot both in front and behind the ship //
/////////////////////////////////////////////////////////
Weapon.FrontAndBack = function (game) {
Phaser.Group.call(this, game, game.world, 'Front And Back', false, true, Phaser.Physics.ARCADE);
this.nextFire = 0;
this.bulletSpeed = 400;
this.fireRate = DEFAULT_FIRE_RATE;
for (var i = 0; i < 128; i++)
{
this.add(new Bullet(game, i % 2), true);
}
return this;
};
Weapon.FrontAndBack.prototype = Object.create(Phaser.Group.prototype);
Weapon.FrontAndBack.prototype.constructor = Weapon.FrontAndBack;
Weapon.FrontAndBack.prototype.fire = function (source) {
if (game.time.time < this.nextFire) { return; }
var x = source.x + 10;
var y = source.y + 10;
fireBullet(this, x,y,0, this.bulletSpeed, 0, 0);
fireBullet(this, x,y,180, this.bulletSpeed, 0, 0);
this.nextFire = game.time.time + this.fireRate;
};
//////////////////////////////////////////////////////
// 3-way Fire (directly above, below and in front) //
//////////////////////////////////////////////////////
Weapon.ThreeWay = function (game) {
Phaser.Group.call(this, game, game.world, 'Three Way', false, true, Phaser.Physics.ARCADE);
this.nextFire = 0;
this.bulletSpeed = 400;
this.fireRate = DEFAULT_FIRE_RATE;
for (var i = 0; i < 96; i++)
{
this.add(new Bullet(game, i % 2), true);
}
return this;
};
Weapon.ThreeWay.prototype = Object.create(Phaser.Group.prototype);
Weapon.ThreeWay.prototype.constructor = Weapon.ThreeWay;
Weapon.ThreeWay.prototype.fire = function (source) {
if (game.time.time < this.nextFire) { return; }
var x = source.x + 10;
var y = source.y + 10;
fireBullet(this, x,y,270, this.bulletSpeed, 0, 0);
fireBullet(this, x,y,0, this.bulletSpeed, 0, 0);
fireBullet(this, x,y,90, this.bulletSpeed, 0, 0);
this.nextFire = game.time.time + this.fireRate;
};
/////////////////////////////////////////////
// 8-way fire, from all sides of the ship //
/////////////////////////////////////////////
Weapon.EightWay = function (game) {
Phaser.Group.call(this, game, game.world, 'Eight Way', false, true, Phaser.Physics.ARCADE);
this.nextFire = 0;
this.bulletSpeed = 300;
this.fireRate = DEFAULT_FIRE_RATE;
for (var i = 0; i < 96; i++)
{
this.add(new Bullet(game, i % 2), true);
}
return this;
};
Weapon.EightWay.prototype = Object.create(Phaser.Group.prototype);
Weapon.EightWay.prototype.constructor = Weapon.EightWay;
Weapon.EightWay.prototype.fire = function (source) {
if (game.time.time < this.nextFire) { return; }
var x = source.x + 16;
var y = source.y + 10;
fireBullet(this, x,y,0, this.bulletSpeed, 0, 0);
fireBullet(this, x,y,45, this.bulletSpeed, 0, 0);
fireBullet(this, x,y,90, this.bulletSpeed, 0, 0);
fireBullet(this, x,y,135, this.bulletSpeed, 0, 0);
fireBullet(this, x,y,225, this.bulletSpeed, 0, 0);
fireBullet(this, x,y,270, this.bulletSpeed, 0, 0);
fireBullet(this, x,y,180, this.bulletSpeed, 0, 0);
fireBullet(this, x,y,315, this.bulletSpeed, 0, 0);
this.nextFire = game.time.time + this.fireRate;
};
////////////////////////////////////////////////////
// Bullets are fired out scattered on the y axis //
////////////////////////////////////////////////////
Weapon.ScatterShot = function (game) {
Phaser.Group.call(this, game, game.world, 'Scatter Shot', false, true, Phaser.Physics.ARCADE);
this.nextFire = 0;
this.bulletSpeed = 500;
this.fireRate = DEFAULT_FIRE_RATE;
for (var i = 0; i < 32; i++)
{
this.add(new Bullet(game, i % 2), true);
}
return this;
};
Weapon.ScatterShot.prototype = Object.create(Phaser.Group.prototype);
Weapon.ScatterShot.prototype.constructor = Weapon.ScatterShot;
Weapon.ScatterShot.prototype.fire = function (source) {
if (game.time.time < this.nextFire) { return; }
var x = source.x + 16;
var y = (source.y + source.height / 2) + game.rnd.between(-10, 10);
fireBullet(this, x, y, source.angle - 90, this.bulletSpeed, 0, 0);
this.nextFire = game.time.time + this.fireRate;
};
//////////////////////////////////////////////////////////////////////////
// Fires a streaming beam of lazers, very fast, in front of the player //
//////////////////////////////////////////////////////////////////////////
Weapon.Beam = function (game) {
Phaser.Group.call(this, game, game.world, 'Beam', false, true, Phaser.Physics.ARCADE);
this.nextFire = 0;
this.bulletSpeed = 1000;
this.fireRate = DEFAULT_FIRE_RATE;
for (var i = 0; i < 128; i++)
{
this.add(new Bullet(game, i % 2), true);
}
return this;
};
Weapon.Beam.prototype = Object.create(Phaser.Group.prototype);
Weapon.Beam.prototype.constructor = Weapon.Beam;
Weapon.Beam.prototype.fire = function (source) {
if (game.time.time < this.nextFire) { return; }
var x = source.x + 40;
var y = source.y + 10;
fireBullet(this, x, y, source.angle - 90, this.bulletSpeed, 0, 0);
this.getFirstExists(false).fire(x, y, 0, this.bulletSpeed, 0, 0);
this.nextFire = game.time.time + this.fireRate;
};