-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
BaseCamera.js
2003 lines (1742 loc) · 59.5 KB
/
BaseCamera.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 <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../../utils/Class');
var Components = require('../../gameobjects/components');
var DegToRad = require('../../math/DegToRad');
var EventEmitter = require('eventemitter3');
var Events = require('./events');
var Rectangle = require('../../geom/rectangle/Rectangle');
var TransformMatrix = require('../../gameobjects/components/TransformMatrix');
var ValueToColor = require('../../display/color/ValueToColor');
var Vector2 = require('../../math/Vector2');
/**
* @classdesc
* A Base Camera class.
*
* The Camera is the way in which all games are rendered in Phaser. They provide a view into your game world,
* and can be positioned, rotated, zoomed and scrolled accordingly.
*
* A Camera consists of two elements: The viewport and the scroll values.
*
* The viewport is the physical position and size of the Camera within your game. Cameras, by default, are
* created the same size as your game, but their position and size can be set to anything. This means if you
* wanted to create a camera that was 320x200 in size, positioned in the bottom-right corner of your game,
* you'd adjust the viewport to do that (using methods like `setViewport` and `setSize`).
*
* If you wish to change where the Camera is looking in your game, then you scroll it. You can do this
* via the properties `scrollX` and `scrollY` or the method `setScroll`. Scrolling has no impact on the
* viewport, and changing the viewport has no impact on the scrolling.
*
* By default a Camera will render all Game Objects it can see. You can change this using the `ignore` method,
* allowing you to filter Game Objects out on a per-Camera basis.
*
* The Base Camera is extended by the Camera class, which adds in special effects including Fade,
* Flash and Camera Shake, as well as the ability to follow Game Objects.
*
* The Base Camera was introduced in Phaser 3.12. It was split off from the Camera class, to allow
* you to isolate special effects as needed. Therefore the 'since' values for properties of this class relate
* to when they were added to the Camera class.
*
* @class BaseCamera
* @memberof Phaser.Cameras.Scene2D
* @constructor
* @since 3.12.0
*
* @extends Phaser.Events.EventEmitter
* @extends Phaser.GameObjects.Components.Alpha
* @extends Phaser.GameObjects.Components.Visible
*
* @param {number} x - The x position of the Camera, relative to the top-left of the game canvas.
* @param {number} y - The y position of the Camera, relative to the top-left of the game canvas.
* @param {number} width - The width of the Camera, in pixels.
* @param {number} height - The height of the Camera, in pixels.
*/
var BaseCamera = new Class({
Extends: EventEmitter,
Mixins: [
Components.AlphaSingle,
Components.Visible
],
initialize:
function BaseCamera (x, y, width, height)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = 0; }
if (height === undefined) { height = 0; }
EventEmitter.call(this);
/**
* A reference to the Scene this camera belongs to.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#scene
* @type {Phaser.Scene}
* @since 3.0.0
*/
this.scene;
/**
* A reference to the Game Scene Manager.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#sceneManager
* @type {Phaser.Scenes.SceneManager}
* @since 3.12.0
*/
this.sceneManager;
/**
* A reference to the Game Scale Manager.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#scaleManager
* @type {Phaser.Scale.ScaleManager}
* @since 3.16.0
*/
this.scaleManager;
/**
* A reference to the Scene's Camera Manager to which this Camera belongs.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#cameraManager
* @type {Phaser.Cameras.Scene2D.CameraManager}
* @since 3.17.0
*/
this.cameraManager;
/**
* The Camera ID. Assigned by the Camera Manager and used to handle camera exclusion.
* This value is a bitmask.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#id
* @type {number}
* @readonly
* @since 3.11.0
*/
this.id = 0;
/**
* The name of the Camera. This is left empty for your own use.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#name
* @type {string}
* @default ''
* @since 3.0.0
*/
this.name = '';
/**
* Should this camera round its pixel values to integers?
*
* @name Phaser.Cameras.Scene2D.BaseCamera#roundPixels
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.roundPixels = false;
/**
* Is this Camera visible or not?
*
* A visible camera will render and perform input tests.
* An invisible camera will not render anything and will skip input tests.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#visible
* @type {boolean}
* @default true
* @since 3.10.0
*/
/**
* Is this Camera using a bounds to restrict scrolling movement?
*
* Set this property along with the bounds via `Camera.setBounds`.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#useBounds
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.useBounds = false;
/**
* The World View is a Rectangle that defines the area of the 'world' the Camera is currently looking at.
* This factors in the Camera viewport size, zoom and scroll position and is updated in the Camera preRender step.
* If you have enabled Camera bounds the worldview will be clamped to those bounds accordingly.
* You can use it for culling or intersection checks.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#worldView
* @type {Phaser.Geom.Rectangle}
* @readonly
* @since 3.11.0
*/
this.worldView = new Rectangle();
/**
* Is this Camera dirty?
*
* A dirty Camera has had either its viewport size, bounds, scroll, rotation or zoom levels changed since the last frame.
*
* This flag is cleared during the `postRenderCamera` method of the renderer.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#dirty
* @type {boolean}
* @default true
* @since 3.11.0
*/
this.dirty = true;
/**
* The x position of the Camera viewport, relative to the top-left of the game canvas.
* The viewport is the area into which the camera renders.
* To adjust the position the camera is looking at in the game world, see the `scrollX` value.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#_x
* @type {number}
* @private
* @since 3.0.0
*/
this._x = x;
/**
* The y position of the Camera, relative to the top-left of the game canvas.
* The viewport is the area into which the camera renders.
* To adjust the position the camera is looking at in the game world, see the `scrollY` value.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#_y
* @type {number}
* @private
* @since 3.0.0
*/
this._y = y;
/**
* The width of the Camera viewport, in pixels.
*
* The viewport is the area into which the Camera renders. Setting the viewport does
* not restrict where the Camera can scroll to.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#_width
* @type {number}
* @private
* @since 3.11.0
*/
this._width = width;
/**
* The height of the Camera viewport, in pixels.
*
* The viewport is the area into which the Camera renders. Setting the viewport does
* not restrict where the Camera can scroll to.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#_height
* @type {number}
* @private
* @since 3.11.0
*/
this._height = height;
/**
* The bounds the camera is restrained to during scrolling.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#_bounds
* @type {Phaser.Geom.Rectangle}
* @private
* @since 3.0.0
*/
this._bounds = new Rectangle();
/**
* The horizontal scroll position of this Camera.
*
* Change this value to cause the Camera to scroll around your Scene.
*
* Alternatively, setting the Camera to follow a Game Object, via the `startFollow` method,
* will automatically adjust the Camera scroll values accordingly.
*
* You can set the bounds within which the Camera can scroll via the `setBounds` method.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#_scrollX
* @type {number}
* @private
* @default 0
* @since 3.11.0
*/
this._scrollX = 0;
/**
* The vertical scroll position of this Camera.
*
* Change this value to cause the Camera to scroll around your Scene.
*
* Alternatively, setting the Camera to follow a Game Object, via the `startFollow` method,
* will automatically adjust the Camera scroll values accordingly.
*
* You can set the bounds within which the Camera can scroll via the `setBounds` method.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#_scrollY
* @type {number}
* @private
* @default 0
* @since 3.11.0
*/
this._scrollY = 0;
/**
* The Camera horizontal zoom value. Change this value to zoom in, or out of, a Scene.
*
* A value of 0.5 would zoom the Camera out, so you can now see twice as much
* of the Scene as before. A value of 2 would zoom the Camera in, so every pixel
* now takes up 2 pixels when rendered.
*
* Set to 1 to return to the default zoom level.
*
* Be careful to never set this value to zero.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#_zoomX
* @type {number}
* @private
* @default 1
* @since 3.50.0
*/
this._zoomX = 1;
/**
* The Camera vertical zoom value. Change this value to zoom in, or out of, a Scene.
*
* A value of 0.5 would zoom the Camera out, so you can now see twice as much
* of the Scene as before. A value of 2 would zoom the Camera in, so every pixel
* now takes up 2 pixels when rendered.
*
* Set to 1 to return to the default zoom level.
*
* Be careful to never set this value to zero.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#_zoomY
* @type {number}
* @private
* @default 1
* @since 3.50.0
*/
this._zoomY = 1;
/**
* The rotation of the Camera in radians.
*
* Camera rotation always takes place based on the Camera viewport. By default, rotation happens
* in the center of the viewport. You can adjust this with the `originX` and `originY` properties.
*
* Rotation influences the rendering of _all_ Game Objects visible by this Camera. However, it does not
* rotate the Camera viewport itself, which always remains an axis-aligned rectangle.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#_rotation
* @type {number}
* @private
* @default 0
* @since 3.11.0
*/
this._rotation = 0;
/**
* A local transform matrix used for internal calculations.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#matrix
* @type {Phaser.GameObjects.Components.TransformMatrix}
* @private
* @since 3.0.0
*/
this.matrix = new TransformMatrix();
/**
* Does this Camera have a transparent background?
*
* @name Phaser.Cameras.Scene2D.BaseCamera#transparent
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.transparent = true;
/**
* The background color of this Camera. Only used if `transparent` is `false`.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#backgroundColor
* @type {Phaser.Display.Color}
* @since 3.0.0
*/
this.backgroundColor = ValueToColor('rgba(0,0,0,0)');
/**
* The Camera alpha value. Setting this property impacts every single object that this Camera
* renders. You can either set the property directly, i.e. via a Tween, to fade a Camera in or out,
* or via the chainable `setAlpha` method instead.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#alpha
* @type {number}
* @default 1
* @since 3.11.0
*/
/**
* Should the camera cull Game Objects before checking them for input hit tests?
* In some special cases it may be beneficial to disable this.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#disableCull
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.disableCull = false;
/**
* A temporary array of culled objects.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#culledObjects
* @type {Phaser.GameObjects.GameObject[]}
* @default []
* @private
* @since 3.0.0
*/
this.culledObjects = [];
/**
* The mid-point of the Camera in 'world' coordinates.
*
* Use it to obtain exactly where in the world the center of the camera is currently looking.
*
* This value is updated in the preRender method, after the scroll values and follower
* have been processed.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#midPoint
* @type {Phaser.Math.Vector2}
* @readonly
* @since 3.11.0
*/
this.midPoint = new Vector2(width / 2, height / 2);
/**
* The horizontal origin of rotation for this Camera.
*
* By default the camera rotates around the center of the viewport.
*
* Changing the origin allows you to adjust the point in the viewport from which rotation happens.
* A value of 0 would rotate from the top-left of the viewport. A value of 1 from the bottom right.
*
* See `setOrigin` to set both origins in a single, chainable call.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#originX
* @type {number}
* @default 0.5
* @since 3.11.0
*/
this.originX = 0.5;
/**
* The vertical origin of rotation for this Camera.
*
* By default the camera rotates around the center of the viewport.
*
* Changing the origin allows you to adjust the point in the viewport from which rotation happens.
* A value of 0 would rotate from the top-left of the viewport. A value of 1 from the bottom right.
*
* See `setOrigin` to set both origins in a single, chainable call.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#originY
* @type {number}
* @default 0.5
* @since 3.11.0
*/
this.originY = 0.5;
/**
* Does this Camera have a custom viewport?
*
* @name Phaser.Cameras.Scene2D.BaseCamera#_customViewport
* @type {boolean}
* @private
* @default false
* @since 3.12.0
*/
this._customViewport = false;
/**
* The Mask this Camera is using during render.
* Set the mask using the `setMask` method. Remove the mask using the `clearMask` method.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#mask
* @type {?(Phaser.Display.Masks.BitmapMask|Phaser.Display.Masks.GeometryMask)}
* @since 3.17.0
*/
this.mask = null;
/**
* The Camera that this Camera uses for translation during masking.
*
* If the mask is fixed in position this will be a reference to
* the CameraManager.default instance. Otherwise, it'll be a reference
* to itself.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#_maskCamera
* @type {?Phaser.Cameras.Scene2D.BaseCamera}
* @private
* @since 3.17.0
*/
this._maskCamera = null;
/**
* This array is populated with all of the Game Objects that this Camera has rendered
* in the previous (or current, depending on when you inspect it) frame.
*
* It is cleared at the start of `Camera.preUpdate`, or if the Camera is destroyed.
*
* You should not modify this array as it is used internally by the input system,
* however you can read it as required. Note that Game Objects may appear in this
* list multiple times if they belong to multiple non-exclusive Containers.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#renderList
* @type {Phaser.GameObjects.GameObject[]}
* @since 3.52.0
*/
this.renderList = [];
/**
* Is this Camera a Scene Camera? (which is the default), or a Camera
* belonging to a Texture?
*
* @name Phaser.Cameras.Scene2D.BaseCamera#isSceneCamera
* @type {boolean}
* @default true
* @since 3.60.0
*/
this.isSceneCamera = true;
},
/**
* Adds the given Game Object to this cameras render list.
*
* This is invoked during the rendering stage. Only objects that are actually rendered
* will appear in the render list.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#addToRenderList
* @since 3.52.0
*
* @param {Phaser.GameObjects.GameObject} child - The Game Object to add to the render list.
*/
addToRenderList: function (child)
{
this.renderList.push(child);
},
/**
* Set the Alpha level of this Camera. The alpha controls the opacity of the Camera as it renders.
* Alpha values are provided as a float between 0, fully transparent, and 1, fully opaque.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#setAlpha
* @since 3.11.0
*
* @param {number} [value=1] - The Camera alpha value.
*
* @return {this} This Camera instance.
*/
/**
* Sets the rotation origin of this Camera.
*
* The values are given in the range 0 to 1 and are only used when calculating Camera rotation.
*
* By default the camera rotates around the center of the viewport.
*
* Changing the origin allows you to adjust the point in the viewport from which rotation happens.
* A value of 0 would rotate from the top-left of the viewport. A value of 1 from the bottom right.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#setOrigin
* @since 3.11.0
*
* @param {number} [x=0.5] - The horizontal origin value.
* @param {number} [y=x] - The vertical origin value. If not defined it will be set to the value of `x`.
*
* @return {this} This Camera instance.
*/
setOrigin: function (x, y)
{
if (x === undefined) { x = 0.5; }
if (y === undefined) { y = x; }
this.originX = x;
this.originY = y;
return this;
},
/**
* Calculates what the Camera.scrollX and scrollY values would need to be in order to move
* the Camera so it is centered on the given x and y coordinates, without actually moving
* the Camera there. The results are clamped based on the Camera bounds, if set.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#getScroll
* @since 3.11.0
*
* @param {number} x - The horizontal coordinate to center on.
* @param {number} y - The vertical coordinate to center on.
* @param {Phaser.Math.Vector2} [out] - A Vector2 to store the values in. If not given a new Vector2 is created.
*
* @return {Phaser.Math.Vector2} The scroll coordinates stored in the `x` and `y` properties.
*/
getScroll: function (x, y, out)
{
if (out === undefined) { out = new Vector2(); }
var originX = this.width * 0.5;
var originY = this.height * 0.5;
out.x = x - originX;
out.y = y - originY;
if (this.useBounds)
{
out.x = this.clampX(out.x);
out.y = this.clampY(out.y);
}
return out;
},
/**
* Moves the Camera horizontally so that it is centered on the given x coordinate, bounds allowing.
* Calling this does not change the scrollY value.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#centerOnX
* @since 3.16.0
*
* @param {number} x - The horizontal coordinate to center on.
*
* @return {this} This Camera instance.
*/
centerOnX: function (x)
{
var originX = this.width * 0.5;
this.midPoint.x = x;
this.scrollX = x - originX;
if (this.useBounds)
{
this.scrollX = this.clampX(this.scrollX);
}
return this;
},
/**
* Moves the Camera vertically so that it is centered on the given y coordinate, bounds allowing.
* Calling this does not change the scrollX value.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#centerOnY
* @since 3.16.0
*
* @param {number} y - The vertical coordinate to center on.
*
* @return {this} This Camera instance.
*/
centerOnY: function (y)
{
var originY = this.height * 0.5;
this.midPoint.y = y;
this.scrollY = y - originY;
if (this.useBounds)
{
this.scrollY = this.clampY(this.scrollY);
}
return this;
},
/**
* Moves the Camera so that it is centered on the given coordinates, bounds allowing.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#centerOn
* @since 3.11.0
*
* @param {number} x - The horizontal coordinate to center on.
* @param {number} y - The vertical coordinate to center on.
*
* @return {this} This Camera instance.
*/
centerOn: function (x, y)
{
this.centerOnX(x);
this.centerOnY(y);
return this;
},
/**
* Moves the Camera so that it is looking at the center of the Camera Bounds, if enabled.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#centerToBounds
* @since 3.0.0
*
* @return {this} This Camera instance.
*/
centerToBounds: function ()
{
if (this.useBounds)
{
var bounds = this._bounds;
var originX = this.width * 0.5;
var originY = this.height * 0.5;
this.midPoint.set(bounds.centerX, bounds.centerY);
this.scrollX = bounds.centerX - originX;
this.scrollY = bounds.centerY - originY;
}
return this;
},
/**
* Moves the Camera so that it is re-centered based on its viewport size.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#centerToSize
* @since 3.0.0
*
* @return {this} This Camera instance.
*/
centerToSize: function ()
{
this.scrollX = this.width * 0.5;
this.scrollY = this.height * 0.5;
return this;
},
/**
* Takes an array of Game Objects and returns a new array featuring only those objects
* visible by this camera.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#cull
* @since 3.0.0
*
* @generic {Phaser.GameObjects.GameObject[]} G - [renderableObjects,$return]
*
* @param {Phaser.GameObjects.GameObject[]} renderableObjects - An array of Game Objects to cull.
*
* @return {Phaser.GameObjects.GameObject[]} An array of Game Objects visible to this Camera.
*/
cull: function (renderableObjects)
{
if (this.disableCull)
{
return renderableObjects;
}
var cameraMatrix = this.matrix.matrix;
var mva = cameraMatrix[0];
var mvb = cameraMatrix[1];
var mvc = cameraMatrix[2];
var mvd = cameraMatrix[3];
/* First Invert Matrix */
var determinant = (mva * mvd) - (mvb * mvc);
if (!determinant)
{
return renderableObjects;
}
var mve = cameraMatrix[4];
var mvf = cameraMatrix[5];
var scrollX = this.scrollX;
var scrollY = this.scrollY;
var cameraW = this.width;
var cameraH = this.height;
var cullTop = this.y;
var cullBottom = cullTop + cameraH;
var cullLeft = this.x;
var cullRight = cullLeft + cameraW;
var culledObjects = this.culledObjects;
var length = renderableObjects.length;
determinant = 1 / determinant;
culledObjects.length = 0;
for (var index = 0; index < length; ++index)
{
var object = renderableObjects[index];
if (!object.hasOwnProperty('width') || object.parentContainer)
{
culledObjects.push(object);
continue;
}
var objectW = object.width;
var objectH = object.height;
var objectX = (object.x - (scrollX * object.scrollFactorX)) - (objectW * object.originX);
var objectY = (object.y - (scrollY * object.scrollFactorY)) - (objectH * object.originY);
var tx = (objectX * mva + objectY * mvc + mve);
var ty = (objectX * mvb + objectY * mvd + mvf);
var tw = ((objectX + objectW) * mva + (objectY + objectH) * mvc + mve);
var th = ((objectX + objectW) * mvb + (objectY + objectH) * mvd + mvf);
if ((tw > cullLeft && tx < cullRight) && (th > cullTop && ty < cullBottom))
{
culledObjects.push(object);
}
}
return culledObjects;
},
/**
* Converts the given `x` and `y` coordinates into World space, based on this Cameras transform.
* You can optionally provide a Vector2, or similar object, to store the results in.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#getWorldPoint
* @since 3.0.0
*
* @generic {Phaser.Math.Vector2} O - [output,$return]
*
* @param {number} x - The x position to convert to world space.
* @param {number} y - The y position to convert to world space.
* @param {(object|Phaser.Math.Vector2)} [output] - An optional object to store the results in. If not provided a new Vector2 will be created.
*
* @return {Phaser.Math.Vector2} An object holding the converted values in its `x` and `y` properties.
*/
getWorldPoint: function (x, y, output)
{
if (output === undefined) { output = new Vector2(); }
var cameraMatrix = this.matrix.matrix;
var mva = cameraMatrix[0];
var mvb = cameraMatrix[1];
var mvc = cameraMatrix[2];
var mvd = cameraMatrix[3];
var mve = cameraMatrix[4];
var mvf = cameraMatrix[5];
// Invert Matrix
var determinant = (mva * mvd) - (mvb * mvc);
if (!determinant)
{
output.x = x;
output.y = y;
return output;
}
determinant = 1 / determinant;
var ima = mvd * determinant;
var imb = -mvb * determinant;
var imc = -mvc * determinant;
var imd = mva * determinant;
var ime = (mvc * mvf - mvd * mve) * determinant;
var imf = (mvb * mve - mva * mvf) * determinant;
var c = Math.cos(this.rotation);
var s = Math.sin(this.rotation);
var zoomX = this.zoomX;
var zoomY = this.zoomY;
var scrollX = this.scrollX;
var scrollY = this.scrollY;
var sx = x + ((scrollX * c - scrollY * s) * zoomX);
var sy = y + ((scrollX * s + scrollY * c) * zoomY);
// Apply transform to point
output.x = (sx * ima + sy * imc) + ime;
output.y = (sx * imb + sy * imd) + imf;
return output;
},
/**
* Given a Game Object, or an array of Game Objects, it will update all of their camera filter settings
* so that they are ignored by this Camera. This means they will not be rendered by this Camera.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#ignore
* @since 3.0.0
*
* @param {(Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]|Phaser.GameObjects.Group|Phaser.GameObjects.Layer|Phaser.GameObjects.Layer[])} entries - The Game Object, or array of Game Objects, to be ignored by this Camera.
*
* @return {this} This Camera instance.
*/
ignore: function (entries)
{
var id = this.id;
if (!Array.isArray(entries))
{
entries = [ entries ];
}
for (var i = 0; i < entries.length; i++)
{
var entry = entries[i];
if (Array.isArray(entry))
{
this.ignore(entry);
}
else if (entry.isParent)
{
this.ignore(entry.getChildren());
}
else
{
entry.cameraFilter |= id;
}
}
return this;
},
/**
* Internal preRender step.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#preRender
* @protected
* @since 3.0.0
*/
preRender: function ()
{
this.renderList.length = 0;
var width = this.width;
var height = this.height;
var halfWidth = width * 0.5;
var halfHeight = height * 0.5;
var zoomX = this.zoomX;
var zoomY = this.zoomY;
var matrix = this.matrix;
var originX = width * this.originX;
var originY = height * this.originY;
var sx = this.scrollX;
var sy = this.scrollY;
if (this.useBounds)
{
sx = this.clampX(sx);
sy = this.clampY(sy);
}
// Values are in pixels and not impacted by zooming the Camera
this.scrollX = sx;
this.scrollY = sy;
var midX = sx + halfWidth;
var midY = sy + halfHeight;
// The center of the camera, in world space, so taking zoom into account
// Basically the pixel value of what it's looking at in the middle of the cam
this.midPoint.set(midX, midY);
var displayWidth = width / zoomX;
var displayHeight = height / zoomY;
this.worldView.setTo(
midX - (displayWidth / 2),
midY - (displayHeight / 2),
displayWidth,
displayHeight
);
matrix.applyITRS(this.x + originX, this.y + originY, this.rotation, zoomX, zoomY);
matrix.translate(-originX, -originY);
},
/**
* Takes an x value and checks it's within the range of the Camera bounds, adjusting if required.
* Do not call this method if you are not using camera bounds.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#clampX
* @since 3.11.0
*
* @param {number} x - The value to horizontally scroll clamp.
*
* @return {number} The adjusted value to use as scrollX.
*/
clampX: function (x)
{
var bounds = this._bounds;
var dw = this.displayWidth;
var bx = bounds.x + ((dw - this.width) / 2);
var bw = Math.max(bx, bx + bounds.width - dw);
if (x < bx)
{
x = bx;
}
else if (x > bw)
{
x = bw;
}