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');
|
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-02-06 22:56:27 +00:00
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class TileSprite
|
|
|
|
* @extends Phaser.GameObjects.GameObject
|
|
|
|
* @memberOf Phaser.GameObjects
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @extends Phaser.GameObjects.Components.Alpha
|
|
|
|
* @extends Phaser.GameObjects.Components.BlendMode
|
|
|
|
* @extends Phaser.GameObjects.Components.Depth
|
|
|
|
* @extends Phaser.GameObjects.Components.Flip
|
|
|
|
* @extends Phaser.GameObjects.Components.GetBounds
|
|
|
|
* @extends Phaser.GameObjects.Components.Origin
|
|
|
|
* @extends Phaser.GameObjects.Components.Pipeline
|
|
|
|
* @extends Phaser.GameObjects.Components.ScaleMode
|
|
|
|
* @extends Phaser.GameObjects.Components.ScrollFactor
|
|
|
|
* @extends Phaser.GameObjects.Components.Size
|
|
|
|
* @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.
|
|
|
|
* @param {string|integer} [frame] - An optional frame from the Texture this Game Object is rendering with.
|
|
|
|
*/
|
2017-04-25 22:09:13 +00:00
|
|
|
var TileSprite = new Class({
|
|
|
|
|
|
|
|
Extends: GameObject,
|
|
|
|
|
|
|
|
Mixins: [
|
|
|
|
Components.Alpha,
|
|
|
|
Components.BlendMode,
|
2018-02-01 00:50:15 +00:00
|
|
|
Components.Depth,
|
2017-04-25 22:09:13 +00:00
|
|
|
Components.Flip,
|
|
|
|
Components.GetBounds,
|
|
|
|
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.Size,
|
|
|
|
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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#tileTexture
|
|
|
|
* @type {?[type]}
|
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#renderer
|
|
|
|
* @type {[type]}
|
|
|
|
* @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
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#canvasPattern
|
|
|
|
* @type {?CanvasPattern}
|
|
|
|
* @default null
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-05-04 00:14:14 +00:00
|
|
|
this.canvasPattern = null;
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.TileSprite#canvasBuffer
|
|
|
|
* @type {HTMLCanvasElement}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-05-02 20:57:21 +00:00
|
|
|
this.canvasBuffer = CanvasPool.create2D(null, this.potWidth, this.potHeight);
|
2018-02-06 22:56:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @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');
|
|
|
|
|
|
|
|
this.updateTileTexture();
|
2017-09-13 20:54:32 +00:00
|
|
|
|
2018-02-16 18:17:51 +00:00
|
|
|
scene.sys.game.renderer.onContextRestored(function (renderer)
|
|
|
|
{
|
2018-02-16 18:54:33 +00:00
|
|
|
var gl = renderer.gl;
|
2018-01-25 00:15:51 +00:00
|
|
|
this.tileTexture = null;
|
|
|
|
this.dirty = true;
|
|
|
|
this.tileTexture = renderer.createTexture2D(0, gl.LINEAR, gl.LINEAR, gl.REPEAT, gl.REPEAT, gl.RGBA, this.canvasBuffer, this.potWidth, this.potHeight);
|
|
|
|
}, this);
|
2017-04-28 18:10:32 +00:00
|
|
|
},
|
|
|
|
|
2018-02-06 22:56:27 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @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
|
|
|
{
|
2017-05-02 20:57:21 +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
|
|
|
|
|
|
|
this.canvasBufferCtx.drawImage(
|
2017-06-02 16:08:22 +00:00
|
|
|
this.frame.source.image,
|
2017-05-02 20:57:21 +00:00
|
|
|
this.frame.cutX, this.frame.cutY,
|
|
|
|
this.frame.cutWidth, this.frame.cutHeight,
|
|
|
|
0, 0,
|
|
|
|
this.potWidth, this.potHeight
|
|
|
|
);
|
2017-04-28 18:10:32 +00:00
|
|
|
|
2018-01-25 00:15:51 +00:00
|
|
|
if (this.renderer.gl)
|
2017-05-02 20:57:21 +00:00
|
|
|
{
|
2018-01-25 00:15:51 +00:00
|
|
|
this.tileTexture = this.renderer.canvasToTexture(this.canvasBuffer, this.tileTexture, (this.tileTexture === null), this.scaleMode);
|
2017-05-02 20:57:21 +00:00
|
|
|
}
|
2017-05-04 00:14:14 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
this.canvasPattern = this.canvasBufferCtx.createPattern(this.canvasBuffer, 'repeat');
|
|
|
|
}
|
2017-06-02 16:08:22 +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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.TileSprite#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-02 16:08:22 +00:00
|
|
|
destroy: function ()
|
2017-05-04 00:28:49 +00:00
|
|
|
{
|
2017-06-02 16:08:22 +00:00
|
|
|
if (this.renderer)
|
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
|
|
|
|
2017-05-04 00:28:49 +00:00
|
|
|
this.canvasPattern = null;
|
|
|
|
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;
|
|
|
|
this.visible = false;
|
2017-04-25 22:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = TileSprite;
|