/
Zone.js
315 lines (276 loc) · 9.34 KB
/
Zone.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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var BlendModes = require('../../renderer/BlendModes');
var Circle = require('../../geom/circle/Circle');
var CircleContains = require('../../geom/circle/Contains');
var Class = require('../../utils/Class');
var Components = require('../components');
var GameObject = require('../GameObject');
var Rectangle = require('../../geom/rectangle/Rectangle');
var RectangleContains = require('../../geom/rectangle/Contains');
/**
* @classdesc
* A Zone Game Object.
*
* A Zone is a non-rendering rectangular Game Object that has a position and size.
* It has no texture and never displays, but does live on the display list and
* can be moved, scaled and rotated like any other Game Object.
*
* Its primary use is for creating Drop Zones and Input Hit Areas and it has a couple of helper methods
* specifically for this. It is also useful for object overlap checks, or as a base for your own
* non-displaying Game Objects.
* The default origin is 0.5, the center of the Zone, the same as with Game Objects.
*
* @class Zone
* @extends Phaser.GameObjects.GameObject
* @memberof Phaser.GameObjects
* @constructor
* @since 3.0.0
*
* @extends Phaser.GameObjects.Components.Depth
* @extends Phaser.GameObjects.Components.GetBounds
* @extends Phaser.GameObjects.Components.Origin
* @extends Phaser.GameObjects.Components.Transform
* @extends Phaser.GameObjects.Components.ScrollFactor
* @extends Phaser.GameObjects.Components.Visible
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs.
* @param {number} x - The horizontal position of this Game Object in the world.
* @param {number} y - The vertical position of this Game Object in the world.
* @param {number} [width=1] - The width of the Game Object.
* @param {number} [height=1] - The height of the Game Object.
*/
var Zone = new Class({
Extends: GameObject,
Mixins: [
Components.Depth,
Components.GetBounds,
Components.Origin,
Components.Transform,
Components.ScrollFactor,
Components.Visible
],
initialize:
function Zone (scene, x, y, width, height)
{
if (width === undefined) { width = 1; }
if (height === undefined) { height = width; }
GameObject.call(this, scene, 'Zone');
this.setPosition(x, y);
/**
* The native (un-scaled) width of this Game Object.
*
* @name Phaser.GameObjects.Zone#width
* @type {number}
* @since 3.0.0
*/
this.width = width;
/**
* The native (un-scaled) height of this Game Object.
*
* @name Phaser.GameObjects.Zone#height
* @type {number}
* @since 3.0.0
*/
this.height = height;
/**
* The Blend Mode of the Game Object.
* Although a Zone never renders, it still has a blend mode to allow it to fit seamlessly into
* display lists without causing a batch flush.
*
* @name Phaser.GameObjects.Zone#blendMode
* @type {number}
* @since 3.0.0
*/
this.blendMode = BlendModes.NORMAL;
this.updateDisplayOrigin();
},
/**
* The displayed width of this Game Object.
* This value takes into account the scale factor.
*
* @name Phaser.GameObjects.Zone#displayWidth
* @type {number}
* @since 3.0.0
*/
displayWidth: {
get: function ()
{
return this.scaleX * this.width;
},
set: function (value)
{
this.scaleX = value / this.width;
}
},
/**
* The displayed height of this Game Object.
* This value takes into account the scale factor.
*
* @name Phaser.GameObjects.Zone#displayHeight
* @type {number}
* @since 3.0.0
*/
displayHeight: {
get: function ()
{
return this.scaleY * this.height;
},
set: function (value)
{
this.scaleY = value / this.height;
}
},
/**
* Sets the size of this Game Object.
*
* @method Phaser.GameObjects.Zone#setSize
* @since 3.0.0
*
* @param {number} width - The width of this Game Object.
* @param {number} height - The height of this Game Object.
* @param {boolean} [resizeInput=true] - If this Zone has a Rectangle for a hit area this argument will resize the hit area as well.
*
* @return {this} This Game Object.
*/
setSize: function (width, height, resizeInput)
{
if (resizeInput === undefined) { resizeInput = true; }
this.width = width;
this.height = height;
this.updateDisplayOrigin();
var input = this.input;
if (resizeInput && input && !input.customHitArea)
{
input.hitArea.width = width;
input.hitArea.height = height;
}
return this;
},
/**
* Sets the display size of this Game Object.
* Calling this will adjust the scale.
*
* @method Phaser.GameObjects.Zone#setDisplaySize
* @since 3.0.0
*
* @param {number} width - The width of this Game Object.
* @param {number} height - The height of this Game Object.
*
* @return {this} This Game Object.
*/
setDisplaySize: function (width, height)
{
this.displayWidth = width;
this.displayHeight = height;
return this;
},
/**
* Sets this Zone to be a Circular Drop Zone.
* The circle is centered on this Zones `x` and `y` coordinates.
*
* @method Phaser.GameObjects.Zone#setCircleDropZone
* @since 3.0.0
*
* @param {number} radius - The radius of the Circle that will form the Drop Zone.
*
* @return {this} This Game Object.
*/
setCircleDropZone: function (radius)
{
return this.setDropZone(new Circle(0, 0, radius), CircleContains);
},
/**
* Sets this Zone to be a Rectangle Drop Zone.
* The rectangle is centered on this Zones `x` and `y` coordinates.
*
* @method Phaser.GameObjects.Zone#setRectangleDropZone
* @since 3.0.0
*
* @param {number} width - The width of the rectangle drop zone.
* @param {number} height - The height of the rectangle drop zone.
*
* @return {this} This Game Object.
*/
setRectangleDropZone: function (width, height)
{
return this.setDropZone(new Rectangle(0, 0, width, height), RectangleContains);
},
/**
* Allows you to define your own Geometry shape to be used as a Drop Zone.
*
* @method Phaser.GameObjects.Zone#setDropZone
* @since 3.0.0
*
* @param {object} [hitArea] - A Geometry shape instance, such as Phaser.Geom.Ellipse, or your own custom shape. If not given it will try to create a Rectangle based on the size of this zone.
* @param {Phaser.Types.Input.HitAreaCallback} [hitAreaCallback] - A function that will return `true` if the given x/y coords it is sent are within the shape. If you provide a shape you must also provide a callback.
*
* @return {this} This Game Object.
*/
setDropZone: function (hitArea, hitAreaCallback)
{
if (!this.input)
{
this.setInteractive(hitArea, hitAreaCallback, true);
}
return this;
},
/**
* A NOOP method so you can pass a Zone to a Container.
* Calling this method will do nothing. It is intentionally empty.
*
* @method Phaser.GameObjects.Zone#setAlpha
* @private
* @since 3.11.0
*/
setAlpha: function ()
{
},
/**
* A NOOP method so you can pass a Zone to a Container in Canvas.
* Calling this method will do nothing. It is intentionally empty.
*
* @method Phaser.GameObjects.Zone#setBlendMode
* @private
* @since 3.16.2
*/
setBlendMode: function ()
{
},
/**
* A Zone does not render.
*
* @method Phaser.GameObjects.Zone#renderCanvas
* @private
* @since 3.53.0
*
* @param {Phaser.Renderer.Canvas.CanvasRenderer} renderer - A reference to the current active Canvas renderer.
* @param {Phaser.GameObjects.Image} src - The Game Object being rendered in this call.
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
*/
renderCanvas: function (renderer, src, camera)
{
camera.addToRenderList(src);
},
/**
* A Zone does not render.
*
* @method Phaser.GameObjects.Zone#renderWebGL
* @private
* @since 3.53.0
*
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer.
* @param {Phaser.GameObjects.Image} src - The Game Object being rendered in this call.
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
*/
renderWebGL: function (renderer, src, camera)
{
camera.addToRenderList(src);
}
});
module.exports = Zone;