2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2023-01-02 17:36:27 +00:00
|
|
|
* @copyright 2013-2023 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2017-07-28 01:22:41 +00:00
|
|
|
var BlendModes = require('../../renderer/BlendModes');
|
|
|
|
var Circle = require('../../geom/circle/Circle');
|
|
|
|
var CircleContains = require('../../geom/circle/Contains');
|
2017-03-27 22:10:04 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-07-04 00:59:31 +00:00
|
|
|
var Components = require('../components');
|
2017-07-28 01:22:41 +00:00
|
|
|
var GameObject = require('../GameObject');
|
|
|
|
var Rectangle = require('../../geom/rectangle/Rectangle');
|
|
|
|
var RectangleContains = require('../../geom/rectangle/Contains');
|
|
|
|
|
2018-02-06 16:15:22 +00:00
|
|
|
/**
|
2018-02-07 15:27:21 +00:00
|
|
|
* @classdesc
|
2018-02-06 16:15:22 +00:00
|
|
|
* 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
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.GameObjects
|
2018-02-06 16:15:22 +00:00
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-13 22:19:44 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.Depth
|
2018-02-06 16:15:22 +00:00
|
|
|
* @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
|
|
|
|
*
|
2018-06-20 07:33:55 +00:00
|
|
|
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs.
|
2018-02-06 16:15:22 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
2017-03-27 22:10:04 +00:00
|
|
|
var Zone = new Class({
|
|
|
|
|
2017-04-05 01:10:48 +00:00
|
|
|
Extends: GameObject,
|
|
|
|
|
2017-03-27 22:10:04 +00:00
|
|
|
Mixins: [
|
2018-02-13 22:19:44 +00:00
|
|
|
Components.Depth,
|
2017-03-27 22:10:04 +00:00
|
|
|
Components.GetBounds,
|
|
|
|
Components.Origin,
|
|
|
|
Components.Transform,
|
2017-06-22 02:19:03 +00:00
|
|
|
Components.ScrollFactor,
|
2017-03-27 22:10:04 +00:00
|
|
|
Components.Visible
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
function Zone (scene, x, y, width, height)
|
2017-03-27 22:10:04 +00:00
|
|
|
{
|
2017-07-28 01:22:41 +00:00
|
|
|
if (width === undefined) { width = 1; }
|
|
|
|
if (height === undefined) { height = width; }
|
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
GameObject.call(this, scene, 'Zone');
|
2017-03-27 22:10:04 +00:00
|
|
|
|
|
|
|
this.setPosition(x, y);
|
2018-01-04 22:02:29 +00:00
|
|
|
|
2018-02-06 16:15:22 +00:00
|
|
|
/**
|
|
|
|
* The native (un-scaled) width of this Game Object.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Zone#width
|
|
|
|
* @type {number}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-04 22:02:29 +00:00
|
|
|
this.width = width;
|
2018-02-06 16:15:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The native (un-scaled) height of this Game Object.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Zone#height
|
|
|
|
* @type {number}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-04 22:02:29 +00:00
|
|
|
this.height = height;
|
2017-07-27 16:40:15 +00:00
|
|
|
|
2018-02-06 16:15:22 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2020-11-23 10:22:13 +00:00
|
|
|
* @type {number}
|
2018-02-06 16:15:22 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-27 16:40:15 +00:00
|
|
|
this.blendMode = BlendModes.NORMAL;
|
2018-07-29 11:34:21 +00:00
|
|
|
|
|
|
|
this.updateDisplayOrigin();
|
2017-07-27 16:40:15 +00:00
|
|
|
},
|
|
|
|
|
2018-02-06 16:15:22 +00:00
|
|
|
/**
|
|
|
|
* The displayed width of this Game Object.
|
|
|
|
* This value takes into account the scale factor.
|
2018-03-19 21:27:16 +00:00
|
|
|
*
|
2018-02-06 16:15:22 +00:00
|
|
|
* @name Phaser.GameObjects.Zone#displayWidth
|
|
|
|
* @type {number}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-04 22:02:29 +00:00
|
|
|
displayWidth: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.scaleX * this.width;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this.scaleX = value / this.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2018-02-06 16:15:22 +00:00
|
|
|
/**
|
|
|
|
* The displayed height of this Game Object.
|
|
|
|
* This value takes into account the scale factor.
|
2018-03-19 21:27:16 +00:00
|
|
|
*
|
2018-02-06 16:15:22 +00:00
|
|
|
* @name Phaser.GameObjects.Zone#displayHeight
|
|
|
|
* @type {number}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-04 22:02:29 +00:00
|
|
|
displayHeight: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.scaleY * this.height;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this.scaleY = value / this.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2018-02-06 16:15:22 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2020-01-26 13:52:37 +00:00
|
|
|
* @return {this} This Game Object.
|
2018-02-06 16:15:22 +00:00
|
|
|
*/
|
2018-01-05 16:18:10 +00:00
|
|
|
setSize: function (width, height, resizeInput)
|
2018-01-04 22:02:29 +00:00
|
|
|
{
|
2018-01-05 16:18:10 +00:00
|
|
|
if (resizeInput === undefined) { resizeInput = true; }
|
|
|
|
|
2018-01-04 22:02:29 +00:00
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
|
2019-06-12 05:54:27 +00:00
|
|
|
this.updateDisplayOrigin();
|
|
|
|
|
2019-05-14 02:12:32 +00:00
|
|
|
var input = this.input;
|
|
|
|
|
|
|
|
if (resizeInput && input && !input.customHitArea)
|
2018-01-05 16:18:10 +00:00
|
|
|
{
|
2019-05-14 02:12:32 +00:00
|
|
|
input.hitArea.width = width;
|
|
|
|
input.hitArea.height = height;
|
2018-01-05 16:18:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 22:02:29 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-06 16:15:22 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2020-01-26 13:52:37 +00:00
|
|
|
* @return {this} This Game Object.
|
2018-02-06 16:15:22 +00:00
|
|
|
*/
|
2018-01-04 22:02:29 +00:00
|
|
|
setDisplaySize: function (width, height)
|
|
|
|
{
|
|
|
|
this.displayWidth = width;
|
|
|
|
this.displayHeight = height;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-06 16:15:22 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2020-01-26 13:52:37 +00:00
|
|
|
* @return {this} This Game Object.
|
2018-02-06 16:15:22 +00:00
|
|
|
*/
|
2017-07-28 01:22:41 +00:00
|
|
|
setCircleDropZone: function (radius)
|
|
|
|
{
|
|
|
|
return this.setDropZone(new Circle(0, 0, radius), CircleContains);
|
|
|
|
},
|
|
|
|
|
2018-02-06 16:15:22 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2020-01-26 13:52:37 +00:00
|
|
|
* @return {this} This Game Object.
|
2018-02-06 16:15:22 +00:00
|
|
|
*/
|
2017-07-28 01:22:41 +00:00
|
|
|
setRectangleDropZone: function (width, height)
|
|
|
|
{
|
2018-09-20 10:49:24 +00:00
|
|
|
return this.setDropZone(new Rectangle(0, 0, width, height), RectangleContains);
|
2017-07-28 01:22:41 +00:00
|
|
|
},
|
|
|
|
|
2018-02-06 16:15:22 +00:00
|
|
|
/**
|
|
|
|
* Allows you to define your own Geometry shape to be used as a Drop Zone.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Zone#setDropZone
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2021-06-16 03:16:19 +00:00
|
|
|
* @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.
|
2018-02-06 16:15:22 +00:00
|
|
|
*
|
2020-01-26 13:52:37 +00:00
|
|
|
* @return {this} This Game Object.
|
2018-02-06 16:15:22 +00:00
|
|
|
*/
|
2020-08-03 20:33:30 +00:00
|
|
|
setDropZone: function (hitArea, hitAreaCallback)
|
2017-07-27 16:40:15 +00:00
|
|
|
{
|
2021-06-16 03:16:19 +00:00
|
|
|
if (!this.input)
|
2017-07-28 01:22:41 +00:00
|
|
|
{
|
2020-08-03 20:33:30 +00:00
|
|
|
this.setInteractive(hitArea, hitAreaCallback, true);
|
2017-07-28 01:22:41 +00:00
|
|
|
}
|
2017-07-27 16:40:15 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-06-12 13:49:02 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2018-07-13 10:14:13 +00:00
|
|
|
* @since 3.11.0
|
2018-06-12 13:49:02 +00:00
|
|
|
*/
|
|
|
|
setAlpha: function ()
|
|
|
|
{
|
|
|
|
},
|
2021-02-04 16:11:09 +00:00
|
|
|
|
2019-02-10 17:30:01 +00:00
|
|
|
/**
|
|
|
|
* 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 ()
|
|
|
|
{
|
|
|
|
},
|
|
|
|
|
2018-02-06 16:15:22 +00:00
|
|
|
/**
|
|
|
|
* A Zone does not render.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Zone#renderCanvas
|
|
|
|
* @private
|
2021-02-04 16:11:09 +00:00
|
|
|
* @since 3.53.0
|
|
|
|
*
|
2021-01-21 03:13:33 +00:00
|
|
|
* @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
|
2018-02-06 16:15:22 +00:00
|
|
|
*/
|
2021-02-04 16:11:09 +00:00
|
|
|
renderCanvas: function (renderer, src, camera)
|
2017-07-27 16:40:15 +00:00
|
|
|
{
|
2021-01-21 03:13:33 +00:00
|
|
|
camera.addToRenderList(src);
|
2017-07-27 16:40:15 +00:00
|
|
|
},
|
|
|
|
|
2018-02-06 16:15:22 +00:00
|
|
|
/**
|
|
|
|
* A Zone does not render.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Zone#renderWebGL
|
|
|
|
* @private
|
2021-02-04 16:11:09 +00:00
|
|
|
* @since 3.53.0
|
|
|
|
*
|
2021-01-21 03:13:33 +00:00
|
|
|
* @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
|
2018-02-06 16:15:22 +00:00
|
|
|
*/
|
2021-02-04 16:11:09 +00:00
|
|
|
renderWebGL: function (renderer, src, camera)
|
2017-07-27 16:40:15 +00:00
|
|
|
{
|
2021-01-21 03:13:33 +00:00
|
|
|
camera.addToRenderList(src);
|
2017-03-27 22:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Zone;
|