forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainer.js
More file actions
298 lines (250 loc) · 8.66 KB
/
Copy pathContainer.js
File metadata and controls
298 lines (250 loc) · 8.66 KB
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
/**
* @author Richard Davey <rich@photonstorm.com>
* @author Felipe Alfonso <@bitnenfer>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
var Components = require('../components');
var GameObject = require('../GameObject');
var List = require('../../structs/List');
var Render = require('./ContainerRender');
var Vector2 = require('../../math/Vector2');
/**
* @classdesc
* A Container Game Object.
*
* @class Container
* @extends Phaser.GameObjects.GameObject
* @memberOf Phaser.GameObjects
* @constructor
* @since 3.4.0
*
* @extends Phaser.GameObjects.Components.BlendMode
* @extends Phaser.GameObjects.Components.Transform
* @extends Phaser.Structs.List
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
* @param {number} [x=0] - The horizontal position of this Game Object in the world.
* @param {number} [y=0] - The vertical position of this Game Object in the world.
* @param {Phaser.GameObjects.GameObject[]} [children] - An optional array of Game Objects to add to this Container.
*/
var Container = new Class({
Extends: GameObject,
Mixins: [
Components.BlendMode,
Components.Depth,
Components.Transform,
Components.Visible,
Render,
List
],
initialize:
function Container (scene, x, y, children)
{
GameObject.call(this, scene, 'Container');
/**
* The parent property as used by the List methods.
*
* @name Phaser.GameObjects.Container#parent
* @type {Phaser.GameObjects.Container}
* @private
* @since 3.4.0
*/
this.parent = this;
/**
* An array holding the children of this Container.
*
* @name Phaser.GameObjects.Container#list
* @type {Phaser.GameObjects.GameObject[]}
* @since 3.4.0
*/
this.list = [];
/**
* The cursor position.
*
* @name Phaser.GameObjects.Container#position
* @type {integer}
* @since 3.4.0
*/
this.position = 0;
/**
* [description]
*
* @name Phaser.GameObjects.Container#localTransform
* @type {Phaser.GameObjects.Components.TransformMatrix}
* @since 3.4.0
*/
this.localTransform = new Components.TransformMatrix();
/**
* [description]
*
* @name Phaser.GameObjects.Container#tempTransformMatrix
* @type {Phaser.GameObjects.Components.TransformMatrix}
* @private
* @since 3.4.0
*/
this.tempTransformMatrix = new Components.TransformMatrix();
/**
* A callback that is invoked every time a child is added to this list.
*
* @name Phaser.GameObjects.Container#addCallback
* @type {function}
* @private
* @since 3.4.0
*/
this.addCallback = this.addHandler;
/**
* A callback that is invoked every time a child is removed from this list.
*
* @name Phaser.GameObjects.Container#removeCallback
* @type {function}
* @private
* @since 3.4.0
*/
this.removeCallback = this.removeHandler;
/**
* A reference to the Scene Display List.
*
* @name Phaser.GameObjects.Container#_displayList
* @type {Phaser.GameObjects.DisplayList}
* @private
* @since 3.4.0
*/
this._displayList = scene.sys.displayList;
this.setPosition(x, y);
if (Array.isArray(children))
{
this.addMultiple(children);
}
},
/**
* Internal add handler.
*
* @method Phaser.GameObjects.Container#addHandler
* @private
* @since 3.4.0
*
* @param {Phaser.GameObjects.Container} list - The List the Game Object was added to (also this Container)
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object that was just added to this Container.
*/
addHandler: function (list, gameObject)
{
this._displayList.remove(gameObject);
gameObject.on('destroy', this.remove, this);
if (gameObject.parentContainer)
{
gameObject.parentContainer.remove(gameObject);
}
gameObject.parentContainer = list;
},
/**
* Internal remove handler.
*
* @method Phaser.GameObjects.Container#removeHandler
* @private
* @since 3.4.0
*
* @param {Phaser.GameObjects.Container} list - The List the Game Object was removed from (also this Container)
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object that was just removed from this Container.
*/
removeHandler: function (list, gameObject)
{
gameObject.off('destroy', list.remove, this);
gameObject.parentContainer = null;
},
/**
* Takes a Point-like object, such as a Vector2, Geom.Point or object with public x and y properties,
* and transforms it into the space of this Container, then returns it in the output object.
*
* @method Phaser.GameObjects.Container#pointToContainer
* @since 3.4.0
*
* @param {(object|Phaser.Geom.Point|Phaser.Math.Vector2)} source - The Source Point to be transformed.
* @param {(object|Phaser.Geom.Point|Phaser.Math.Vector2)} [output] - A destination object to store the transformed point in. If none given a Vector2 will be created and returned.
*
* @return {(object|Phaser.Geom.Point|Phaser.Math.Vector2)} The transformed point.
*/
pointToContainer: function (source, output)
{
if (output === undefined) { output = new Vector2(); }
if (this.parentContainer)
{
return this.parentContainer.pointToContainer(source, output);
}
var tempMatrix = this.tempTransformMatrix;
// No need to loadIdentity because applyITRS overwrites every value anyway
tempMatrix.applyITRS(this.x, this.y, this.rotation, this.scaleX, this.scaleY);
tempMatrix.invert();
tempMatrix.transformPoint(source.x, source.y, output);
return output;
},
/**
*
*
* @method Phaser.GameObjects.Container#localToWorld
* @since 3.4.0
*
* @param {Phaser.GameObjects.GameObject} child - The child of this Container.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The camera to transform against.
* @param {(object|Phaser.Geom.Point|Phaser.Math.Vector2)} [output] - A destination object to store the transformed point in. If none given a Vector2 will be created and returned.
*
* @return {(object|Phaser.Geom.Point|Phaser.Math.Vector2)} The transformed point.
*/
localToWorld: function (child, camera, output)
{
if (camera === undefined) { camera = this.scene.sys.cameras.main; }
if (output === undefined) { output = new Vector2(); }
if (this.exists(child))
{
// Do matrix magic here
// See: Camera.getWorldPoint and InputManager.hitTest
}
return output;
},
/**
* Destroys this Container removing it and all children from the Display List and
* severing all ties to parent resources.
*
* Use this to remove a Container from your game if you don't ever plan to use it again.
* As long as no reference to it exists within your own code it should become free for
* garbage collection by the browser.
*
* If you just want to temporarily disable an object then look at using the
* Game Object Pool instead of destroying it, as destroyed objects cannot be resurrected.
*
* @method Phaser.GameObjects.Container#destroy
* @since 3.4.0
*/
destroy: function ()
{
// This Game Object had already been destroyed
if (!this.scene)
{
return;
}
if (this.preDestroy)
{
this.preDestroy.call(this);
}
this.emit('destroy', this);
var sys = this.scene.sys;
sys.displayList.remove(this);
if (this.data)
{
this.data.destroy();
this.data = undefined;
}
// Tell the Scene to re-sort the children
sys.queueDepthSort();
this.active = false;
this.visible = false;
this.scene = undefined;
this.removeAllListeners();
this.removeAll();
this.list = [];
this.parent = null;
this.parentContainer = null;
}
});
module.exports = Container;