2017-04-25 22:09:13 +00:00
|
|
|
|
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var GameObject = require('../GameObject');
|
|
|
|
var Components = require('../../components');
|
|
|
|
var TileSpriteRender = require('./TileSpriteRender');
|
2017-05-08 21:03:18 +00:00
|
|
|
var CanvasPool = require('../../dom/CanvasPool');
|
2017-04-25 22:09:13 +00:00
|
|
|
|
|
|
|
var TileSprite = new Class({
|
|
|
|
|
|
|
|
Extends: GameObject,
|
|
|
|
|
|
|
|
Mixins: [
|
|
|
|
Components.Alpha,
|
|
|
|
Components.BlendMode,
|
|
|
|
Components.Flip,
|
|
|
|
Components.GetBounds,
|
|
|
|
Components.Origin,
|
|
|
|
Components.RenderTarget,
|
|
|
|
Components.ScaleMode,
|
|
|
|
Components.Size,
|
|
|
|
Components.Texture,
|
|
|
|
Components.Transform,
|
|
|
|
Components.Visible,
|
|
|
|
TileSpriteRender
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-04-28 18:10:32 +00:00
|
|
|
function TileSprite (state, x, y, width, height, texture, frame)
|
2017-04-25 22:09:13 +00:00
|
|
|
{
|
2017-04-28 18:10:32 +00:00
|
|
|
var resourceManager = state.game.renderer.resourceManager;
|
|
|
|
|
2017-04-25 22:09:13 +00:00
|
|
|
GameObject.call(this, state, 'TileSprite');
|
|
|
|
|
|
|
|
this.tilePositionX = 0;
|
|
|
|
this.tilePositionY = 0;
|
2017-05-02 20:57:21 +00:00
|
|
|
this.dirty = true;
|
|
|
|
this.tileTexture = null;
|
|
|
|
this.renderer = null;
|
2017-04-25 22:09:13 +00:00
|
|
|
|
|
|
|
this.setTexture(texture, frame);
|
|
|
|
this.setPosition(x, y);
|
|
|
|
this.setSizeToFrame();
|
|
|
|
this.setOrigin();
|
2017-05-02 20:57:21 +00:00
|
|
|
this.setSize(width, height);
|
|
|
|
|
|
|
|
this.potWidth = this.frame.width;
|
2017-06-02 16:08:22 +00:00
|
|
|
this.potHeight = this.frame.height;
|
2017-05-04 00:14:14 +00:00
|
|
|
this.canvasPattern = null;
|
2017-05-02 20:57:21 +00:00
|
|
|
|
2017-06-02 16:08:22 +00:00
|
|
|
if (resourceManager)
|
2017-05-02 20:57:21 +00:00
|
|
|
{
|
|
|
|
this.potWidth--;
|
|
|
|
this.potWidth |= this.potWidth >> 1;
|
|
|
|
this.potWidth |= this.potWidth >> 2;
|
|
|
|
this.potWidth |= this.potWidth >> 4;
|
|
|
|
this.potWidth |= this.potWidth >> 8;
|
|
|
|
this.potWidth |= this.potWidth >> 16;
|
|
|
|
this.potWidth++;
|
|
|
|
|
|
|
|
this.potHeight--;
|
|
|
|
this.potHeight |= this.potHeight >> 1;
|
|
|
|
this.potHeight |= this.potHeight >> 2;
|
|
|
|
this.potHeight |= this.potHeight >> 4;
|
|
|
|
this.potHeight |= this.potHeight >> 8;
|
|
|
|
this.potHeight |= this.potHeight >> 16;
|
|
|
|
this.potHeight++;
|
|
|
|
|
|
|
|
this.renderer = state.game.renderer;
|
2017-06-02 16:08:22 +00:00
|
|
|
var gl = state.game.renderer.gl;
|
2017-04-28 18:10:32 +00:00
|
|
|
|
2017-05-02 20:57:21 +00:00
|
|
|
this.tileTexture = resourceManager.createTexture(0, gl.LINEAR, gl.LINEAR, gl.REPEAT, gl.REPEAT, gl.RGBA, this.canvasBuffer, this.potWidth, this.potHeight);
|
2017-06-02 16:08:22 +00:00
|
|
|
}
|
2017-05-02 20:57:21 +00:00
|
|
|
|
|
|
|
this.canvasBuffer = CanvasPool.create2D(null, this.potWidth, this.potHeight);
|
|
|
|
this.canvasBufferCtx = this.canvasBuffer.getContext('2d');
|
|
|
|
|
|
|
|
this.updateTileTexture();
|
2017-04-28 18:10:32 +00:00
|
|
|
},
|
|
|
|
|
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
|
|
|
|
2017-05-04 00:14:14 +00:00
|
|
|
this.canvasBuffer.width = this.canvasBuffer.width;
|
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
|
|
|
|
2017-06-02 16:08:22 +00:00
|
|
|
if (this.renderer)
|
2017-05-02 20:57:21 +00:00
|
|
|
{
|
|
|
|
this.renderer.uploadCanvasToGPU(this.canvasBuffer, this.tileTexture, true);
|
|
|
|
}
|
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
|
|
|
},
|
|
|
|
|
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
|
|
|
{
|
|
|
|
this.renderer.gl.deleteTexture(this.tileTexture);
|
|
|
|
}
|
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;
|