phaser/src/gameobjects/zone/Zone.js

316 lines
9.3 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
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
*/
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');
var Components = require('../components');
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
*
* @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
*
* @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: [
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:
function Zone (scene, x, y, width, height)
2017-03-27 22:10:04 +00:00
{
if (width === undefined) { width = 1; }
if (height === undefined) { height = width; }
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;
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
*/
this.blendMode = BlendModes.NORMAL;
this.updateDisplayOrigin();
},
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.
*
* @return {this} This Game Object.
2018-02-06 16:15:22 +00:00
*/
setSize: function (width, height, resizeInput)
2018-01-04 22:02:29 +00:00
{
if (resizeInput === undefined) { resizeInput = true; }
2018-01-04 22:02:29 +00:00
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;
}
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.
*
* @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.
*
* @return {this} This Game Object.
2018-02-06 16:15:22 +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.
*
* @return {this} This Game Object.
2018-02-06 16:15:22 +00:00
*/
setRectangleDropZone: function (width, height)
{
2018-09-20 10:49:24 +00:00
return this.setDropZone(new Rectangle(0, 0, width, height), RectangleContains);
},
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
*
* @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
*
* @return {this} This Game Object.
2018-02-06 16:15:22 +00:00
*/
setDropZone: function (hitArea, hitAreaCallback)
{
if (!this.input)
{
this.setInteractive(hitArea, hitAreaCallback, true);
}
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
/**
* 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)
{
2021-01-21 03:13:33 +00:00
camera.addToRenderList(src);
},
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)
{
2021-01-21 03:13:33 +00:00
camera.addToRenderList(src);
2017-03-27 22:10:04 +00:00
}
});
module.exports = Zone;