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');
|
2017-04-25 22:09:13 +00:00
|
|
|
var TileSpriteRender = require('./TileSpriteRender');
|
|
|
|
|
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
|
|
|
|
* @memberOf Phaser.GameObjects
|
|
|
|
* @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-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.Texture
|
|
|
|
* @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.
|
|
|
|
* @param {number} width - The width of the Game Object.
|
|
|
|
* @param {number} height - The height of the Game Object.
|
|
|
|
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
|
2018-03-20 14:56:31 +00:00
|
|
|
* @param {(string|integer)} [frame] - 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-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,
|
2017-04-25 22:09:13 +00:00
|
|
|
Components.Texture,
|
2017-07-04 11:36:19 +00:00
|
|
|
Components.Tint,
|
2017-04-25 22:09:13 +00:00
|
|
|
Components.Transform,
|
|
|
|
Components.Visible,
|
|
|
|
TileSpriteRender
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
function TileSprite (scene, x, y, width, height, texture, frame)
|
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-02-06 22:56:27 +00:00
|
|
|
/**
|
2018-06-08 16:07:24 +00:00
|
|
|
* The horizontal scroll position of the Tile Sprite.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#tilePositionX
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-04-25 22:09:13 +00:00
|
|
|
this.tilePositionX = 0;
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-08 16:07:24 +00:00
|
|
|
* The vertical scroll position of the Tile Sprite.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#tilePositionY
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-04-25 22:09:13 +00:00
|
|
|
this.tilePositionY = 0;
|
2018-02-06 22:56:27 +00:00
|
|
|
|
2018-06-28 11:59:27 +00:00
|
|
|
/**
|
|
|
|
* The horizontal scale of the Tile Sprite texture.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#tileScaleX
|
|
|
|
* @type {number}
|
|
|
|
* @default 1
|
|
|
|
* @since 3.11.0
|
|
|
|
*/
|
|
|
|
this.tileScaleX = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The vertical scale of the Tile Sprite texture.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#tileScaleY
|
|
|
|
* @type {number}
|
|
|
|
* @default 1
|
|
|
|
* @since 3.11.0
|
|
|
|
*/
|
|
|
|
this.tileScaleY = 1;
|
|
|
|
|
2018-02-06 22:56:27 +00:00
|
|
|
/**
|
2018-06-08 16:07:24 +00:00
|
|
|
* 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.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#dirty
|
|
|
|
* @type {boolean}
|
|
|
|
* @default true
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-05-02 20:57:21 +00:00
|
|
|
this.dirty = true;
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-08 16:07:24 +00:00
|
|
|
* The texture that the Tile Sprite is rendered to, which is then rendered to a Scene.
|
2018-06-28 11:59:27 +00:00
|
|
|
* In WebGL this is a WebGLTexture. In Canvas it's a Canvas Fill Pattern.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#tileTexture
|
2018-06-28 11:59:27 +00:00
|
|
|
* @type {?(WebGLTexture|CanvasPattern)}
|
2018-02-06 22:56:27 +00:00
|
|
|
* @default null
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-05-02 20:57:21 +00:00
|
|
|
this.tileTexture = null;
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-08 16:07:24 +00:00
|
|
|
* The renderer in use by this Tile Sprite.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#renderer
|
2018-03-20 14:56:31 +00:00
|
|
|
* @type {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)}
|
2018-02-06 22:56:27 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-25 00:15:51 +00:00
|
|
|
this.renderer = renderer;
|
2017-04-25 22:09:13 +00:00
|
|
|
|
|
|
|
this.setTexture(texture, frame);
|
|
|
|
this.setPosition(x, y);
|
2017-05-02 20:57:21 +00:00
|
|
|
this.setSize(width, height);
|
2018-02-09 15:23:26 +00:00
|
|
|
this.setOriginFromFrame();
|
2018-01-29 21:46:48 +00:00
|
|
|
this.initPipeline('TextureTintPipeline');
|
2017-05-02 20:57:21 +00:00
|
|
|
|
2018-02-06 22:56:27 +00:00
|
|
|
/**
|
|
|
|
* The next power of two value from the width of the Frame.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#potWidth
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-08-17 01:05:41 +00:00
|
|
|
this.potWidth = GetPowerOfTwo(this.frame.width);
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The next power of two value from the height of the Frame.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#potHeight
|
|
|
|
* @type {integer}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-08-17 01:05:41 +00:00
|
|
|
this.potHeight = GetPowerOfTwo(this.frame.height);
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-20 07:33:55 +00:00
|
|
|
* The Canvas Pattern used to repeat the TileSprite's texture.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#canvasPattern
|
|
|
|
* @type {?CanvasPattern}
|
|
|
|
* @default null
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-06-28 11:59:27 +00:00
|
|
|
// this.canvasPattern = null;
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-20 07:33:55 +00:00
|
|
|
* The Canvas that the TileSprite's texture is rendered to.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#canvasBuffer
|
|
|
|
* @type {HTMLCanvasElement}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-04-13 15:48:34 +00:00
|
|
|
this.canvasBuffer = CanvasPool.create2D(this, this.potWidth, this.potHeight);
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-20 07:33:55 +00:00
|
|
|
* The Canvas Context used to render the TileSprite's texture.
|
2018-02-06 22:56:27 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#canvasBufferCtx
|
|
|
|
* @type {CanvasRenderingContext2D}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-05-02 20:57:21 +00:00
|
|
|
this.canvasBufferCtx = this.canvasBuffer.getContext('2d');
|
|
|
|
|
2018-06-28 11:59:27 +00:00
|
|
|
/**
|
|
|
|
* The previous Texture Frame being used.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Components.Texture#oldFrame
|
|
|
|
* @type {Phaser.Textures.Frame}
|
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-02-28 20:04:57 +00:00
|
|
|
this.oldFrame = null;
|
|
|
|
|
2017-05-02 20:57:21 +00:00
|
|
|
this.updateTileTexture();
|
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.tileTexture = null;
|
|
|
|
this.dirty = true;
|
2018-03-12 14:52:35 +00:00
|
|
|
this.tileTexture = renderer.createTexture2D(0, gl.LINEAR, gl.LINEAR, gl.REPEAT, gl.REPEAT, gl.RGBA, this.canvasBuffer, this.potWidth, this.potHeight);
|
2018-03-12 13:37:13 +00:00
|
|
|
}, this);
|
|
|
|
}
|
2017-04-28 18:10:32 +00:00
|
|
|
},
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.TileSprite} This Tile Sprite instance.
|
|
|
|
*/
|
|
|
|
setTilePosition: function (x, y)
|
|
|
|
{
|
|
|
|
if (x !== undefined)
|
|
|
|
{
|
|
|
|
this.tilePositionX = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y !== undefined)
|
|
|
|
{
|
|
|
|
this.tilePositionY = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-02 16:08:22 +00:00
|
|
|
updateTileTexture: function ()
|
2017-04-28 18:10:32 +00:00
|
|
|
{
|
2018-06-28 11:59:27 +00:00
|
|
|
var frame = this.frame;
|
|
|
|
|
|
|
|
if (!this.dirty && this.oldFrame === frame)
|
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-06-28 11:59:27 +00:00
|
|
|
this.oldFrame = frame;
|
2018-02-28 20:04:57 +00:00
|
|
|
|
2018-06-28 11:59:27 +00:00
|
|
|
var ctx = this.canvasBufferCtx;
|
|
|
|
var canvas = this.canvasBuffer;
|
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-06-28 11:59:27 +00:00
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
|
|
|
canvas.width = fw;
|
|
|
|
canvas.height = fh;
|
|
|
|
|
|
|
|
ctx.drawImage(
|
|
|
|
frame.source.image,
|
|
|
|
frame.cutX, frame.cutY,
|
|
|
|
frame.cutWidth, frame.cutHeight,
|
|
|
|
0, 0,
|
|
|
|
fw, fh
|
|
|
|
);
|
|
|
|
|
|
|
|
this.tileTexture = (this.renderer.gl) ? this.renderer.canvasToTexture(canvas, this.tileTexture) : ctx.createPattern(canvas, 'repeat');
|
|
|
|
|
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-01-25 00:15:51 +00:00
|
|
|
this.renderer.deleteTexture(this.tileTexture);
|
2017-05-04 00:28:49 +00:00
|
|
|
}
|
2017-06-02 16:08:22 +00:00
|
|
|
|
2017-05-04 00:28:49 +00:00
|
|
|
CanvasPool.remove(this.canvasBuffer);
|
2017-06-02 16:08:22 +00:00
|
|
|
|
2018-06-28 11:59:27 +00:00
|
|
|
this.tileTexture = null;
|
2017-05-04 00:28:49 +00:00
|
|
|
this.canvasBufferCtx = null;
|
|
|
|
this.canvasBuffer = null;
|
2017-06-02 16:08:22 +00:00
|
|
|
|
2017-05-04 00:28:49 +00:00
|
|
|
this.renderer = null;
|
2017-04-25 22:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = TileSprite;
|