phaser/v3/src/gameobjects/blitter/Blitter.js

135 lines
3.9 KiB
JavaScript
Raw Normal View History

/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var CONST = require('../../const');
2017-01-24 12:55:45 +00:00
var Bob = require('./Bob');
var GameObject = require('../GameObject');
2017-01-24 12:55:45 +00:00
var Children = require('../../components/Children');
/**
2017-01-24 12:55:45 +00:00
* A Blitter Game Object.
*
2017-01-24 12:55:45 +00:00
* The Blitter Game Object is a special type of Container, that contains Blitter.Bob objects.
* These objects can be thought of as just texture frames with a position and nothing more.
2017-01-24 12:55:45 +00:00
* Bobs don't have any update methods, or the ability to have children, or any kind of special effects.
* They are essentially just super-fast texture frame renderers, and the Blitter object creates and manages them.
2017-01-24 12:55:45 +00:00
*
* @class Blitter
* @extends Phaser.GameObject
* @constructor
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {number} [x=0] - The x coordinate of the Image. The coordinate is relative to any parent container this Image may be in.
* @param {number} [y=0] - The y coordinate of the Image. The coordinate is relative to any parent container this Image may be in.
* @param {string} [key] - The texture used by the Image during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture, BitmapData or PIXI.Texture.
* @param {string|number} [frame] - If this Image is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
*/
var Blitter = function (state, x, y, key, frame)
{
var _texture = state.sys.textures.get(key);
var _frame = _texture.get(frame);
GameObject.call(this, state, x, y, _texture, _frame);
this.type = CONST.BLITTER;
2017-01-24 12:55:45 +00:00
this.children = new Children(this);
this.renderList = [];
this.dirty = false;
};
Blitter.prototype = Object.create(GameObject.prototype);
Blitter.prototype.constructor = Blitter;
Blitter.prototype.renderCanvas = require('./BlitterCanvasRenderer');
Blitter.prototype.renderWebGL = require('./BlitterWebGLRenderer');
2017-01-24 12:55:45 +00:00
// frame MUST be part of the Blitter texture
Blitter.prototype.create = function (x, y, frame, visible, index)
{
if (frame === undefined) { frame = this.frame; }
if (visible === undefined) { visible = true; }
2017-02-03 16:11:31 +00:00
if (index === undefined) { index = this.children.length; }
if (typeof frame === 'string')
{
frame = this.texture.get(frame);
}
2017-01-24 12:55:45 +00:00
var bob = new Bob(this, x, y, frame, visible);
2017-01-24 12:55:45 +00:00
this.children.addAt(bob, index, false);
this.dirty = true;
2017-01-24 12:55:45 +00:00
return bob;
};
2017-01-24 12:55:45 +00:00
// frame MUST be part of the Blitter texture
Blitter.prototype.createFromCallback = function (callback, quantity, frame, visible)
{
2017-01-24 13:25:19 +00:00
var bobs = this.createMultiple(quantity, frame, visible);
2017-01-24 12:55:45 +00:00
for (var i = 0; i < bobs.length; i++)
{
var bob = bobs[i];
2017-01-24 12:55:45 +00:00
callback.call(this, bob, i);
}
2017-01-24 12:55:45 +00:00
return bobs;
};
2017-01-24 12:55:45 +00:00
// frame MUST be part of the Blitter texture
Blitter.prototype.createMultiple = function (quantity, frame, visible)
{
2017-01-24 13:25:19 +00:00
if (frame === undefined) { frame = this.frame; }
2017-01-24 12:55:45 +00:00
if (visible === undefined) { visible = true; }
2017-01-24 12:55:45 +00:00
if (!Array.isArray(frame))
{
frame = [ frame ];
}
2017-01-24 12:55:45 +00:00
var bobs = [];
var _this = this;
frame.forEach(function (singleFrame)
{
for (var i = 0; i < quantity; i++)
{
2017-01-24 12:55:45 +00:00
bobs.push(_this.create(0, 0, singleFrame, visible));
}
2017-01-24 12:55:45 +00:00
});
2017-01-24 12:55:45 +00:00
return bobs;
};
Blitter.prototype.childCanRender = function (child)
{
return (child.visible && child.alpha > 0);
};
Blitter.prototype.getRenderList = function ()
{
if (this.dirty)
{
this.renderList = this.children.list.filter(this.childCanRender, this);
this.dirty = false;
}
return this.renderList;
};
Blitter.prototype.clear = function ()
{
this.children.removeAll();
this.dirty = true;
};
module.exports = Blitter;