forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphaser-094.js
12294 lines (12289 loc) · 517 KB
/
phaser-094.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
/// <reference path="Game.ts" />
/**
* Phaser - Basic
*
* A useful "generic" object on which all GameObjects and Groups are based.
* It has no size, position or graphical data.
*/
var Phaser;
(function (Phaser) {
var Basic = (function () {
/**
* Instantiate the basic object.
*/
function Basic(game) {
/**
* Allows you to give this object a name. Useful for debugging, but not actually used internally.
*/
this.name = '';
this._game = game;
this.ID = -1;
this.exists = true;
this.active = true;
this.visible = true;
this.alive = true;
this.isGroup = false;
this.ignoreDrawDebug = false;
}
Basic.prototype.destroy = /**
* Override this to null out iables or manually call
* <code>destroy()</code> on class members if necessary.
* Don't forget to call <code>super.destroy()</code>!
*/
function () {
};
Basic.prototype.preUpdate = /**
* Pre-update is called right before <code>update()</code> on each object in the game loop.
*/
function () {
};
Basic.prototype.update = /**
* Override this to update your class's position and appearance.
* This is where most of your game rules and behavioral code will go.
*/
function () {
};
Basic.prototype.postUpdate = /**
* Post-update is called right after <code>update()</code> on each object in the game loop.
*/
function () {
};
Basic.prototype.render = function (camera, cameraOffsetX, cameraOffsetY) {
};
Basic.prototype.kill = /**
* Handy for "killing" game objects.
* Default behavior is to flag them as nonexistent AND dead.
* However, if you want the "corpse" to remain in the game,
* like to animate an effect or whatever, you should override this,
* setting only alive to false, and leaving exists true.
*/
function () {
this.alive = false;
this.exists = false;
};
Basic.prototype.revive = /**
* Handy for bringing game objects "back to life". Just sets alive and exists back to true.
* In practice, this is most often called by <code>Object.reset()</code>.
*/
function () {
this.alive = true;
this.exists = true;
};
Basic.prototype.toString = /**
* Convert object to readable string name. Useful for debugging, save games, etc.
*/
function () {
return "";
};
return Basic;
})();
Phaser.Basic = Basic;
})(Phaser || (Phaser = {}));
/// <reference path="Signal.ts" />
/**
* Phaser - SignalBinding
*
* An object that represents a binding between a Signal and a listener function.
* Based on JS Signals by Miller Medeiros. Converted by TypeScript by Richard Davey.
* Released under the MIT license
* http://millermedeiros.github.com/js-signals/
*/
var Phaser;
(function (Phaser) {
var SignalBinding = (function () {
/**
* Object that represents a binding between a Signal and a listener function.
* <br />- <strong>This is an internal constructor and shouldn't be called by regular users.</strong>
* <br />- inspired by Joa Ebert AS3 SignalBinding and Robert Penner's Slot classes.
* @author Miller Medeiros
* @constructor
* @internal
* @name SignalBinding
* @param {Signal} signal Reference to Signal object that listener is currently bound to.
* @param {Function} listener Handler function bound to the signal.
* @param {boolean} isOnce If binding should be executed just once.
* @param {Object} [listenerContext] Context on which listener will be executed (object that should represent the `this` variable inside listener function).
* @param {Number} [priority] The priority level of the event listener. (default = 0).
*/
function SignalBinding(signal, listener, isOnce, listenerContext, priority) {
if (typeof priority === "undefined") { priority = 0; }
/**
* If binding is active and should be executed.
* @type boolean
*/
this.active = true;
/**
* Default parameters passed to listener during `Signal.dispatch` and `SignalBinding.execute`. (curried parameters)
* @type Array|null
*/
this.params = null;
this._listener = listener;
this._isOnce = isOnce;
this.context = listenerContext;
this._signal = signal;
this.priority = priority || 0;
}
SignalBinding.prototype.execute = /**
* Call listener passing arbitrary parameters.
* <p>If binding was added using `Signal.addOnce()` it will be automatically removed from signal dispatch queue, this method is used internally for the signal dispatch.</p>
* @param {Array} [paramsArr] Array of parameters that should be passed to the listener
* @return {*} Value returned by the listener.
*/
function (paramsArr) {
var handlerReturn;
var params;
if(this.active && !!this._listener) {
params = this.params ? this.params.concat(paramsArr) : paramsArr;
handlerReturn = this._listener.apply(this.context, params);
if(this._isOnce) {
this.detach();
}
}
return handlerReturn;
};
SignalBinding.prototype.detach = /**
* Detach binding from signal.
* - alias to: mySignal.remove(myBinding.getListener());
* @return {Function|null} Handler function bound to the signal or `null` if binding was previously detached.
*/
function () {
return this.isBound() ? this._signal.remove(this._listener, this.context) : null;
};
SignalBinding.prototype.isBound = /**
* @return {Boolean} `true` if binding is still bound to the signal and have a listener.
*/
function () {
return (!!this._signal && !!this._listener);
};
SignalBinding.prototype.isOnce = /**
* @return {boolean} If SignalBinding will only be executed once.
*/
function () {
return this._isOnce;
};
SignalBinding.prototype.getListener = /**
* @return {Function} Handler function bound to the signal.
*/
function () {
return this._listener;
};
SignalBinding.prototype.getSignal = /**
* @return {Signal} Signal that listener is currently bound to.
*/
function () {
return this._signal;
};
SignalBinding.prototype._destroy = /**
* Delete instance properties
* @private
*/
function () {
delete this._signal;
delete this._listener;
delete this.context;
};
SignalBinding.prototype.toString = /**
* @return {string} String representation of the object.
*/
function () {
return '[SignalBinding isOnce:' + this._isOnce + ', isBound:' + this.isBound() + ', active:' + this.active + ']';
};
return SignalBinding;
})();
Phaser.SignalBinding = SignalBinding;
})(Phaser || (Phaser = {}));
/// <reference path="SignalBinding.ts" />
/**
* Phaser - Signal
*
* A Signal is used for object communication via a custom broadcaster instead of Events.
* Based on JS Signals by Miller Medeiros. Converted by TypeScript by Richard Davey.
* Released under the MIT license
* http://millermedeiros.github.com/js-signals/
*/
var Phaser;
(function (Phaser) {
var Signal = (function () {
function Signal() {
/**
*
* @property _bindings
* @type Array
* @private
*/
this._bindings = [];
/**
*
* @property _prevParams
* @type Any
* @private
*/
this._prevParams = null;
/**
* If Signal should keep record of previously dispatched parameters and
* automatically execute listener during `add()`/`addOnce()` if Signal was
* already dispatched before.
* @type boolean
*/
this.memorize = false;
/**
* @type boolean
* @private
*/
this._shouldPropagate = true;
/**
* If Signal is active and should broadcast events.
* <p><strong>IMPORTANT:</strong> Setting this property during a dispatch will only affect the next dispatch, if you want to stop the propagation of a signal use `halt()` instead.</p>
* @type boolean
*/
this.active = true;
}
Signal.VERSION = '1.0.0';
Signal.prototype.validateListener = /**
*
* @method validateListener
* @param {Any} listener
* @param {Any} fnName
*/
function (listener, fnName) {
if(typeof listener !== 'function') {
throw new Error('listener is a required param of {fn}() and should be a Function.'.replace('{fn}', fnName));
}
};
Signal.prototype._registerListener = /**
* @param {Function} listener
* @param {boolean} isOnce
* @param {Object} [listenerContext]
* @param {Number} [priority]
* @return {SignalBinding}
* @private
*/
function (listener, isOnce, listenerContext, priority) {
var prevIndex = this._indexOfListener(listener, listenerContext);
var binding;
if(prevIndex !== -1) {
binding = this._bindings[prevIndex];
if(binding.isOnce() !== isOnce) {
throw new Error('You cannot add' + (isOnce ? '' : 'Once') + '() then add' + (!isOnce ? '' : 'Once') + '() the same listener without removing the relationship first.');
}
} else {
binding = new Phaser.SignalBinding(this, listener, isOnce, listenerContext, priority);
this._addBinding(binding);
}
if(this.memorize && this._prevParams) {
binding.execute(this._prevParams);
}
return binding;
};
Signal.prototype._addBinding = /**
*
* @method _addBinding
* @param {SignalBinding} binding
* @private
*/
function (binding) {
//simplified insertion sort
var n = this._bindings.length;
do {
--n;
}while(this._bindings[n] && binding.priority <= this._bindings[n].priority);
this._bindings.splice(n + 1, 0, binding);
};
Signal.prototype._indexOfListener = /**
*
* @method _indexOfListener
* @param {Function} listener
* @return {number}
* @private
*/
function (listener, context) {
var n = this._bindings.length;
var cur;
while(n--) {
cur = this._bindings[n];
if(cur.getListener() === listener && cur.context === context) {
return n;
}
}
return -1;
};
Signal.prototype.has = /**
* Check if listener was attached to Signal.
* @param {Function} listener
* @param {Object} [context]
* @return {boolean} if Signal has the specified listener.
*/
function (listener, context) {
if (typeof context === "undefined") { context = null; }
return this._indexOfListener(listener, context) !== -1;
};
Signal.prototype.add = /**
* Add a listener to the signal.
* @param {Function} listener Signal handler function.
* @param {Object} [listenerContext] Context on which listener will be executed (object that should represent the `this` variable inside listener function).
* @param {Number} [priority] The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added. (default = 0)
* @return {SignalBinding} An Object representing the binding between the Signal and listener.
*/
function (listener, listenerContext, priority) {
if (typeof listenerContext === "undefined") { listenerContext = null; }
if (typeof priority === "undefined") { priority = 0; }
this.validateListener(listener, 'add');
return this._registerListener(listener, false, listenerContext, priority);
};
Signal.prototype.addOnce = /**
* Add listener to the signal that should be removed after first execution (will be executed only once).
* @param {Function} listener Signal handler function.
* @param {Object} [listenerContext] Context on which listener will be executed (object that should represent the `this` variable inside listener function).
* @param {Number} [priority] The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added. (default = 0)
* @return {SignalBinding} An Object representing the binding between the Signal and listener.
*/
function (listener, listenerContext, priority) {
if (typeof listenerContext === "undefined") { listenerContext = null; }
if (typeof priority === "undefined") { priority = 0; }
this.validateListener(listener, 'addOnce');
return this._registerListener(listener, true, listenerContext, priority);
};
Signal.prototype.remove = /**
* Remove a single listener from the dispatch queue.
* @param {Function} listener Handler function that should be removed.
* @param {Object} [context] Execution context (since you can add the same handler multiple times if executing in a different context).
* @return {Function} Listener handler function.
*/
function (listener, context) {
if (typeof context === "undefined") { context = null; }
this.validateListener(listener, 'remove');
var i = this._indexOfListener(listener, context);
if(i !== -1) {
this._bindings[i]._destroy()//no reason to a SignalBinding exist if it isn't attached to a signal
;
this._bindings.splice(i, 1);
}
return listener;
};
Signal.prototype.removeAll = /**
* Remove all listeners from the Signal.
*/
function () {
var n = this._bindings.length;
while(n--) {
this._bindings[n]._destroy();
}
this._bindings.length = 0;
};
Signal.prototype.getNumListeners = /**
* @return {number} Number of listeners attached to the Signal.
*/
function () {
return this._bindings.length;
};
Signal.prototype.halt = /**
* Stop propagation of the event, blocking the dispatch to next listeners on the queue.
* <p><strong>IMPORTANT:</strong> should be called only during signal dispatch, calling it before/after dispatch won't affect signal broadcast.</p>
* @see Signal.prototype.disable
*/
function () {
this._shouldPropagate = false;
};
Signal.prototype.dispatch = /**
* Dispatch/Broadcast Signal to all listeners added to the queue.
* @param {...*} [params] Parameters that should be passed to each handler.
*/
function () {
var paramsArr = [];
for (var _i = 0; _i < (arguments.length - 0); _i++) {
paramsArr[_i] = arguments[_i + 0];
}
if(!this.active) {
return;
}
var n = this._bindings.length;
var bindings;
if(this.memorize) {
this._prevParams = paramsArr;
}
if(!n) {
//should come after memorize
return;
}
bindings = this._bindings.slice(0)//clone array in case add/remove items during dispatch
;
this._shouldPropagate = true//in case `halt` was called before dispatch or during the previous dispatch.
;
//execute all callbacks until end of the list or until a callback returns `false` or stops propagation
//reverse loop since listeners with higher priority will be added at the end of the list
do {
n--;
}while(bindings[n] && this._shouldPropagate && bindings[n].execute(paramsArr) !== false);
};
Signal.prototype.forget = /**
* Forget memorized arguments.
* @see Signal.memorize
*/
function () {
this._prevParams = null;
};
Signal.prototype.dispose = /**
* Remove all bindings from signal and destroy any reference to external objects (destroy Signal object).
* <p><strong>IMPORTANT:</strong> calling any method on the signal instance after calling dispose will throw errors.</p>
*/
function () {
this.removeAll();
delete this._bindings;
delete this._prevParams;
};
Signal.prototype.toString = /**
* @return {string} String representation of the object.
*/
function () {
return '[Signal active:' + this.active + ' numListeners:' + this.getNumListeners() + ']';
};
return Signal;
})();
Phaser.Signal = Signal;
})(Phaser || (Phaser = {}));
var __extends = this.__extends || function (d, b) {
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
/// <reference path="../Game.ts" />
/// <reference path="../Basic.ts" />
/// <reference path="../Signal.ts" />
/**
* Phaser - GameObject
*
* This is the base GameObject on which all other game objects are derived. It contains all the logic required for position,
* motion, size, collision and input.
*/
var Phaser;
(function (Phaser) {
var GameObject = (function (_super) {
__extends(GameObject, _super);
function GameObject(game, x, y, width, height) {
if (typeof x === "undefined") { x = 0; }
if (typeof y === "undefined") { y = 0; }
if (typeof width === "undefined") { width = 16; }
if (typeof height === "undefined") { height = 16; }
_super.call(this, game);
this._angle = 0;
this.outOfBoundsAction = 0;
this.z = 0;
// This value is added to the angle of the GameObject.
// For example if you had a sprite drawn facing straight up then you could set
// rotationOffset to 90 and it would correspond correctly with Phasers rotation system
this.rotationOffset = 0;
this.renderRotation = true;
this.moves = true;
// Input
this.inputEnabled = false;
this._inputOver = false;
this.bounds = new Phaser.Rectangle(x, y, width, height);
this.exists = true;
this.active = true;
this.visible = true;
this.alive = true;
this.isGroup = false;
this.alpha = 1;
this.scale = new Phaser.MicroPoint(1, 1);
this.last = new Phaser.MicroPoint(x, y);
this.origin = new Phaser.MicroPoint(this.bounds.halfWidth, this.bounds.halfHeight);
this.align = GameObject.ALIGN_TOP_LEFT;
this.mass = 1;
this.elasticity = 0;
this.health = 1;
this.immovable = false;
this.moves = true;
this.worldBounds = null;
this.touching = Phaser.Collision.NONE;
this.wasTouching = Phaser.Collision.NONE;
this.allowCollisions = Phaser.Collision.ANY;
this.velocity = new Phaser.MicroPoint();
this.acceleration = new Phaser.MicroPoint();
this.drag = new Phaser.MicroPoint();
this.maxVelocity = new Phaser.MicroPoint(10000, 10000);
this.angle = 0;
this.angularVelocity = 0;
this.angularAcceleration = 0;
this.angularDrag = 0;
this.maxAngular = 10000;
this.cameraBlacklist = [];
this.scrollFactor = new Phaser.MicroPoint(1, 1);
}
GameObject.ALIGN_TOP_LEFT = 0;
GameObject.ALIGN_TOP_CENTER = 1;
GameObject.ALIGN_TOP_RIGHT = 2;
GameObject.ALIGN_CENTER_LEFT = 3;
GameObject.ALIGN_CENTER = 4;
GameObject.ALIGN_CENTER_RIGHT = 5;
GameObject.ALIGN_BOTTOM_LEFT = 6;
GameObject.ALIGN_BOTTOM_CENTER = 7;
GameObject.ALIGN_BOTTOM_RIGHT = 8;
GameObject.OUT_OF_BOUNDS_STOP = 0;
GameObject.OUT_OF_BOUNDS_KILL = 1;
GameObject.prototype.preUpdate = function () {
// flicker time
this.last.x = this.bounds.x;
this.last.y = this.bounds.y;
};
GameObject.prototype.update = function () {
};
GameObject.prototype.postUpdate = function () {
if(this.moves) {
this.updateMotion();
}
if(this.worldBounds != null) {
if(this.outOfBoundsAction == GameObject.OUT_OF_BOUNDS_KILL) {
if(this.x < this.worldBounds.x || this.x > this.worldBounds.right || this.y < this.worldBounds.y || this.y > this.worldBounds.bottom) {
this.kill();
}
} else {
if(this.x < this.worldBounds.x) {
this.x = this.worldBounds.x;
} else if(this.x > this.worldBounds.right) {
this.x = this.worldBounds.right;
}
if(this.y < this.worldBounds.y) {
this.y = this.worldBounds.y;
} else if(this.y > this.worldBounds.bottom) {
this.y = this.worldBounds.bottom;
}
}
}
if(this.inputEnabled) {
this.updateInput();
}
this.wasTouching = this.touching;
this.touching = Phaser.Collision.NONE;
};
GameObject.prototype.updateInput = function () {
};
GameObject.prototype.updateMotion = function () {
var delta;
var velocityDelta;
velocityDelta = (this._game.motion.computeVelocity(this.angularVelocity, this.angularAcceleration, this.angularDrag, this.maxAngular) - this.angularVelocity) / 2;
this.angularVelocity += velocityDelta;
this._angle += this.angularVelocity * this._game.time.elapsed;
this.angularVelocity += velocityDelta;
velocityDelta = (this._game.motion.computeVelocity(this.velocity.x, this.acceleration.x, this.drag.x, this.maxVelocity.x) - this.velocity.x) / 2;
this.velocity.x += velocityDelta;
delta = this.velocity.x * this._game.time.elapsed;
this.velocity.x += velocityDelta;
this.bounds.x += delta;
velocityDelta = (this._game.motion.computeVelocity(this.velocity.y, this.acceleration.y, this.drag.y, this.maxVelocity.y) - this.velocity.y) / 2;
this.velocity.y += velocityDelta;
delta = this.velocity.y * this._game.time.elapsed;
this.velocity.y += velocityDelta;
this.bounds.y += delta;
};
GameObject.prototype.overlaps = /**
* Checks to see if some <code>GameObject</code> overlaps this <code>GameObject</code> or <code>Group</code>.
* If the group has a LOT of things in it, it might be faster to use <code>Collision.overlaps()</code>.
* WARNING: Currently tilemaps do NOT support screen space overlap checks!
*
* @param ObjectOrGroup The object or group being tested.
* @param InScreenSpace Whether to take scroll factors numbero account when checking for overlap. Default is false, or "only compare in world space."
* @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
*
* @return Whether or not the two objects overlap.
*/
function (ObjectOrGroup, InScreenSpace, Camera) {
if (typeof InScreenSpace === "undefined") { InScreenSpace = false; }
if (typeof Camera === "undefined") { Camera = null; }
if(ObjectOrGroup.isGroup) {
var results = false;
var i = 0;
var members = ObjectOrGroup.members;
while(i < length) {
if(this.overlaps(members[i++], InScreenSpace, Camera)) {
results = true;
}
}
return results;
}
if(!InScreenSpace) {
return (ObjectOrGroup.x + ObjectOrGroup.width > this.x) && (ObjectOrGroup.x < this.x + this.width) && (ObjectOrGroup.y + ObjectOrGroup.height > this.y) && (ObjectOrGroup.y < this.y + this.height);
}
if(Camera == null) {
Camera = this._game.camera;
}
var objectScreenPos = ObjectOrGroup.getScreenXY(null, Camera);
this.getScreenXY(this._point, Camera);
return (objectScreenPos.x + ObjectOrGroup.width > this._point.x) && (objectScreenPos.x < this._point.x + this.width) && (objectScreenPos.y + ObjectOrGroup.height > this._point.y) && (objectScreenPos.y < this._point.y + this.height);
};
GameObject.prototype.overlapsAt = /**
* Checks to see if this <code>GameObject</code> were located at the given position, would it overlap the <code>GameObject</code> or <code>Group</code>?
* This is distinct from overlapsPoint(), which just checks that point, rather than taking the object's size numbero account.
* WARNING: Currently tilemaps do NOT support screen space overlap checks!
*
* @param X The X position you want to check. Pretends this object (the caller, not the parameter) is located here.
* @param Y The Y position you want to check. Pretends this object (the caller, not the parameter) is located here.
* @param ObjectOrGroup The object or group being tested.
* @param InScreenSpace Whether to take scroll factors numbero account when checking for overlap. Default is false, or "only compare in world space."
* @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
*
* @return Whether or not the two objects overlap.
*/
function (X, Y, ObjectOrGroup, InScreenSpace, Camera) {
if (typeof InScreenSpace === "undefined") { InScreenSpace = false; }
if (typeof Camera === "undefined") { Camera = null; }
if(ObjectOrGroup.isGroup) {
var results = false;
var basic;
var i = 0;
var members = ObjectOrGroup.members;
while(i < length) {
if(this.overlapsAt(X, Y, members[i++], InScreenSpace, Camera)) {
results = true;
}
}
return results;
}
if(!InScreenSpace) {
return (ObjectOrGroup.x + ObjectOrGroup.width > X) && (ObjectOrGroup.x < X + this.width) && (ObjectOrGroup.y + ObjectOrGroup.height > Y) && (ObjectOrGroup.y < Y + this.height);
}
if(Camera == null) {
Camera = this._game.camera;
}
var objectScreenPos = ObjectOrGroup.getScreenXY(null, Camera);
this._point.x = X - Camera.scroll.x * this.scrollFactor.x//copied from getScreenXY()
;
this._point.y = Y - Camera.scroll.y * this.scrollFactor.y;
this._point.x += (this._point.x > 0) ? 0.0000001 : -0.0000001;
this._point.y += (this._point.y > 0) ? 0.0000001 : -0.0000001;
return (objectScreenPos.x + ObjectOrGroup.width > this._point.x) && (objectScreenPos.x < this._point.x + this.width) && (objectScreenPos.y + ObjectOrGroup.height > this._point.y) && (objectScreenPos.y < this._point.y + this.height);
};
GameObject.prototype.overlapsPoint = /**
* Checks to see if a point in 2D world space overlaps this <code>GameObject</code>.
*
* @param Point The point in world space you want to check.
* @param InScreenSpace Whether to take scroll factors numbero account when checking for overlap.
* @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
*
* @return Whether or not the point overlaps this object.
*/
function (point, InScreenSpace, Camera) {
if (typeof InScreenSpace === "undefined") { InScreenSpace = false; }
if (typeof Camera === "undefined") { Camera = null; }
if(!InScreenSpace) {
return (point.x > this.x) && (point.x < this.x + this.width) && (point.y > this.y) && (point.y < this.y + this.height);
}
if(Camera == null) {
Camera = this._game.camera;
}
var X = point.x - Camera.scroll.x;
var Y = point.y - Camera.scroll.y;
this.getScreenXY(this._point, Camera);
return (X > this._point.x) && (X < this._point.x + this.width) && (Y > this._point.y) && (Y < this._point.y + this.height);
};
GameObject.prototype.onScreen = /**
* Check and see if this object is currently on screen.
*
* @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
*
* @return Whether the object is on screen or not.
*/
function (Camera) {
if (typeof Camera === "undefined") { Camera = null; }
if(Camera == null) {
Camera = this._game.camera;
}
this.getScreenXY(this._point, Camera);
return (this._point.x + this.width > 0) && (this._point.x < Camera.width) && (this._point.y + this.height > 0) && (this._point.y < Camera.height);
};
GameObject.prototype.getScreenXY = /**
* Call this to figure out the on-screen position of the object.
*
* @param Camera Specify which game camera you want. If null getScreenXY() will just grab the first global camera.
* @param Point Takes a <code>MicroPoint</code> object and assigns the post-scrolled X and Y values of this object to it.
*
* @return The <code>MicroPoint</code> you passed in, or a new <code>Point</code> if you didn't pass one, containing the screen X and Y position of this object.
*/
function (point, Camera) {
if (typeof point === "undefined") { point = null; }
if (typeof Camera === "undefined") { Camera = null; }
if(point == null) {
point = new Phaser.MicroPoint();
}
if(Camera == null) {
Camera = this._game.camera;
}
point.x = this.x - Camera.scroll.x * this.scrollFactor.x;
point.y = this.y - Camera.scroll.y * this.scrollFactor.y;
point.x += (point.x > 0) ? 0.0000001 : -0.0000001;
point.y += (point.y > 0) ? 0.0000001 : -0.0000001;
return point;
};
Object.defineProperty(GameObject.prototype, "solid", {
get: /**
* Whether the object collides or not. For more control over what directions
* the object will collide from, use collision constants (like LEFT, FLOOR, etc)
* to set the value of allowCollisions directly.
*/
function () {
return (this.allowCollisions & Phaser.Collision.ANY) > Phaser.Collision.NONE;
},
set: /**
* @private
*/
function (Solid) {
if(Solid) {
this.allowCollisions = Phaser.Collision.ANY;
} else {
this.allowCollisions = Phaser.Collision.NONE;
}
},
enumerable: true,
configurable: true
});
GameObject.prototype.getMidpoint = /**
* Retrieve the midpoint of this object in world coordinates.
*
* @Point Allows you to pass in an existing <code>Point</code> object if you're so inclined. Otherwise a new one is created.
*
* @return A <code>Point</code> object containing the midpoint of this object in world coordinates.
*/
function (point) {
if (typeof point === "undefined") { point = null; }
if(point == null) {
point = new Phaser.MicroPoint();
}
point.copyFrom(this.bounds.center);
return point;
};
GameObject.prototype.reset = /**
* Handy for reviving game objects.
* Resets their existence flags and position.
*
* @param X The new X position of this object.
* @param Y The new Y position of this object.
*/
function (X, Y) {
this.revive();
this.touching = Phaser.Collision.NONE;
this.wasTouching = Phaser.Collision.NONE;
this.x = X;
this.y = Y;
this.last.x = X;
this.last.y = Y;
this.velocity.x = 0;
this.velocity.y = 0;
};
GameObject.prototype.isTouching = /**
* Handy for checking if this object is touching a particular surface.
* For slightly better performance you can just & the value directly numbero <code>touching</code>.
* However, this method is good for readability and accessibility.
*
* @param Direction Any of the collision flags (e.g. LEFT, FLOOR, etc).
*
* @return Whether the object is touching an object in (any of) the specified direction(s) this frame.
*/
function (Direction) {
return (this.touching & Direction) > Phaser.Collision.NONE;
};
GameObject.prototype.justTouched = /**
* Handy for checking if this object is just landed on a particular surface.
*
* @param Direction Any of the collision flags (e.g. LEFT, FLOOR, etc).
*
* @return Whether the object just landed on (any of) the specified surface(s) this frame.
*/
function (Direction) {
return ((this.touching & Direction) > Phaser.Collision.NONE) && ((this.wasTouching & Direction) <= Phaser.Collision.NONE);
};
GameObject.prototype.hurt = /**
* Reduces the "health" variable of this sprite by the amount specified in Damage.
* Calls kill() if health drops to or below zero.
*
* @param Damage How much health to take away (use a negative number to give a health bonus).
*/
function (Damage) {
this.health = this.health - Damage;
if(this.health <= 0) {
this.kill();
}
};
GameObject.prototype.setBounds = /**
* Set the world bounds that this GameObject can exist within. By default a GameObject can exist anywhere
* in the world. But by setting the bounds (which are given in world dimensions, not screen dimensions)
* it can be stopped from leaving the world, or a section of it.
*/
function (x, y, width, height) {
this.worldBounds = new Phaser.Quad(x, y, width, height);
};
GameObject.prototype.hideFromCamera = /**
* If you do not wish this object to be visible to a specific camera, pass the camera here.
*/
function (camera) {
if(this.cameraBlacklist.indexOf(camera.ID) == -1) {
this.cameraBlacklist.push(camera.ID);
}
};
GameObject.prototype.showToCamera = function (camera) {
if(this.cameraBlacklist.indexOf(camera.ID) !== -1) {
this.cameraBlacklist.slice(this.cameraBlacklist.indexOf(camera.ID), 1);
}
};
GameObject.prototype.clearCameraList = function () {
this.cameraBlacklist.length = 0;
};
GameObject.prototype.destroy = function () {
};
Object.defineProperty(GameObject.prototype, "x", {
get: function () {
return this.bounds.x;
},
set: function (value) {
this.bounds.x = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(GameObject.prototype, "y", {
get: function () {
return this.bounds.y;
},
set: function (value) {
this.bounds.y = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(GameObject.prototype, "rotation", {
get: function () {
return this._angle;
},
set: function (value) {
this._angle = this._game.math.wrap(value, 360, 0);
},
enumerable: true,
configurable: true
});
Object.defineProperty(GameObject.prototype, "angle", {
get: function () {
return this._angle;
},
set: function (value) {
this._angle = this._game.math.wrap(value, 360, 0);
},
enumerable: true,
configurable: true
});
Object.defineProperty(GameObject.prototype, "width", {
get: function () {
return this.bounds.width;
},
set: function (value) {
this.bounds.width = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(GameObject.prototype, "height", {
get: function () {
return this.bounds.height;
},
set: function (value) {
this.bounds.height = value;
},
enumerable: true,
configurable: true
});
return GameObject;
})(Phaser.Basic);
Phaser.GameObject = GameObject;
})(Phaser || (Phaser = {}));
/// <reference path="../gameobjects/Sprite.ts" />
/// <reference path="../Game.ts" />
/**
* Phaser - Camera
*
* A Camera is your view into the game world. It has a position, size, scale and rotation and renders only those objects
* within its field of view. The game automatically creates a single Stage sized camera on boot, but it can be changed and
* additional cameras created via the CameraManager.
*/
var Phaser;
(function (Phaser) {
var Camera = (function () {
/**
* Instantiates a new camera at the specified location, with the specified size and zoom level.
*
* @param X X location of the camera's display in pixels. Uses native, 1:1 resolution, ignores zoom.
* @param Y Y location of the camera's display in pixels. Uses native, 1:1 resolution, ignores zoom.
* @param Width The width of the camera display in pixels.
* @param Height The height of the camera display in pixels.
* @param Zoom The initial zoom level of the camera. A zoom level of 2 will make all pixels display at 2x resolution.
*/
function Camera(game, id, x, y, width, height) {
this._clip = false;
this._rotation = 0;
this._target = null;
this._sx = 0;
this._sy = 0;
this._fxFlashComplete = null;
this._fxFlashDuration = 0;
this._fxFlashAlpha = 0;
this._fxFadeComplete = null;
this._fxFadeDuration = 0;
this._fxFadeAlpha = 0;
this._fxShakeIntensity = 0;
this._fxShakeDuration = 0;
this._fxShakeComplete = null;
this._fxShakeOffset = new Phaser.MicroPoint(0, 0);
this._fxShakeDirection = 0;
this._fxShakePrevX = 0;
this._fxShakePrevY = 0;
this.scale = new Phaser.MicroPoint(1, 1);
this.scroll = new Phaser.MicroPoint(0, 0);
this.bounds = null;
this.deadzone = null;
// Camera Border
this.showBorder = false;
this.borderColor = 'rgb(255,255,255)';
// Camera Background Color
this.opaque = true;
this._bgColor = 'rgb(0,0,0)';
this._bgTextureRepeat = 'repeat';
// Camera Shadow
this.showShadow = false;
this.shadowColor = 'rgb(0,0,0)';
this.shadowBlur = 10;
this.shadowOffset = new Phaser.MicroPoint(4, 4);
this.visible = true;
this.alpha = 1;
// The x/y position of the current input event in world coordinates
this.inputX = 0;
this.inputY = 0;
this._game = game;
this.ID = id;
this._stageX = x;
this._stageY = y;
// The view into the world canvas we wish to render
this.worldView = new Phaser.Rectangle(0, 0, width, height);
this.checkClip();
}
Camera.STYLE_LOCKON = 0;
Camera.STYLE_PLATFORMER = 1;
Camera.STYLE_TOPDOWN = 2;
Camera.STYLE_TOPDOWN_TIGHT = 3;
Camera.SHAKE_BOTH_AXES = 0;
Camera.SHAKE_HORIZONTAL_ONLY = 1;
Camera.SHAKE_VERTICAL_ONLY = 2;
Camera.prototype.flash = /**
* The camera is filled with this color and returns to normal at the given duration.
*
* @param Color The color you want to use in 0xRRGGBB format, i.e. 0xffffff for white.
* @param Duration How long it takes for the flash to fade.
* @param OnComplete An optional function you want to run when the flash finishes. Set to null for no callback.
* @param Force Force an already running flash effect to reset.
*/
function (color, duration, onComplete, force) {
if (typeof color === "undefined") { color = 0xffffff; }
if (typeof duration === "undefined") { duration = 1; }
if (typeof onComplete === "undefined") { onComplete = null; }
if (typeof force === "undefined") { force = false; }
if(force === false && this._fxFlashAlpha > 0) {
// You can't flash again unless you force it
return;
}
if(duration <= 0) {
duration = 1;
}
var red = color >> 16 & 0xFF;
var green = color >> 8 & 0xFF;
var blue = color & 0xFF;
this._fxFlashColor = 'rgba(' + red + ',' + green + ',' + blue + ',';
this._fxFlashDuration = duration;
this._fxFlashAlpha = 1;
this._fxFlashComplete = onComplete;
};
Camera.prototype.fade = /**
* The camera is gradually filled with this color.