/
Transform.js
605 lines (521 loc) · 16.4 KB
/
Transform.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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var MATH_CONST = require('../../math/const');
var TransformMatrix = require('./TransformMatrix');
var TransformXY = require('../../math/TransformXY');
var WrapAngle = require('../../math/angle/Wrap');
var WrapAngleDegrees = require('../../math/angle/WrapDegrees');
var Vector2 = require('../../math/Vector2');
// global bitmask flag for GameObject.renderMask (used by Scale)
var _FLAG = 4; // 0100
/**
* Provides methods used for getting and setting the position, scale and rotation of a Game Object.
*
* @namespace Phaser.GameObjects.Components.Transform
* @since 3.0.0
*/
var Transform = {
/**
* Private internal value. Holds the horizontal scale value.
*
* @name Phaser.GameObjects.Components.Transform#_scaleX
* @type {number}
* @private
* @default 1
* @since 3.0.0
*/
_scaleX: 1,
/**
* Private internal value. Holds the vertical scale value.
*
* @name Phaser.GameObjects.Components.Transform#_scaleY
* @type {number}
* @private
* @default 1
* @since 3.0.0
*/
_scaleY: 1,
/**
* Private internal value. Holds the rotation value in radians.
*
* @name Phaser.GameObjects.Components.Transform#_rotation
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
_rotation: 0,
/**
* The x position of this Game Object.
*
* @name Phaser.GameObjects.Components.Transform#x
* @type {number}
* @default 0
* @since 3.0.0
*/
x: 0,
/**
* The y position of this Game Object.
*
* @name Phaser.GameObjects.Components.Transform#y
* @type {number}
* @default 0
* @since 3.0.0
*/
y: 0,
/**
* The z position of this Game Object.
*
* Note: The z position does not control the rendering order of 2D Game Objects. Use
* {@link Phaser.GameObjects.Components.Depth#depth} instead.
*
* @name Phaser.GameObjects.Components.Transform#z
* @type {number}
* @default 0
* @since 3.0.0
*/
z: 0,
/**
* The w position of this Game Object.
*
* @name Phaser.GameObjects.Components.Transform#w
* @type {number}
* @default 0
* @since 3.0.0
*/
w: 0,
/**
* This is a special setter that allows you to set both the horizontal and vertical scale of this Game Object
* to the same value, at the same time. When reading this value the result returned is `(scaleX + scaleY) / 2`.
*
* Use of this property implies you wish the horizontal and vertical scales to be equal to each other. If this
* isn't the case, use the `scaleX` or `scaleY` properties instead.
*
* @name Phaser.GameObjects.Components.Transform#scale
* @type {number}
* @default 1
* @since 3.18.0
*/
scale: {
get: function ()
{
return (this._scaleX + this._scaleY) / 2;
},
set: function (value)
{
this._scaleX = value;
this._scaleY = value;
if (value === 0)
{
this.renderFlags &= ~_FLAG;
}
else
{
this.renderFlags |= _FLAG;
}
}
},
/**
* The horizontal scale of this Game Object.
*
* @name Phaser.GameObjects.Components.Transform#scaleX
* @type {number}
* @default 1
* @since 3.0.0
*/
scaleX: {
get: function ()
{
return this._scaleX;
},
set: function (value)
{
this._scaleX = value;
if (value === 0)
{
this.renderFlags &= ~_FLAG;
}
else
{
this.renderFlags |= _FLAG;
}
}
},
/**
* The vertical scale of this Game Object.
*
* @name Phaser.GameObjects.Components.Transform#scaleY
* @type {number}
* @default 1
* @since 3.0.0
*/
scaleY: {
get: function ()
{
return this._scaleY;
},
set: function (value)
{
this._scaleY = value;
if (value === 0)
{
this.renderFlags &= ~_FLAG;
}
else
{
this.renderFlags |= _FLAG;
}
}
},
/**
* The angle of this Game Object as expressed in degrees.
*
* Phaser uses a right-hand clockwise rotation system, where 0 is right, 90 is down, 180/-180 is left
* and -90 is up.
*
* If you prefer to work in radians, see the `rotation` property instead.
*
* @name Phaser.GameObjects.Components.Transform#angle
* @type {number}
* @default 0
* @since 3.0.0
*/
angle: {
get: function ()
{
return WrapAngleDegrees(this._rotation * MATH_CONST.RAD_TO_DEG);
},
set: function (value)
{
// value is in degrees
this.rotation = WrapAngleDegrees(value) * MATH_CONST.DEG_TO_RAD;
}
},
/**
* The angle of this Game Object in radians.
*
* Phaser uses a right-hand clockwise rotation system, where 0 is right, PI/2 is down, +-PI is left
* and -PI/2 is up.
*
* If you prefer to work in degrees, see the `angle` property instead.
*
* @name Phaser.GameObjects.Components.Transform#rotation
* @type {number}
* @default 1
* @since 3.0.0
*/
rotation: {
get: function ()
{
return this._rotation;
},
set: function (value)
{
// value is in radians
this._rotation = WrapAngle(value);
}
},
/**
* Sets the position of this Game Object.
*
* @method Phaser.GameObjects.Components.Transform#setPosition
* @since 3.0.0
*
* @param {number} [x=0] - The x position of this Game Object.
* @param {number} [y=x] - The y position of this Game Object. If not set it will use the `x` value.
* @param {number} [z=0] - The z position of this Game Object.
* @param {number} [w=0] - The w position of this Game Object.
*
* @return {this} This Game Object instance.
*/
setPosition: function (x, y, z, w)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = x; }
if (z === undefined) { z = 0; }
if (w === undefined) { w = 0; }
this.x = x;
this.y = y;
this.z = z;
this.w = w;
return this;
},
/**
* Copies an object's coordinates to this Game Object's position.
*
* @method Phaser.GameObjects.Components.Transform#copyPosition
* @since 3.50.0
*
* @param {(Phaser.Types.Math.Vector2Like|Phaser.Types.Math.Vector3Like|Phaser.Types.Math.Vector4Like)} source - An object with numeric 'x', 'y', 'z', or 'w' properties. Undefined values are not copied.
*
* @return {this} This Game Object instance.
*/
copyPosition: function (source)
{
if (source.x !== undefined) { this.x = source.x; }
if (source.y !== undefined) { this.y = source.y; }
if (source.z !== undefined) { this.z = source.z; }
if (source.w !== undefined) { this.w = source.w; }
return this;
},
/**
* Sets the position of this Game Object to be a random position within the confines of
* the given area.
*
* If no area is specified a random position between 0 x 0 and the game width x height is used instead.
*
* The position does not factor in the size of this Game Object, meaning that only the origin is
* guaranteed to be within the area.
*
* @method Phaser.GameObjects.Components.Transform#setRandomPosition
* @since 3.8.0
*
* @param {number} [x=0] - The x position of the top-left of the random area.
* @param {number} [y=0] - The y position of the top-left of the random area.
* @param {number} [width] - The width of the random area.
* @param {number} [height] - The height of the random area.
*
* @return {this} This Game Object instance.
*/
setRandomPosition: function (x, y, width, height)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = this.scene.sys.scale.width; }
if (height === undefined) { height = this.scene.sys.scale.height; }
this.x = x + (Math.random() * width);
this.y = y + (Math.random() * height);
return this;
},
/**
* Sets the rotation of this Game Object.
*
* @method Phaser.GameObjects.Components.Transform#setRotation
* @since 3.0.0
*
* @param {number} [radians=0] - The rotation of this Game Object, in radians.
*
* @return {this} This Game Object instance.
*/
setRotation: function (radians)
{
if (radians === undefined) { radians = 0; }
this.rotation = radians;
return this;
},
/**
* Sets the angle of this Game Object.
*
* @method Phaser.GameObjects.Components.Transform#setAngle
* @since 3.0.0
*
* @param {number} [degrees=0] - The rotation of this Game Object, in degrees.
*
* @return {this} This Game Object instance.
*/
setAngle: function (degrees)
{
if (degrees === undefined) { degrees = 0; }
this.angle = degrees;
return this;
},
/**
* Sets the scale of this Game Object.
*
* @method Phaser.GameObjects.Components.Transform#setScale
* @since 3.0.0
*
* @param {number} x - The horizontal scale of this Game Object.
* @param {number} [y=x] - The vertical scale of this Game Object. If not set it will use the `x` value.
*
* @return {this} This Game Object instance.
*/
setScale: function (x, y)
{
if (x === undefined) { x = 1; }
if (y === undefined) { y = x; }
this.scaleX = x;
this.scaleY = y;
return this;
},
/**
* Sets the x position of this Game Object.
*
* @method Phaser.GameObjects.Components.Transform#setX
* @since 3.0.0
*
* @param {number} [value=0] - The x position of this Game Object.
*
* @return {this} This Game Object instance.
*/
setX: function (value)
{
if (value === undefined) { value = 0; }
this.x = value;
return this;
},
/**
* Sets the y position of this Game Object.
*
* @method Phaser.GameObjects.Components.Transform#setY
* @since 3.0.0
*
* @param {number} [value=0] - The y position of this Game Object.
*
* @return {this} This Game Object instance.
*/
setY: function (value)
{
if (value === undefined) { value = 0; }
this.y = value;
return this;
},
/**
* Sets the z position of this Game Object.
*
* Note: The z position does not control the rendering order of 2D Game Objects. Use
* {@link Phaser.GameObjects.Components.Depth#setDepth} instead.
*
* @method Phaser.GameObjects.Components.Transform#setZ
* @since 3.0.0
*
* @param {number} [value=0] - The z position of this Game Object.
*
* @return {this} This Game Object instance.
*/
setZ: function (value)
{
if (value === undefined) { value = 0; }
this.z = value;
return this;
},
/**
* Sets the w position of this Game Object.
*
* @method Phaser.GameObjects.Components.Transform#setW
* @since 3.0.0
*
* @param {number} [value=0] - The w position of this Game Object.
*
* @return {this} This Game Object instance.
*/
setW: function (value)
{
if (value === undefined) { value = 0; }
this.w = value;
return this;
},
/**
* Gets the local transform matrix for this Game Object.
*
* @method Phaser.GameObjects.Components.Transform#getLocalTransformMatrix
* @since 3.4.0
*
* @param {Phaser.GameObjects.Components.TransformMatrix} [tempMatrix] - The matrix to populate with the values from this Game Object.
*
* @return {Phaser.GameObjects.Components.TransformMatrix} The populated Transform Matrix.
*/
getLocalTransformMatrix: function (tempMatrix)
{
if (tempMatrix === undefined) { tempMatrix = new TransformMatrix(); }
return tempMatrix.applyITRS(this.x, this.y, this._rotation, this._scaleX, this._scaleY);
},
/**
* Gets the world transform matrix for this Game Object, factoring in any parent Containers.
*
* @method Phaser.GameObjects.Components.Transform#getWorldTransformMatrix
* @since 3.4.0
*
* @param {Phaser.GameObjects.Components.TransformMatrix} [tempMatrix] - The matrix to populate with the values from this Game Object.
* @param {Phaser.GameObjects.Components.TransformMatrix} [parentMatrix] - A temporary matrix to hold parent values during the calculations.
*
* @return {Phaser.GameObjects.Components.TransformMatrix} The populated Transform Matrix.
*/
getWorldTransformMatrix: function (tempMatrix, parentMatrix)
{
if (tempMatrix === undefined) { tempMatrix = new TransformMatrix(); }
if (parentMatrix === undefined) { parentMatrix = new TransformMatrix(); }
var parent = this.parentContainer;
if (!parent)
{
return this.getLocalTransformMatrix(tempMatrix);
}
tempMatrix.applyITRS(this.x, this.y, this._rotation, this._scaleX, this._scaleY);
while (parent)
{
parentMatrix.applyITRS(parent.x, parent.y, parent._rotation, parent._scaleX, parent._scaleY);
parentMatrix.multiply(tempMatrix, tempMatrix);
parent = parent.parentContainer;
}
return tempMatrix;
},
/**
* Takes the given `x` and `y` coordinates and converts them into local space for this
* Game Object, taking into account parent and local transforms, and the Display Origin.
*
* The returned Vector2 contains the translated point in its properties.
*
* A Camera needs to be provided in order to handle modified scroll factors. If no
* camera is specified, it will use the `main` camera from the Scene to which this
* Game Object belongs.
*
* @method Phaser.GameObjects.Components.Transform#getLocalPoint
* @since 3.50.0
*
* @param {number} x - The x position to translate.
* @param {number} y - The y position to translate.
* @param {Phaser.Math.Vector2} [point] - A Vector2, or point-like object, to store the results in.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera which is being tested against. If not given will use the Scene default camera.
*
* @return {Phaser.Math.Vector2} The translated point.
*/
getLocalPoint: function (x, y, point, camera)
{
if (!point) { point = new Vector2(); }
if (!camera) { camera = this.scene.sys.cameras.main; }
var csx = camera.scrollX;
var csy = camera.scrollY;
var px = x + (csx * this.scrollFactorX) - csx;
var py = y + (csy * this.scrollFactorY) - csy;
if (this.parentContainer)
{
this.getWorldTransformMatrix().applyInverse(px, py, point);
}
else
{
TransformXY(px, py, this.x, this.y, this.rotation, this.scaleX, this.scaleY, point);
}
// Normalize origin
if (this._originComponent)
{
point.x += this._displayOriginX;
point.y += this._displayOriginY;
}
return point;
},
/**
* Gets the sum total rotation of all of this Game Objects parent Containers.
*
* The returned value is in radians and will be zero if this Game Object has no parent container.
*
* @method Phaser.GameObjects.Components.Transform#getParentRotation
* @since 3.18.0
*
* @return {number} The sum total rotation, in radians, of all parent containers of this Game Object.
*/
getParentRotation: function ()
{
var rotation = 0;
var parent = this.parentContainer;
while (parent)
{
rotation += parent.rotation;
parent = parent.parentContainer;
}
return rotation;
}
};
module.exports = Transform;