-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
WeaponPlugin.js
1225 lines (1009 loc) · 40.3 KB
/
WeaponPlugin.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <[email protected]>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Weapon Plugin provides the ability to easily create a bullet pool and manager.
*
* Weapons fire Phaser.Bullet objects, which are essentially Sprites with a few extra properties.
* The Bullets are enabled for Arcade Physics. They do not currently work with P2 Physics.
*
* The Bullets are created inside of `Weapon.bullets`, which is a Phaser.Group instance. Anything you
* can usually do with a Group, such as move it around the display list, iterate it, etc can be done
* to the bullets Group too.
*
* Bullets can have textures and even animations. You can control the speed at which they are fired,
* the firing rate, the firing angle, and even set things like gravity for them.
*
* A small example, assumed to be running from within a Phaser.State create method.
*
* `var weapon = this.add.weapon(10, 'bullet');`
* `weapon.fireFrom.set(300, 300);`
* `this.input.onDown.add(weapon.fire, this);`
*
* @class Phaser.Weapon
* @constructor
* @param {Phaser.Game} game - A reference to the current Phaser.Game instance.
* @param {Phaser.PluginManager} parent - The Phaser Plugin Manager which looks after this plugin.
*/
Phaser.Weapon = function (game, parent) {
Phaser.Plugin.call(this, game, parent);
/**
* This is the Phaser.Group that contains all of the bullets managed by this plugin.
* @type {Phaser.Group}
*/
this.bullets = null;
/**
* Should the bullet pool run out of bullets (i.e. they are all in flight) then this
* boolean controls if the Group will create a brand new bullet object or not.
* @type {boolean}
*/
this.autoExpandBulletsGroup = false;
/**
* Will this weapon auto fire? If set to true then a new bullet will be fired
* based on the `fireRate` value.
* @type {boolean}
*/
this.autofire = false;
/**
* The total number of bullets this Weapon has fired so far.
* You can limit the number of shots allowed (via `fireLimit`), and reset
* this total via `Weapon.resetShots`.
* @type {number}
*/
this.shots = 0;
/**
* The maximum number of shots that this Weapon is allowed to fire before it stops.
* When the limit is his the `Weapon.onFireLimit` Signal is dispatched.
* You can reset the shot counter via `Weapon.resetShots`.
* @type {number}
*/
this.fireLimit = 0;
/**
* The rate at which this Weapon can fire. The value is given in milliseconds.
* @type {number}
*/
this.fireRate = 100;
/**
* This is a modifier that is added to the `fireRate` each update to add variety
* to the firing rate of the Weapon. The value is given in milliseconds.
* If you've a `fireRate` of 200 and a `fireRateVariance` of 50 then the actual
* firing rate of the Weapon will be between 150 and 250.
* @type {number}
*/
this.fireRateVariance = 0;
/**
* This is a Rectangle from within which the bullets are fired. By default it's a 1x1
* rectangle, the equivalent of a Point. But you can change the width and height, and if
* larger than 1x1 it'll pick a random point within the rectangle to launch the bullet from.
* @type {Phaser.Rectangle}
*/
this.fireFrom = new Phaser.Rectangle(0, 0, 1, 1);
/**
* The angle at which the bullets are fired. This can be a const such as Phaser.ANGLE_UP
* or it can be any number from 0 to 360 inclusive, where 0 degrees is to the right.
* @type {integer}
*/
this.fireAngle = Phaser.ANGLE_UP;
/**
* When a Bullet is fired it can optionally inherit the velocity of the `trackedSprite` if set.
* @type {boolean}
*/
this.bulletInheritSpriteSpeed = false;
/**
* The string based name of the animation that the Bullet will be given on launch.
* This is set via `Weapon.addBulletAnimation`.
* @type {string}
*/
this.bulletAnimation = '';
/**
* If you've added a set of frames via `Weapon.setBulletFrames` then you can optionally
* chose for each Bullet fired to pick a random frame from the set.
* @type {boolean}
*/
this.bulletFrameRandom = false;
/**
* If you've added a set of frames via `Weapon.setBulletFrames` then you can optionally
* chose for each Bullet fired to use the next frame in the set. The frame index is then
* advanced one frame until it reaches the end of the set, then it starts from the start
* again. Cycling frames like this allows you to create varied bullet effects via
* sprite sheets.
* @type {boolean}
*/
this.bulletFrameCycle = false;
/**
* Should the Bullets wrap around the world bounds? This automatically calls
* `World.wrap` on the Bullet each frame. See the docs for that method for details.
* @type {boolean}
*/
this.bulletWorldWrap = false;
/**
* If `bulletWorldWrap` is true then you can provide an optional padding value with this
* property. It's added to the calculations determining when the Bullet should wrap around
* the world or not. The value is given in pixels.
* @type {integer}
*/
this.bulletWorldWrapPadding = 0;
/**
* An optional angle offset applied to the Bullets when they are launched.
* This is useful if for example your bullet sprites have been drawn facing up, instead of
* to the right, and you want to fire them at an angle. In which case you can set the
* angle offset to be 90 and they'll be properly rotated when fired.
* @type {number}
*/
this.bulletAngleOffset = 0;
/**
* This is a variance added to the angle of Bullets when they are fired.
* If you fire from an angle of 90 and have a `bulletAngleVariance` of 20 then the actual
* angle of the Bullets will be between 70 and 110 degrees. This is a quick way to add a
* great 'spread' effect to a Weapon.
* @type {number}
*/
this.bulletAngleVariance = 0;
/**
* The speed at which the bullets are fired. This value is given in pixels per second, and
* is used to set the starting velocity of the bullets.
* @type {number}
*/
this.bulletSpeed = 200;
/**
* This is a variance added to the speed of Bullets when they are fired.
* If bullets have a `bulletSpeed` value of 200, and a `bulletSpeedVariance` of 50
* then the actual speed of the Bullets will be between 150 and 250 pixels per second.
* @type {number}
*/
this.bulletSpeedVariance = 0;
/**
* If you've set `bulletKillType` to `Phaser.Weapon.KILL_LIFESPAN` this controls the amount
* of lifespan the Bullets have set on launch. The value is given in milliseconds.
* When a Bullet hits its lifespan limit it will be automatically killed.
* @type {number}
*/
this.bulletLifespan = 0;
/**
* If you've set `bulletKillType` to `Phaser.Weapon.KILL_DISTANCE` this controls the distance
* the Bullet can travel before it is automatically killed. The distance is given in pixels.
* @type {number}
*/
this.bulletKillDistance = 0;
/**
* This is the amount of gravity added to the Bullets physics body when fired.
* Gravity is expressed in pixels / second / second.
* @type {Phaser.Point}
*/
this.bulletGravity = new Phaser.Point(0, 0);
/**
* Bullets can optionally adjust their rotation in-flight to match their velocity.
* This can create the effect of a bullet 'pointing' to the path it is following, for example
* an arrow being fired from a bow, and works especially well when added to `bulletGravity`.
* @type {boolean}
*/
this.bulletRotateToVelocity = false;
/**
* The Texture Key that the Bullets use when rendering.
* Changing this has no effect on bullets in-flight, only on newly spawned bullets.
* @type {string}
*/
this.bulletKey = '';
/**
* The Texture Frame that the Bullets use when rendering.
* Changing this has no effect on bullets in-flight, only on newly spawned bullets.
* @type {string|integer}
*/
this.bulletFrame = '';
/**
* Private var that holds the public `bulletClass` property.
* @type {object}
* @private
*/
this._bulletClass = Phaser.Bullet;
/**
* Private var that holds the public `bulletCollideWorldBounds` property.
* @type {boolean}
* @private
*/
this._bulletCollideWorldBounds = false;
/**
* Private var that holds the public `bulletKillType` property.
* @type {integer}
* @private
*/
this._bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
/**
* Holds internal data about custom bullet body sizes.
*
* @type {Object}
* @private
*/
this._data = {
customBody: false,
width: 0,
height: 0,
offsetX: 0,
offsetY: 0
};
/**
* This Rectangle defines the bounds that are used when determining if a Bullet should be killed or not.
* It's used in combination with `Weapon.bulletKillType` when that is set to either `Phaser.Weapon.KILL_WEAPON_BOUNDS`
* or `Phaser.Weapon.KILL_STATIC_BOUNDS`. If you are not using either of these kill types then the bounds are ignored.
* If you are tracking a Sprite or Point then the bounds are centered on that object every frame.
*
* @type {Phaser.Rectangle}
*/
this.bounds = new Phaser.Rectangle();
/**
* The Rectangle used to calculate the bullet bounds from.
*
* @type {Phaser.Rectangle}
* @private
*/
this.bulletBounds = game.world.bounds;
/**
* This array stores the frames added via `Weapon.setBulletFrames`.
*
* @type {Array}
* @protected
*/
this.bulletFrames = [];
/**
* The index of the frame within `Weapon.bulletFrames` that is currently being used.
* This value is only used if `Weapon.bulletFrameCycle` is set to `true`.
* @type {number}
* @private
*/
this.bulletFrameIndex = 0;
/**
* An internal object that stores the animation data added via `Weapon.addBulletAnimation`.
* @type {Object}
* @private
*/
this.anims = {};
/**
* The onFire Signal is dispatched each time `Weapon.fire` is called, and a Bullet is
* _successfully_ launched. The callback is set two arguments: a reference to the bullet sprite itself,
* and a reference to the Weapon that fired the bullet.
*
* @type {Phaser.Signal}
*/
this.onFire = new Phaser.Signal();
/**
* The onKill Signal is dispatched each time a Bullet that is in-flight is killed. This can be the result
* of leaving the Weapon bounds, an expiring lifespan, or exceeding a specified distance.
* The callback is sent one argument: A reference to the bullet sprite itself.
*
* @type {Phaser.Signal}
*/
this.onKill = new Phaser.Signal();
/**
* The onFireLimit Signal is dispatched if `Weapon.fireLimit` is > 0, and a bullet launch takes the number
* of shots fired to equal the fire limit.
* The callback is sent two arguments: A reference to the Weapon that hit the limit, and the value of
* `Weapon.fireLimit`.
*
* @type {Phaser.Signal}
*/
this.onFireLimit = new Phaser.Signal();
/**
* The Sprite currently being tracked by the Weapon, if any.
* This is set via the `Weapon.trackSprite` method.
*
* @type {Phaser.Sprite|Object}
*/
this.trackedSprite = null;
/**
* The Pointer currently being tracked by the Weapon, if any.
* This is set via the `Weapon.trackPointer` method.
*
* @type {Phaser.Pointer}
*/
this.trackedPointer = null;
/**
* If the Weapon is tracking a Sprite, should it also track the Sprites rotation?
* This is useful for a game such as Asteroids, where you want the weapon to fire based
* on the sprites rotation.
*
* @type {boolean}
*/
this.trackRotation = false;
/**
* The Track Offset is a Point object that allows you to specify a pixel offset that bullets use
* when launching from a tracked Sprite or Pointer. For example if you've got a bullet that is 2x2 pixels
* in size, but you're tracking a Sprite that is 32x32, then you can set `trackOffset.x = 16` to have
* the bullet launched from the center of the Sprite.
*
* @type {Phaser.Point}
*/
this.trackOffset = new Phaser.Point();
/**
* Internal firing rate time tracking variable.
*
* @type {number}
* @private
*/
this._nextFire = 0;
};
Phaser.Weapon.prototype = Object.create(Phaser.Plugin.prototype);
Phaser.Weapon.prototype.constructor = Phaser.Weapon;
/**
* A `bulletKillType` constant that stops the bullets from ever being destroyed automatically.
* @constant
* @type {integer}
*/
Phaser.Weapon.KILL_NEVER = 0;
/**
* A `bulletKillType` constant that automatically kills the bullets when their `bulletLifespan` expires.
* @constant
* @type {integer}
*/
Phaser.Weapon.KILL_LIFESPAN = 1;
/**
* A `bulletKillType` constant that automatically kills the bullets after they
* exceed the `bulletDistance` from their original firing position.
* @constant
* @type {integer}
*/
Phaser.Weapon.KILL_DISTANCE = 2;
/**
* A `bulletKillType` constant that automatically kills the bullets when they leave the `Weapon.bounds` rectangle.
* @constant
* @type {integer}
*/
Phaser.Weapon.KILL_WEAPON_BOUNDS = 3;
/**
* A `bulletKillType` constant that automatically kills the bullets when they leave the `Camera.bounds` rectangle.
* @constant
* @type {integer}
*/
Phaser.Weapon.KILL_CAMERA_BOUNDS = 4;
/**
* A `bulletKillType` constant that automatically kills the bullets when they leave the `World.bounds` rectangle.
* @constant
* @type {integer}
*/
Phaser.Weapon.KILL_WORLD_BOUNDS = 5;
/**
* A `bulletKillType` constant that automatically kills the bullets when they leave the `Weapon.bounds` rectangle.
* @constant
* @type {integer}
*/
Phaser.Weapon.KILL_STATIC_BOUNDS = 6;
/**
* This method performs two actions: First it will check to see if the `Weapon.bullets` Group exists or not,
* and if not it creates it, adding it the `group` given as the 4th argument.
*
* Then it will seed the bullet pool with the `quantity` number of Bullets, using the texture key and frame
* provided (if any).
*
* If for example you set the quantity to be 10, then this Weapon will only ever be able to have 10 bullets
* in-flight simultaneously. If you try to fire an 11th bullet then nothing will happen until one, or more, of
* the in-flight bullets have been killed, freeing them up for use by the Weapon again.
*
* If you do not wish to have a limit set, then pass in -1 as the quantity. In this instance the Weapon will
* keep increasing the size of the bullet pool as needed. It will never reduce the size of the pool however,
* so be careful it doesn't grow too large.
*
* You can either set the texture key and frame here, or via the `Weapon.bulletKey` and `Weapon.bulletFrame`
* properties. You can also animate bullets, or set them to use random frames. All Bullets belonging to a
* single Weapon instance must share the same texture key however.
*
* @method Phaser.Weapon#createBullets
* @param {integer} [quantity=1] - The quantity of bullets to seed the Weapon with. If -1 it will set the pool to automatically expand.
* @param {string} [key] - The Game.cache key of the image that this Sprite will use.
* @param {integer|string} [frame] - If the Sprite image contains multiple frames you can specify which one to use here.
* @param {Phaser.Group} [group] - Optional Group to add the object to. If not specified it will be added to the World group.
* @return {Phaser.Weapon} This Weapon instance.
*/
Phaser.Weapon.prototype.createBullets = function (quantity, key, frame, group) {
if (quantity === undefined) { quantity = 1; }
if (group === undefined) { group = this.game.world; }
if (!this.bullets)
{
this.bullets = this.game.add.physicsGroup(Phaser.Physics.ARCADE, group);
this.bullets.classType = this._bulletClass;
}
if (quantity !== 0)
{
if (quantity === -1)
{
this.autoExpandBulletsGroup = true;
quantity = 1;
}
this.bullets.createMultiple(quantity, key, frame);
this.bullets.setAll('data.bulletManager', this);
this.bulletKey = key;
this.bulletFrame = frame;
}
return this;
};
/**
* Call a function on each in-flight bullet in this Weapon.
*
* See {@link Phaser.Group#forEachExists forEachExists} for more details.
*
* @method Phaser.Weapon#forEach
* @param {function} callback - The function that will be called for each applicable child. The child will be passed as the first argument.
* @param {object} callbackContext - The context in which the function should be called (usually 'this').
* @param {...any} [args=(none)] - Additional arguments to pass to the callback function, after the child item.
* @return {Phaser.Weapon} This Weapon instance.
*/
Phaser.Weapon.prototype.forEach = function (callback, callbackContext) {
this.bullets.forEachExists(callback, callbackContext, arguments);
return this;
};
/**
* Sets `Body.enable` to `false` on each bullet in this Weapon.
* This has the effect of stopping them in-flight should they be moving.
* It also stops them being able to be checked for collision.
*
* @method Phaser.Weapon#pauseAll
* @return {Phaser.Weapon} This Weapon instance.
*/
Phaser.Weapon.prototype.pauseAll = function () {
this.bullets.setAll('body.enable', false);
return this;
};
/**
* Sets `Body.enable` to `true` on each bullet in this Weapon.
* This has the effect of resuming their motion should they be in-flight.
* It also enables them for collision checks again.
*
* @method Phaser.Weapon#resumeAll
* @return {Phaser.Weapon} This Weapon instance.
*/
Phaser.Weapon.prototype.resumeAll = function () {
this.bullets.setAll('body.enable', true);
return this;
};
/**
* Calls `Bullet.kill` on every in-flight bullet in this Weapon.
* Also re-enables their physics bodies, should they have been disabled via `pauseAll`.
*
* @method Phaser.Weapon#killAll
* @return {Phaser.Weapon} This Weapon instance.
*/
Phaser.Weapon.prototype.killAll = function () {
this.bullets.callAllExists('kill', true);
this.bullets.setAll('body.enable', true);
return this;
};
/**
* Resets the `Weapon.shots` counter back to zero. This is used when you've set
* `Weapon.fireLimit`, and have hit (or just wish to reset) your limit.
*
* @method Phaser.Weapon#resetShots
* @param {integer} [newLimit] - Optionally set a new `Weapon.fireLimit`.
* @return {Phaser.Weapon} This Weapon instance.
*/
Phaser.Weapon.prototype.resetShots = function (newLimit) {
this.shots = 0;
if (newLimit !== undefined)
{
this.fireLimit = newLimit;
}
return this;
};
/**
* Destroys this Weapon. It removes itself from the PluginManager, destroys
* the bullets Group, and nulls internal references.
*
* @method Phaser.Weapon#destroy
*/
Phaser.Weapon.prototype.destroy = function () {
this.parent.remove(this, false);
this.bullets.destroy();
this.game = null;
this.parent = null;
this.active = false;
this.visible = false;
};
/**
* Internal update method, called by the PluginManager.
*
* @method Phaser.Weapon#update
* @protected
*/
Phaser.Weapon.prototype.update = function () {
if (this._bulletKillType === Phaser.Weapon.KILL_WEAPON_BOUNDS)
{
if (this.trackedSprite)
{
this.trackedSprite.updateTransform();
this.bounds.centerOn(this.trackedSprite.worldPosition.x, this.trackedSprite.worldPosition.y);
}
else if (this.trackedPointer)
{
this.bounds.centerOn(this.trackedPointer.worldX, this.trackedPointer.worldY);
}
}
if (this.autofire && this.game.time.now < this._nextFire)
{
this.fire();
}
};
/**
* Sets this Weapon to track the given Sprite, or any Object with a public `world` Point object.
* When a Weapon tracks a Sprite it will automatically update its `fireFrom` value to match the Sprites
* position within the Game World, adjusting the coordinates based on the offset arguments.
*
* This allows you to lock a Weapon to a Sprite, so that bullets are always launched from its location.
*
* Calling `trackSprite` will reset `Weapon.trackedPointer` to null, should it have been set, as you can
* only track _either_ a Sprite, or a Pointer, at once, but not both.
*
* @method Phaser.Weapon#trackSprite
* @param {Phaser.Sprite|Object} sprite - The Sprite to track the position of.
* @param {integer} [offsetX=0] - The horizontal offset from the Sprites position to be applied to the Weapon.
* @param {integer} [offsetY=0] - The vertical offset from the Sprites position to be applied to the Weapon.
* @param {boolean} [trackRotation=false] - Should the Weapon also track the Sprites rotation?
* @return {Phaser.Weapon} This Weapon instance.
*/
Phaser.Weapon.prototype.trackSprite = function (sprite, offsetX, offsetY, trackRotation) {
if (offsetX === undefined) { offsetX = 0; }
if (offsetY === undefined) { offsetY = 0; }
if (trackRotation === undefined) { trackRotation = false; }
this.trackedPointer = null;
this.trackedSprite = sprite;
this.trackRotation = trackRotation;
this.trackOffset.set(offsetX, offsetY);
return this;
};
/**
* Sets this Weapon to track the given Pointer.
* When a Weapon tracks a Pointer it will automatically update its `fireFrom` value to match the Pointers
* position within the Game World, adjusting the coordinates based on the offset arguments.
*
* This allows you to lock a Weapon to a Pointer, so that bullets are always launched from its location.
*
* Calling `trackPointer` will reset `Weapon.trackedSprite` to null, should it have been set, as you can
* only track _either_ a Pointer, or a Sprite, at once, but not both.
*
* @method Phaser.Weapon#trackPointer
* @param {Phaser.Pointer} [pointer] - The Pointer to track the position of. Defaults to `Input.activePointer` if not specified.
* @param {integer} [offsetX=0] - The horizontal offset from the Pointers position to be applied to the Weapon.
* @param {integer} [offsetY=0] - The vertical offset from the Pointers position to be applied to the Weapon.
* @return {Phaser.Weapon} This Weapon instance.
*/
Phaser.Weapon.prototype.trackPointer = function (pointer, offsetX, offsetY) {
if (pointer === undefined) { pointer = this.game.input.activePointer; }
if (offsetX === undefined) { offsetX = 0; }
if (offsetY === undefined) { offsetY = 0; }
this.trackedPointer = pointer;
this.trackedSprite = null;
this.trackRotation = false;
this.trackOffset.set(offsetX, offsetY);
return this;
};
/**
* Attempts to fire a single Bullet. If there are no more bullets available in the pool, and the pool cannot be extended,
* then this method returns `false`. It will also return false if not enough time has expired since the last time
* the Weapon was fired, as defined in the `Weapon.fireRate` property.
*
* Otherwise the first available bullet is selected and launched.
*
* The arguments are all optional, but allow you to control both where the bullet is launched from, and aimed at.
*
* If you don't provide any of the arguments then it uses those set via properties such as `Weapon.trackedSprite`,
* `Weapon.bulletAngle` and so on.
*
* When the bullet is launched it has its texture and frame updated, as required. The velocity of the bullet is
* calculated based on Weapon properties like `bulletSpeed`.
*
* @method Phaser.Weapon#fire
* @param {Phaser.Sprite|Phaser.Point|Object} [from] - Optionally fires the bullet **from** the `x` and `y` properties of this object. If set this overrides `Weapon.trackedSprite` or `trackedPointer`. Pass `null` to ignore it.
* @param {number} [x] - The x coordinate, in world space, to fire the bullet **towards**. If left as `undefined` the bullet direction is based on its angle.
* @param {number} [y] - The y coordinate, in world space, to fire the bullet **towards**. If left as `undefined` the bullet direction is based on its angle.
* @return {boolean} True if a bullet was successfully fired, otherwise false.
*/
Phaser.Weapon.prototype.fire = function (from, x, y) {
if (this.game.time.now < this._nextFire || (this.fireLimit > 0 && this.shots === this.fireLimit))
{
return false;
}
var speed = this.bulletSpeed;
// Apply +- speed variance
if (this.bulletSpeedVariance !== 0)
{
speed += Phaser.Math.between(-this.bulletSpeedVariance, this.bulletSpeedVariance);
}
if (from)
{
if (this.fireFrom.width > 1)
{
this.fireFrom.centerOn(from.x, from.y);
}
else
{
this.fireFrom.x = from.x;
this.fireFrom.y = from.y;
}
}
else if (this.trackedSprite)
{
if (this.fireFrom.width > 1)
{
this.fireFrom.centerOn(this.trackedSprite.world.x + this.trackOffset.x, this.trackedSprite.world.y + this.trackOffset.y);
}
else
{
this.fireFrom.x = this.trackedSprite.world.x + this.trackOffset.x;
this.fireFrom.y = this.trackedSprite.world.y + this.trackOffset.y;
}
if (this.bulletInheritSpriteSpeed)
{
speed += this.trackedSprite.body.speed;
}
}
else if (this.trackedPointer)
{
if (this.fireFrom.width > 1)
{
this.fireFrom.centerOn(this.trackedPointer.world.x + this.trackOffset.x, this.trackedPointer.world.y + this.trackOffset.y);
}
else
{
this.fireFrom.x = this.trackedPointer.world.x + this.trackOffset.x;
this.fireFrom.y = this.trackedPointer.world.y + this.trackOffset.y;
}
}
var fromX = (this.fireFrom.width > 1) ? this.fireFrom.randomX : this.fireFrom.x;
var fromY = (this.fireFrom.height > 1) ? this.fireFrom.randomY : this.fireFrom.y;
var angle = (this.trackRotation) ? this.trackedSprite.angle : this.fireAngle;
// The position (in world space) to fire the bullet towards, if set
if (x !== undefined && y !== undefined)
{
angle = this.game.math.radToDeg(Math.atan2(y - fromY, x - fromX));
}
// Apply +- angle variance
if (this.bulletAngleVariance !== 0)
{
angle += Phaser.Math.between(-this.bulletAngleVariance, this.bulletAngleVariance);
}
var moveX = 0;
var moveY = 0;
// Avoid sin/cos for right-angled shots
if (angle === 0 || angle === 180)
{
moveX = Math.cos(this.game.math.degToRad(angle)) * speed;
}
else if (angle === 90 || angle === 270)
{
moveY = Math.sin(this.game.math.degToRad(angle)) * speed;
}
else
{
moveX = Math.cos(this.game.math.degToRad(angle)) * speed;
moveY = Math.sin(this.game.math.degToRad(angle)) * speed;
}
var bullet = null;
if (this.autoExpandBulletsGroup)
{
bullet = this.bullets.getFirstExists(false, true, fromX, fromY, this.bulletKey, this.bulletFrame);
bullet.data.bulletManager = this;
}
else
{
bullet = this.bullets.getFirstExists(false);
}
if (bullet)
{
bullet.reset(fromX, fromY);
bullet.data.fromX = fromX;
bullet.data.fromY = fromY;
bullet.data.killType = this.bulletKillType;
bullet.data.killDistance = this.bulletKillDistance;
bullet.data.rotateToVelocity = this.bulletRotateToVelocity;
if (this.bulletKillType === Phaser.Weapon.KILL_LIFESPAN)
{
bullet.lifespan = this.bulletLifespan;
}
bullet.angle = angle + this.bulletAngleOffset;
// Frames and Animations
if (this.bulletAnimation !== '')
{
if (bullet.animations.getAnimation(this.bulletAnimation) === null)
{
var anim = this.anims[this.bulletAnimation];
bullet.animations.add(anim.name, anim.frames, anim.frameRate, anim.loop, anim.useNumericIndex);
}
bullet.animations.play(this.bulletAnimation);
}
else
{
if (this.bulletFrameCycle)
{
bullet.frame = this.bulletFrames[this.bulletFrameIndex];
this.bulletFrameIndex++;
if (this.bulletFrameIndex >= this.bulletFrames.length)
{
this.bulletFrameIndex = 0;
}
}
else if (this.bulletFrameRandom)
{
bullet.frame = this.bulletFrames[Math.floor(Math.random() * this.bulletFrames.length)];
}
}
if (bullet.data.bodyDirty)
{
if (this._data.customBody)
{
bullet.body.setSize(this._data.width, this._data.height, this._data.offsetX, this._data.offsetY);
}
bullet.body.collideWorldBounds = this.bulletCollideWorldBounds;
bullet.data.bodyDirty = false;
}
bullet.body.velocity.set(moveX, moveY);
bullet.body.gravity.set(this.bulletGravity.x, this.bulletGravity.y);
this._nextFire = this.game.time.now + this.fireRate;
this.shots++;
this.onFire.dispatch(bullet, this, speed);
if (this.fireLimit > 0 && this.shots === this.fireLimit)
{
this.onFireLimit.dispatch(this, this.fireLimit);
}
}
};
/**
* Fires a bullet **at** the given Pointer. The bullet will be launched from the `Weapon.fireFrom` position,
* or from a Tracked Sprite or Pointer, if you have one set.
*
* @method Phaser.Weapon#fireAtPointer
* @param {Phaser.Pointer} [pointer] - The Pointer to fire the bullet towards.
* @return {boolean} True if a bullet was successfully fired, otherwise false.
*/
Phaser.Weapon.prototype.fireAtPointer = function (pointer) {
if (pointer === undefined) { pointer = this.game.input.activePointer; }
return this.fire(null, pointer.worldX, pointer.worldY);
};
/**
* Fires a bullet **at** the given Sprite. The bullet will be launched from the `Weapon.fireFrom` position,
* or from a Tracked Sprite or Pointer, if you have one set.
*
* @method Phaser.Weapon#fireAtSprite
* @param {Phaser.Sprite} [sprite] - The Sprite to fire the bullet towards.
* @return {boolean} True if a bullet was successfully fired, otherwise false.
*/
Phaser.Weapon.prototype.fireAtSprite = function (sprite) {
return this.fire(null, sprite.world.x, sprite.world.y);
};
/**
* Fires a bullet **at** the given coordinates. The bullet will be launched from the `Weapon.fireFrom` position,
* or from a Tracked Sprite or Pointer, if you have one set.
*
* @method Phaser.Weapon#fireAtXY
* @param {number} [x] - The x coordinate, in world space, to fire the bullet towards.
* @param {number} [y] - The y coordinate, in world space, to fire the bullet towards.
* @return {boolean} True if a bullet was successfully fired, otherwise false.
*/
Phaser.Weapon.prototype.fireAtXY = function (x, y) {
return this.fire(null, x, y);
};
/**
* You can modify the size of the physics Body the Bullets use to be any dimension you need.
* This allows you to make it smaller, or larger, than the parent Sprite.
* You can also control the x and y offset of the Body. This is the position of the
* Body relative to the top-left of the Sprite _texture_.
*
* For example: If you have a Sprite with a texture that is 80x100 in size,
* and you want the physics body to be 32x32 pixels in the middle of the texture, you would do:
*
* `setSize(32, 32, 24, 34)`
*
* Where the first two parameters is the new Body size (32x32 pixels).
* 24 is the horizontal offset of the Body from the top-left of the Sprites texture, and 34
* is the vertical offset.
*
* @method Phaser.Weapon#setBulletBodyOffset
* @param {number} width - The width of the Body.
* @param {number} height - The height of the Body.
* @param {number} [offsetX] - The X offset of the Body from the top-left of the Sprites texture.
* @param {number} [offsetY] - The Y offset of the Body from the top-left of the Sprites texture.
* @return {Phaser.Weapon} The Weapon Plugin.
*/
Phaser.Weapon.prototype.setBulletBodyOffset = function (width, height, offsetX, offsetY) {
if (offsetX === undefined) { offsetX = 0; }
if (offsetY === undefined) { offsetY = 0; }
this._data.customBody = true;
this._data.width = width;
this._data.height = height;
this._data.offsetX = offsetX;
this._data.offsetY = offsetY;
// Update all bullets in the pool
this.bullets.callAll('body.setSize', 'body', width, height, offsetX, offsetY);
this.bullets.setAll('data.bodyDirty', false);
return this;
};
/**
* Sets the texture frames that the bullets can use when being launched.
*
* This is intended for use when you've got numeric based frames, such as those loaded via a Sprite Sheet.
*
* It works by calling `Phaser.ArrayUtils.numberArray` internally, using the min and max values
* provided. Then it sets the frame index to be zero.
*
* You can optionally set the cycle and random booleans, to allow bullets to cycle through the frames
* when they're fired, or pick one at random.
*
* @method Phaser.Weapon#setBulletFrames
* @param {integer} min - The minimum value the frame can be. Usually zero.
* @param {integer} max - The maximum value the frame can be.
* @param {boolean} [cycle=true] - Should the bullet frames cycle as they are fired?
* @param {boolean} [random=false] - Should the bullet frames be picked at random as they are fired?
* @return {Phaser.Weapon} The Weapon Plugin.
*/
Phaser.Weapon.prototype.setBulletFrames = function (min, max, cycle, random) {
if (cycle === undefined) { cycle = true; }
if (random === undefined) { random = false; }
this.bulletFrames = Phaser.ArrayUtils.numberArray(min, max);
this.bulletFrameIndex = 0;
this.bulletFrameCycle = cycle;