2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-10-11 16:05:59 +00:00
|
|
|
var CanvasPool = require('../../display/canvas/CanvasPool');
|
2017-04-25 22:09:13 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-07-04 00:59:31 +00:00
|
|
|
var Components = require('../components');
|
2018-03-12 13:37:13 +00:00
|
|
|
var CONST = require('../../const');
|
2017-08-17 01:05:41 +00:00
|
|
|
var GameObject = require('../GameObject');
|
|
|
|
var GetPowerOfTwo = require('../../math/pow2/GetPowerOfTwo');
|
2018-10-02 12:30:45 +00:00
|
|
|
var Smoothing = require('../../display/canvas/Smoothing');
|
2017-04-25 22:09:13 +00:00
|
|
|
var TileSpriteRender = require('./TileSpriteRender');
|
2018-08-03 18:14:06 +00:00
|
|
|
var Vector2 = require('../../math/Vector2');
|
|
|
|
|
|
|
|
// bitmask flag for GameObject.renderMask
|
|
|
|
var _FLAG = 8; // 1000
|
2017-04-25 22:09:13 +00:00
|
|
|
|
2018-02-06 22:56:27 +00:00
|
|
|
/**
|
2018-02-07 15:27:21 +00:00
|
|
|
* @classdesc
|
2018-06-20 07:33:55 +00:00
|
|
|
* A TileSprite is a Sprite that has a repeating texture.
|
|
|
|
*
|
|
|
|
* The texture can be scrolled and scaled independently of the TileSprite itself. Textures will automatically wrap and
|
|
|
|
* are designed so that you can create game backdrops using seamless textures as a source.
|
|
|
|
*
|
2018-06-28 11:59:27 +00:00
|
|
|
* You shouldn't ever create a TileSprite any larger than your actual screen size. If you want to create a large repeating background
|
|
|
|
* that scrolls across the whole map of your game, then you create a TileSprite that fits the screen size and then use the `tilePosition`
|
|
|
|
* property to scroll the texture as the player moves. If you create a TileSprite that is thousands of pixels in size then it will
|
|
|
|
* consume huge amounts of memory and cause performance issues. Remember: use `tilePosition` to scroll your texture and `tileScale` to
|
|
|
|
* adjust the scale of the texture - don't resize the sprite itself or make it larger than it needs.
|
|
|
|
*
|
|
|
|
* An important note about Tile Sprites and NPOT textures: Internally, TileSprite textures use GL_REPEAT to provide
|
|
|
|
* seamless repeating of the textures. This, combined with the way in which the textures are handled in WebGL, means
|
|
|
|
* they need to be POT (power-of-two) sizes in order to wrap. If you provide a NPOT (non power-of-two) texture to a
|
|
|
|
* TileSprite it will generate a POT sized canvas and draw your texture to it, scaled up to the POT size. It's then
|
|
|
|
* scaled back down again during rendering to the original dimensions. While this works, in that it allows you to use
|
|
|
|
* any size texture for a Tile Sprite, it does mean that NPOT textures are going to appear anti-aliased when rendered,
|
|
|
|
* due to the interpolation that took place when it was resized into a POT texture. This is especially visible in
|
|
|
|
* pixel art graphics. If you notice it and it becomes an issue, the only way to avoid it is to ensure that you
|
|
|
|
* provide POT textures for Tile Sprites.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
|
|
|
* @class TileSprite
|
|
|
|
* @extends Phaser.GameObjects.GameObject
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.GameObjects
|
2018-02-06 22:56:27 +00:00
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @extends Phaser.GameObjects.Components.Alpha
|
|
|
|
* @extends Phaser.GameObjects.Components.BlendMode
|
2018-06-27 10:20:21 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.ComputedSize
|
2018-08-03 18:14:06 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.Crop
|
2018-02-06 22:56:27 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.Depth
|
|
|
|
* @extends Phaser.GameObjects.Components.Flip
|
|
|
|
* @extends Phaser.GameObjects.Components.GetBounds
|
2018-04-20 17:57:49 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.Mask
|
2018-02-06 22:56:27 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.Origin
|
|
|
|
* @extends Phaser.GameObjects.Components.Pipeline
|
|
|
|
* @extends Phaser.GameObjects.Components.ScaleMode
|
|
|
|
* @extends Phaser.GameObjects.Components.ScrollFactor
|
|
|
|
* @extends Phaser.GameObjects.Components.Tint
|
|
|
|
* @extends Phaser.GameObjects.Components.Transform
|
|
|
|
* @extends Phaser.GameObjects.Components.Visible
|
|
|
|
*
|
|
|
|
* @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 - The horizontal position of this Game Object in the world.
|
|
|
|
* @param {number} y - The vertical position of this Game Object in the world.
|
2018-10-02 12:13:51 +00:00
|
|
|
* @param {integer} width - The width of the Game Object. If zero it will use the size of the texture frame.
|
|
|
|
* @param {integer} height - The height of the Game Object. If zero it will use the size of the texture frame.
|
2018-08-03 18:14:06 +00:00
|
|
|
* @param {string} textureKey - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
|
|
|
|
* @param {(string|integer)} [frameKey] - An optional frame from the Texture this Game Object is rendering with.
|
2018-02-06 22:56:27 +00:00
|
|
|
*/
|
2017-04-25 22:09:13 +00:00
|
|
|
var TileSprite = new Class({
|
|
|
|
|
|
|
|
Extends: GameObject,
|
|
|
|
|
|
|
|
Mixins: [
|
|
|
|
Components.Alpha,
|
|
|
|
Components.BlendMode,
|
2018-06-27 10:20:21 +00:00
|
|
|
Components.ComputedSize,
|
2018-08-03 18:14:06 +00:00
|
|
|
Components.Crop,
|
2018-02-01 00:50:15 +00:00
|
|
|
Components.Depth,
|
2017-04-25 22:09:13 +00:00
|
|
|
Components.Flip,
|
|
|
|
Components.GetBounds,
|
2018-04-20 17:57:49 +00:00
|
|
|
Components.Mask,
|
2017-04-25 22:09:13 +00:00
|
|
|
Components.Origin,
|
2018-01-29 21:46:48 +00:00
|
|
|
Components.Pipeline,
|
2017-04-25 22:09:13 +00:00
|
|
|
Components.ScaleMode,
|
2017-07-04 11:36:19 +00:00
|
|
|
Components.ScrollFactor,
|
|
|
|
Components.Tint,
|
2017-04-25 22:09:13 +00:00
|
|
|
Components.Transform,
|
|
|
|
Components.Visible,
|
|
|
|
TileSpriteRender
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2018-08-03 18:14:06 +00:00
|
|
|
function TileSprite (scene, x, y, width, height, textureKey, frameKey)
|
2017-04-25 22:09:13 +00:00
|
|
|
{
|
2018-01-25 00:15:51 +00:00
|
|
|
var renderer = scene.sys.game.renderer;
|
2017-04-28 18:10:32 +00:00
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
GameObject.call(this, scene, 'TileSprite');
|
2017-04-25 22:09:13 +00:00
|
|
|
|
2018-10-02 12:13:51 +00:00
|
|
|
var displayTexture = scene.sys.textures.get(textureKey);
|
|
|
|
var displayFrame = displayTexture.get(frameKey);
|
|
|
|
|
|
|
|
if (!width || !height)
|
|
|
|
{
|
|
|
|
width = displayFrame.width;
|
|
|
|
height = displayFrame.height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
width = Math.floor(width);
|
|
|
|
height = Math.floor(height);
|
|
|
|
}
|
|
|
|
|
2018-02-06 22:56:27 +00:00
|
|
|
/**
|
2018-08-03 18:14:06 +00:00
|
|
|
* Internal tile position vector.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
2018-08-03 18:14:06 +00:00
|
|
|
* @name Phaser.GameObjects.TileSprite#_tilePosition
|
|
|
|
* @type {Phaser.Math.Vector2}
|
|
|
|
* @private
|
|
|
|
* @since 3.12.0
|
2018-02-06 22:56:27 +00:00
|
|
|
*/
|
2018-08-03 18:14:06 +00:00
|
|
|
this._tilePosition = new Vector2();
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-03 18:14:06 +00:00
|
|
|
* Internal tile scale vector.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
2018-08-03 18:14:06 +00:00
|
|
|
* @name Phaser.GameObjects.TileSprite#_tileScale
|
|
|
|
* @type {Phaser.Math.Vector2}
|
|
|
|
* @private
|
|
|
|
* @since 3.12.0
|
|
|
|
*/
|
|
|
|
this._tileScale = new Vector2(1, 1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the Tile Sprite has changed in some way, requiring an re-render of its tile texture.
|
|
|
|
*
|
|
|
|
* Such changes include the texture frame and scroll position of the Tile Sprite.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#dirty
|
|
|
|
* @type {boolean}
|
|
|
|
* @default false
|
2018-02-06 22:56:27 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-08-03 18:14:06 +00:00
|
|
|
this.dirty = false;
|
2018-02-06 22:56:27 +00:00
|
|
|
|
2018-06-28 11:59:27 +00:00
|
|
|
/**
|
2018-08-03 18:14:06 +00:00
|
|
|
* The renderer in use by this Tile Sprite.
|
2018-06-28 11:59:27 +00:00
|
|
|
*
|
2018-08-03 18:14:06 +00:00
|
|
|
* @name Phaser.GameObjects.TileSprite#renderer
|
|
|
|
* @type {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)}
|
|
|
|
* @since 3.0.0
|
2018-06-28 11:59:27 +00:00
|
|
|
*/
|
2018-08-03 18:14:06 +00:00
|
|
|
this.renderer = renderer;
|
2018-06-28 11:59:27 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-03 18:14:06 +00:00
|
|
|
* The Canvas element that the TileSprite renders its fill pattern in to.
|
|
|
|
* Only used in Canvas mode.
|
2018-06-28 11:59:27 +00:00
|
|
|
*
|
2018-08-03 18:14:06 +00:00
|
|
|
* @name Phaser.GameObjects.TileSprite#canvas
|
|
|
|
* @type {?HTMLCanvasElement}
|
|
|
|
* @since 3.12.0
|
2018-06-28 11:59:27 +00:00
|
|
|
*/
|
2018-08-03 18:14:06 +00:00
|
|
|
this.canvas = CanvasPool.create(this, width, height);
|
2018-06-28 11:59:27 +00:00
|
|
|
|
2018-02-06 22:56:27 +00:00
|
|
|
/**
|
2018-08-03 18:14:06 +00:00
|
|
|
* The Context of the Canvas element that the TileSprite renders its fill pattern in to.
|
|
|
|
* Only used in Canvas mode.
|
2018-06-08 16:07:24 +00:00
|
|
|
*
|
2018-08-03 18:14:06 +00:00
|
|
|
* @name Phaser.GameObjects.TileSprite#context
|
|
|
|
* @type {CanvasRenderingContext2D}
|
|
|
|
* @since 3.12.0
|
|
|
|
*/
|
|
|
|
this.context = this.canvas.getContext('2d');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Texture the TileSprite is using as its fill pattern.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
2018-08-03 18:14:06 +00:00
|
|
|
* @name Phaser.GameObjects.TileSprite#displayTexture
|
|
|
|
* @type {Phaser.Textures.Texture|Phaser.Textures.CanvasTexture}
|
|
|
|
* @private
|
|
|
|
* @since 3.12.0
|
2018-02-06 22:56:27 +00:00
|
|
|
*/
|
2018-10-02 12:13:51 +00:00
|
|
|
this.displayTexture = displayTexture;
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-03 18:14:06 +00:00
|
|
|
* The Frame the TileSprite is using as its fill pattern.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
2018-08-03 18:14:06 +00:00
|
|
|
* @name Phaser.GameObjects.TileSprite#displayFrame
|
|
|
|
* @type {Phaser.Textures.Frame}
|
|
|
|
* @private
|
|
|
|
* @since 3.12.0
|
2018-02-06 22:56:27 +00:00
|
|
|
*/
|
2018-10-02 12:13:51 +00:00
|
|
|
this.displayFrame = displayFrame;
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-03 18:14:06 +00:00
|
|
|
* The internal crop data object, as used by `setCrop` and passed to the `Frame.setCropUVs` method.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
2018-08-03 18:14:06 +00:00
|
|
|
* @name Phaser.GameObjects.TileSprite#_crop
|
|
|
|
* @type {object}
|
|
|
|
* @private
|
|
|
|
* @since 3.12.0
|
2018-02-06 22:56:27 +00:00
|
|
|
*/
|
2018-08-03 18:14:06 +00:00
|
|
|
this._crop = this.resetCropObject();
|
|
|
|
|
2018-09-18 10:18:00 +00:00
|
|
|
/**
|
|
|
|
* The Texture this Game Object is using to render with.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#texture
|
|
|
|
* @type {Phaser.Textures.Texture|Phaser.Textures.CanvasTexture}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-08-03 18:14:06 +00:00
|
|
|
this.texture = scene.sys.textures.addCanvas(null, this.canvas, true);
|
|
|
|
|
2018-09-18 10:18:00 +00:00
|
|
|
/**
|
|
|
|
* The Texture Frame this Game Object is using to render with.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#frame
|
|
|
|
* @type {Phaser.Textures.Frame}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-08-03 18:14:06 +00:00
|
|
|
this.frame = this.texture.get();
|
2017-04-25 22:09:13 +00:00
|
|
|
|
2018-02-06 22:56:27 +00:00
|
|
|
/**
|
2018-08-03 18:14:06 +00:00
|
|
|
* The next power of two value from the width of the Fill Pattern frame.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#potWidth
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-10-02 12:13:51 +00:00
|
|
|
this.potWidth = GetPowerOfTwo(displayFrame.width);
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-03 18:14:06 +00:00
|
|
|
* The next power of two value from the height of the Fill Pattern frame.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#potHeight
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-10-02 12:13:51 +00:00
|
|
|
this.potHeight = GetPowerOfTwo(displayFrame.height);
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-03 18:14:06 +00:00
|
|
|
* The Canvas that the TileSprites texture is rendered to.
|
|
|
|
* This is used to create a WebGL texture from.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
2018-08-03 18:14:06 +00:00
|
|
|
* @name Phaser.GameObjects.TileSprite#fillCanvas
|
2018-02-06 22:56:27 +00:00
|
|
|
* @type {HTMLCanvasElement}
|
2018-08-03 18:14:06 +00:00
|
|
|
* @since 3.12.0
|
2018-02-06 22:56:27 +00:00
|
|
|
*/
|
2018-08-03 18:14:06 +00:00
|
|
|
this.fillCanvas = CanvasPool.create2D(this, this.potWidth, this.potHeight);
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-03 18:14:06 +00:00
|
|
|
* The Canvas Context used to render the TileSprites texture.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
2018-08-03 18:14:06 +00:00
|
|
|
* @name Phaser.GameObjects.TileSprite#fillContext
|
2018-02-06 22:56:27 +00:00
|
|
|
* @type {CanvasRenderingContext2D}
|
2018-08-03 18:14:06 +00:00
|
|
|
* @since 3.12.0
|
2018-02-06 22:56:27 +00:00
|
|
|
*/
|
2018-08-03 18:14:06 +00:00
|
|
|
this.fillContext = this.fillCanvas.getContext('2d');
|
2017-05-02 20:57:21 +00:00
|
|
|
|
2018-06-28 11:59:27 +00:00
|
|
|
/**
|
2018-08-03 18:14:06 +00:00
|
|
|
* The texture that the Tile Sprite is rendered to, which is then rendered to a Scene.
|
|
|
|
* In WebGL this is a WebGLTexture. In Canvas it's a Canvas Fill Pattern.
|
2018-06-28 11:59:27 +00:00
|
|
|
*
|
2018-08-03 18:14:06 +00:00
|
|
|
* @name Phaser.GameObjects.TileSprite#fillPattern
|
|
|
|
* @type {?(WebGLTexture|CanvasPattern)}
|
|
|
|
* @since 3.12.0
|
2018-06-28 11:59:27 +00:00
|
|
|
*/
|
2018-08-03 18:14:06 +00:00
|
|
|
this.fillPattern = null;
|
|
|
|
|
2018-09-18 10:18:00 +00:00
|
|
|
this.setPosition(x, y);
|
|
|
|
this.setSize(width, height);
|
2018-10-05 15:27:42 +00:00
|
|
|
this.setFrame(frameKey);
|
2018-09-18 10:18:00 +00:00
|
|
|
this.setOriginFromFrame();
|
|
|
|
this.initPipeline();
|
2017-09-13 20:54:32 +00:00
|
|
|
|
2018-03-12 13:37:13 +00:00
|
|
|
if (scene.sys.game.config.renderType === CONST.WEBGL)
|
2018-02-16 18:17:51 +00:00
|
|
|
{
|
2018-03-12 13:37:13 +00:00
|
|
|
scene.sys.game.renderer.onContextRestored(function (renderer)
|
|
|
|
{
|
|
|
|
var gl = renderer.gl;
|
|
|
|
|
|
|
|
this.dirty = true;
|
2018-08-03 18:14:06 +00:00
|
|
|
this.fillPattern = null;
|
|
|
|
this.fillPattern = renderer.createTexture2D(0, gl.LINEAR, gl.LINEAR, gl.REPEAT, gl.REPEAT, gl.RGBA, this.fillCanvas, this.potWidth, this.potHeight);
|
2018-03-12 13:37:13 +00:00
|
|
|
}, this);
|
|
|
|
}
|
2017-04-28 18:10:32 +00:00
|
|
|
},
|
|
|
|
|
2018-08-03 18:14:06 +00:00
|
|
|
/**
|
|
|
|
* Sets the texture and frame this Game Object will use to render with.
|
|
|
|
*
|
|
|
|
* Textures are referenced by their string-based keys, as stored in the Texture Manager.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.TileSprite#setTexture
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} key - The key of the texture to be used, as stored in the Texture Manager.
|
|
|
|
* @param {(string|integer)} [frame] - The name or index of the frame within the Texture.
|
|
|
|
*
|
|
|
|
* @return {this} This Game Object instance.
|
|
|
|
*/
|
|
|
|
setTexture: function (key, frame)
|
|
|
|
{
|
|
|
|
this.displayTexture = this.scene.sys.textures.get(key);
|
|
|
|
|
|
|
|
return this.setFrame(frame);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the frame this Game Object will use to render with.
|
|
|
|
*
|
|
|
|
* The Frame has to belong to the current Texture being used.
|
|
|
|
*
|
|
|
|
* It can be either a string or an index.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.TileSprite#setFrame
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {(string|integer)} frame - The name or index of the frame within the Texture.
|
|
|
|
*
|
|
|
|
* @return {this} This Game Object instance.
|
|
|
|
*/
|
2018-10-05 15:27:42 +00:00
|
|
|
setFrame: function (frame)
|
2018-08-03 18:14:06 +00:00
|
|
|
{
|
|
|
|
this.displayFrame = this.displayTexture.get(frame);
|
|
|
|
|
|
|
|
if (!this.displayFrame.cutWidth || !this.displayFrame.cutHeight)
|
|
|
|
{
|
|
|
|
this.renderFlags &= ~_FLAG;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.renderFlags |= _FLAG;
|
|
|
|
}
|
|
|
|
|
2018-09-18 10:18:00 +00:00
|
|
|
this.dirty = true;
|
|
|
|
|
2018-08-03 18:14:06 +00:00
|
|
|
this.updateTileTexture();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-03-10 19:41:22 +00:00
|
|
|
/**
|
|
|
|
* Sets {@link Phaser.GameObjects.TileSprite#tilePositionX} and {@link Phaser.GameObjects.TileSprite#tilePositionY}.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.TileSprite#setTilePosition
|
2018-03-12 14:23:20 +00:00
|
|
|
* @since 3.3.0
|
2018-03-10 19:41:22 +00:00
|
|
|
*
|
|
|
|
* @param {number} [x] - The x position of this sprite's tiling texture.
|
|
|
|
* @param {number} [y] - The y position of this sprite's tiling texture.
|
|
|
|
*
|
2018-08-03 18:14:06 +00:00
|
|
|
* @return {this} This Tile Sprite instance.
|
2018-03-10 19:41:22 +00:00
|
|
|
*/
|
|
|
|
setTilePosition: function (x, y)
|
|
|
|
{
|
|
|
|
if (x !== undefined)
|
|
|
|
{
|
|
|
|
this.tilePositionX = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y !== undefined)
|
|
|
|
{
|
|
|
|
this.tilePositionY = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-08-03 18:14:06 +00:00
|
|
|
/**
|
|
|
|
* Sets {@link Phaser.GameObjects.TileSprite#tileScaleX} and {@link Phaser.GameObjects.TileSprite#tileScaleY}.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.TileSprite#setTileScale
|
|
|
|
* @since 3.12.0
|
|
|
|
*
|
|
|
|
* @param {number} [x] - The horizontal scale of the tiling texture.
|
|
|
|
* @param {number} [y] - The vertical scale of the tiling texture.
|
|
|
|
*
|
|
|
|
* @return {this} This Tile Sprite instance.
|
|
|
|
*/
|
|
|
|
setTileScale: function (x, y)
|
|
|
|
{
|
|
|
|
if (x !== undefined)
|
|
|
|
{
|
2018-09-04 19:19:42 +00:00
|
|
|
this.tileScaleX = x;
|
2018-08-03 18:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (y !== undefined)
|
|
|
|
{
|
2018-09-04 19:19:42 +00:00
|
|
|
this.tileScaleY = y;
|
2018-08-03 18:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-06 22:56:27 +00:00
|
|
|
/**
|
2018-06-08 16:07:24 +00:00
|
|
|
* Render the tile texture if it is dirty, or if the frame has changed.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.TileSprite#updateTileTexture
|
2018-08-03 18:14:06 +00:00
|
|
|
* @private
|
2018-02-06 22:56:27 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-02 16:08:22 +00:00
|
|
|
updateTileTexture: function ()
|
2017-04-28 18:10:32 +00:00
|
|
|
{
|
2018-08-03 18:14:06 +00:00
|
|
|
if (!this.dirty)
|
2017-06-02 16:08:22 +00:00
|
|
|
{
|
2017-05-02 20:57:21 +00:00
|
|
|
return;
|
2017-06-02 16:08:22 +00:00
|
|
|
}
|
2017-05-02 20:57:21 +00:00
|
|
|
|
2018-08-03 18:14:06 +00:00
|
|
|
// Draw the displayTexture to our fillCanvas
|
|
|
|
|
|
|
|
var frame = this.displayFrame;
|
2018-02-28 20:04:57 +00:00
|
|
|
|
2018-08-03 18:14:06 +00:00
|
|
|
var ctx = this.fillContext;
|
|
|
|
var canvas = this.fillCanvas;
|
2018-03-05 01:45:28 +00:00
|
|
|
|
2018-06-28 11:59:27 +00:00
|
|
|
var fw = this.potWidth;
|
|
|
|
var fh = this.potHeight;
|
|
|
|
|
|
|
|
if (!this.renderer.gl)
|
2017-05-04 00:14:14 +00:00
|
|
|
{
|
2018-06-28 11:59:27 +00:00
|
|
|
fw = frame.cutWidth;
|
|
|
|
fh = frame.cutHeight;
|
2017-05-04 00:14:14 +00:00
|
|
|
}
|
2017-06-02 16:08:22 +00:00
|
|
|
|
2018-08-03 18:14:06 +00:00
|
|
|
ctx.clearRect(0, 0, fw, fh);
|
2018-06-28 11:59:27 +00:00
|
|
|
|
|
|
|
canvas.width = fw;
|
|
|
|
canvas.height = fh;
|
|
|
|
|
|
|
|
ctx.drawImage(
|
|
|
|
frame.source.image,
|
|
|
|
frame.cutX, frame.cutY,
|
|
|
|
frame.cutWidth, frame.cutHeight,
|
|
|
|
0, 0,
|
|
|
|
fw, fh
|
|
|
|
);
|
|
|
|
|
2018-08-03 18:14:06 +00:00
|
|
|
if (this.renderer.gl)
|
|
|
|
{
|
|
|
|
this.fillPattern = this.renderer.canvasToTexture(canvas, this.fillPattern);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.fillPattern = ctx.createPattern(canvas, 'repeat');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateCanvas();
|
|
|
|
|
|
|
|
this.dirty = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw the fill pattern to the internal canvas.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.TileSprite#updateCanvas
|
|
|
|
* @private
|
|
|
|
* @since 3.12.0
|
|
|
|
*/
|
|
|
|
updateCanvas: function ()
|
|
|
|
{
|
|
|
|
var canvas = this.canvas;
|
|
|
|
|
|
|
|
if (canvas.width !== this.width || canvas.height !== this.height)
|
|
|
|
{
|
|
|
|
canvas.width = this.width;
|
|
|
|
canvas.height = this.height;
|
|
|
|
|
2018-08-20 16:06:06 +00:00
|
|
|
this.frame.setSize(this.width, this.height);
|
2018-08-03 18:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.dirty || this.renderer && this.renderer.gl)
|
|
|
|
{
|
|
|
|
this.dirty = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctx = this.context;
|
|
|
|
|
2018-10-02 12:30:45 +00:00
|
|
|
if (!this.scene.sys.game.config.antialias)
|
|
|
|
{
|
|
|
|
Smoothing.disable(ctx);
|
|
|
|
}
|
|
|
|
|
2018-08-03 18:14:06 +00:00
|
|
|
var scaleX = this._tileScale.x;
|
|
|
|
var scaleY = this._tileScale.y;
|
|
|
|
|
|
|
|
var positionX = this._tilePosition.x;
|
|
|
|
var positionY = this._tilePosition.y;
|
|
|
|
|
|
|
|
ctx.clearRect(0, 0, this.width, this.height);
|
|
|
|
|
|
|
|
ctx.save();
|
|
|
|
|
|
|
|
ctx.scale(scaleX, scaleY);
|
|
|
|
|
|
|
|
ctx.translate(-positionX, -positionY);
|
|
|
|
|
|
|
|
ctx.fillStyle = this.fillPattern;
|
|
|
|
|
|
|
|
ctx.fillRect(positionX, positionY, this.width / scaleX, this.height / scaleY);
|
|
|
|
|
|
|
|
ctx.restore();
|
2018-06-28 11:59:27 +00:00
|
|
|
|
2017-05-02 20:57:21 +00:00
|
|
|
this.dirty = false;
|
2017-05-04 00:28:49 +00:00
|
|
|
},
|
|
|
|
|
2018-02-06 22:56:27 +00:00
|
|
|
/**
|
2018-05-22 14:54:53 +00:00
|
|
|
* Internal destroy handler, called as part of the destroy process.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
2018-05-22 14:54:53 +00:00
|
|
|
* @method Phaser.GameObjects.TileSprite#preDestroy
|
|
|
|
* @protected
|
|
|
|
* @since 3.9.0
|
2018-02-06 22:56:27 +00:00
|
|
|
*/
|
2018-05-22 14:54:53 +00:00
|
|
|
preDestroy: function ()
|
2017-05-04 00:28:49 +00:00
|
|
|
{
|
2018-05-22 14:54:53 +00:00
|
|
|
if (this.renderer && this.renderer.gl)
|
2017-05-04 00:28:49 +00:00
|
|
|
{
|
2018-08-03 18:14:06 +00:00
|
|
|
this.renderer.deleteTexture(this.fillPattern);
|
2017-05-04 00:28:49 +00:00
|
|
|
}
|
2017-06-02 16:08:22 +00:00
|
|
|
|
2018-08-03 18:14:06 +00:00
|
|
|
CanvasPool.remove(this.canvas);
|
|
|
|
CanvasPool.remove(this.fillCanvas);
|
|
|
|
|
|
|
|
this.fillPattern = null;
|
|
|
|
this.fillContext = null;
|
|
|
|
this.fillCanvas = null;
|
|
|
|
|
|
|
|
this.displayTexture = null;
|
|
|
|
this.displayFrame = null;
|
2017-06-02 16:08:22 +00:00
|
|
|
|
2018-08-03 18:14:06 +00:00
|
|
|
this.texture.destroy();
|
2017-06-02 16:08:22 +00:00
|
|
|
|
2017-05-04 00:28:49 +00:00
|
|
|
this.renderer = null;
|
2018-08-03 18:14:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The horizontal scroll position of the Tile Sprite.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#tilePositionX
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
tilePositionX: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._tilePosition.x;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._tilePosition.x = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The vertical scroll position of the Tile Sprite.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#tilePositionY
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
tilePositionY: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._tilePosition.y;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._tilePosition.y = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The horizontal scale of the Tile Sprite texture.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#tileScaleX
|
|
|
|
* @type {number}
|
|
|
|
* @default 1
|
|
|
|
* @since 3.11.0
|
|
|
|
*/
|
|
|
|
tileScaleX: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._tileScale.x;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._tileScale.x = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The vertical scale of the Tile Sprite texture.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#tileScaleY
|
|
|
|
* @type {number}
|
|
|
|
* @default 1
|
|
|
|
* @since 3.11.0
|
|
|
|
*/
|
|
|
|
tileScaleY: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this._tileScale.y;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._tileScale.y = value;
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
2017-04-25 22:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = TileSprite;
|