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-02-23 03:54:54 +00:00
|
|
|
var BlitterRender = require('./BlitterRender');
|
|
|
|
var Bob = require('./Bob');
|
2017-07-04 00:59:31 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var Components = require('../components');
|
2018-01-20 17:45:01 +00:00
|
|
|
var Frame = require('../../textures/Frame');
|
2017-07-04 00:59:31 +00:00
|
|
|
var GameObject = require('../GameObject');
|
2018-02-14 12:25:17 +00:00
|
|
|
var List = require('../../structs/List');
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2018-03-19 21:27:16 +00:00
|
|
|
/**
|
2018-04-16 13:43:24 +00:00
|
|
|
* @callback Phaser.GameObjects.Blitter.BlitterFromCallback
|
2018-03-19 21:27:16 +00:00
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.Blitter} blitter - [description]
|
|
|
|
* @param {integer} index - [description]
|
|
|
|
*/
|
|
|
|
|
2018-01-31 13:54:44 +00:00
|
|
|
/**
|
2018-02-07 15:27:21 +00:00
|
|
|
* @classdesc
|
2018-01-31 13:54:44 +00:00
|
|
|
* A Blitter Game Object.
|
|
|
|
*
|
2018-02-06 14:13:30 +00:00
|
|
|
* The Blitter Game Object is a special kind of container that creates, updates and manages Bob objects.
|
|
|
|
* Bobs are designed for rendering speed rather than flexibility. They consist of a texture, or frame from a texture,
|
|
|
|
* a position and an alpha value. You cannot scale or rotate them. They use a batched drawing method for speed
|
|
|
|
* during rendering.
|
|
|
|
*
|
|
|
|
* A Blitter Game Object has one texture bound to it. Bobs created by the Blitter can use any Frame from this
|
|
|
|
* Texture to render with, but they cannot use any other Texture. It is this single texture-bind that allows
|
|
|
|
* them their speed.
|
|
|
|
*
|
|
|
|
* If you have a need to blast a large volume of frames around the screen then Blitter objects are well worth
|
|
|
|
* investigating. They are especially useful for using as a base for your own special effects systems.
|
2018-01-31 13:54:44 +00:00
|
|
|
*
|
|
|
|
* @class Blitter
|
|
|
|
* @extends Phaser.GameObjects.GameObject
|
|
|
|
* @memberOf Phaser.GameObjects
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-01 00:04:45 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.Alpha
|
2018-02-01 01:20:11 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.BlendMode
|
|
|
|
* @extends Phaser.GameObjects.Components.Depth
|
|
|
|
* @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.Transform
|
|
|
|
* @extends Phaser.GameObjects.Components.Visible
|
2018-03-19 21:27:16 +00:00
|
|
|
*
|
2018-01-31 13:54:44 +00:00
|
|
|
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. It can only belong to one Scene at any given time.
|
2018-02-05 23:59:51 +00:00
|
|
|
* @param {number} [x=0] - The x coordinate of this Game Object in world space.
|
2018-01-31 13:54:44 +00:00
|
|
|
* @param {number} [y=0] - The y coordinate of this Game Object in world space.
|
|
|
|
* @param {string} [texture='__DEFAULT'] - The key of the texture this Game Object will use for rendering. The Texture must already exist in the Texture Manager.
|
2018-03-20 14:56:31 +00:00
|
|
|
* @param {(string|integer)} [frame=0] - The Frame of the Texture that this Game Object will use. Only set if the Texture has multiple frames, such as a Texture Atlas or Sprite Sheet.
|
2018-01-31 13:54:44 +00:00
|
|
|
*/
|
2017-02-23 03:54:54 +00:00
|
|
|
var Blitter = new Class({
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2017-04-05 01:10:48 +00:00
|
|
|
Extends: GameObject,
|
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
Mixins: [
|
|
|
|
Components.Alpha,
|
|
|
|
Components.BlendMode,
|
2018-02-01 00:50:15 +00:00
|
|
|
Components.Depth,
|
2018-01-29 21:46:48 +00:00
|
|
|
Components.Pipeline,
|
2017-02-23 03:54:54 +00:00
|
|
|
Components.ScaleMode,
|
2018-02-01 01:20:11 +00:00
|
|
|
Components.ScrollFactor,
|
2017-03-02 02:06:22 +00:00
|
|
|
Components.Size,
|
|
|
|
Components.Texture,
|
|
|
|
Components.Transform,
|
2017-02-23 03:54:54 +00:00
|
|
|
Components.Visible,
|
|
|
|
BlitterRender
|
|
|
|
],
|
2017-01-24 12:55:45 +00:00
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
initialize:
|
2017-02-13 23:57:32 +00:00
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
function Blitter (scene, x, y, texture, frame)
|
2017-02-23 03:54:54 +00:00
|
|
|
{
|
2017-07-14 13:30:20 +00:00
|
|
|
GameObject.call(this, scene, 'Blitter');
|
2017-02-13 23:57:32 +00:00
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
this.setTexture(texture, frame);
|
2017-03-02 02:06:22 +00:00
|
|
|
this.setPosition(x, y);
|
2018-01-29 21:46:48 +00:00
|
|
|
this.initPipeline('TextureTintPipeline');
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2018-01-26 15:37:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-01 00:04:45 +00:00
|
|
|
* @name Phaser.GameObjects.Blitter#children
|
2018-03-29 13:53:06 +00:00
|
|
|
* @type {Phaser.Structs.List.<Phaser.GameObjects.Blitter.Bob>}
|
2018-01-26 15:37:34 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-02-14 12:25:17 +00:00
|
|
|
this.children = new List();
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2018-01-26 15:37:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-01 00:04:45 +00:00
|
|
|
* @name Phaser.GameObjects.Blitter#renderList
|
2018-03-27 12:52:58 +00:00
|
|
|
* @type {Phaser.GameObjects.Blitter.Bob[]}
|
2018-01-26 15:37:34 +00:00
|
|
|
* @default []
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-02-23 03:54:54 +00:00
|
|
|
this.renderList = [];
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
this.dirty = false;
|
|
|
|
},
|
2017-02-01 00:43:38 +00:00
|
|
|
|
2018-01-26 15:37:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Blitter#create
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} x - The x position of the Bob. Bob coordinate are relative to the position of the Blitter object.
|
|
|
|
* @param {number} y - The y position of the Bob. Bob coordinate are relative to the position of the Blitter object.
|
2018-03-20 14:56:31 +00:00
|
|
|
* @param {(string|integer|Phaser.Textures.Frame)} [frame] - The Frame the Bob will use. It _must_ be part of the Texture the parent Blitter object is using.
|
2018-01-26 15:37:34 +00:00
|
|
|
* @param {boolean} [visible=true] - Should the created Bob render or not?
|
|
|
|
* @param {integer} [index] - The position in the Blitters Display List to add the new Bob at. Defaults to the top of the list.
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.Blitter.Bob} The newly created Bob object.
|
|
|
|
*/
|
2017-02-23 03:54:54 +00:00
|
|
|
create: function (x, y, frame, visible, index)
|
2017-02-01 00:43:38 +00:00
|
|
|
{
|
2017-02-23 03:54:54 +00:00
|
|
|
if (visible === undefined) { visible = true; }
|
|
|
|
if (index === undefined) { index = this.children.length; }
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2017-08-14 12:13:27 +00:00
|
|
|
if (frame === undefined)
|
|
|
|
{
|
|
|
|
frame = this.frame;
|
|
|
|
}
|
2018-01-20 17:45:01 +00:00
|
|
|
else if (!(frame instanceof Frame))
|
2017-02-23 03:54:54 +00:00
|
|
|
{
|
|
|
|
frame = this.texture.get(frame);
|
|
|
|
}
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2017-10-02 13:50:02 +00:00
|
|
|
var bob = new Bob(this, x, y, frame, visible);
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
this.children.addAt(bob, index, false);
|
2017-02-13 23:57:32 +00:00
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
this.dirty = true;
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
return bob;
|
|
|
|
},
|
2017-01-24 12:55:45 +00:00
|
|
|
|
2018-01-26 15:37:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Blitter#createFromCallback
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-04-16 13:43:24 +00:00
|
|
|
* @param {Phaser.GameObjects.Blitter.BlitterFromCallback} callback - The callback to invoke after creating a bob. It will be sent two arguments: The Bob and the index of the Bob.
|
2018-02-01 05:48:56 +00:00
|
|
|
* @param {integer} quantity - The quantity of Bob objects to create.
|
2018-03-23 15:54:12 +00:00
|
|
|
* @param {(string|integer|Phaser.Textures.Frame|string[]|integer[]|Phaser.Textures.Frame[])} [frame] - The Frame the Bobs will use. It must be part of the Blitter Texture.
|
2018-02-01 05:48:56 +00:00
|
|
|
* @param {boolean} [visible=true] - [description]
|
2018-01-26 15:37:34 +00:00
|
|
|
*
|
2018-02-01 05:48:56 +00:00
|
|
|
* @return {Phaser.GameObjects.Blitter.Bob[]} An array of Bob objects that were created.
|
2018-01-26 15:37:34 +00:00
|
|
|
*/
|
2017-02-23 03:54:54 +00:00
|
|
|
createFromCallback: function (callback, quantity, frame, visible)
|
2017-01-24 12:55:45 +00:00
|
|
|
{
|
2017-02-23 03:54:54 +00:00
|
|
|
var bobs = this.createMultiple(quantity, frame, visible);
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
for (var i = 0; i < bobs.length; i++)
|
|
|
|
{
|
|
|
|
var bob = bobs[i];
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
callback.call(this, bob, i);
|
|
|
|
}
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
return bobs;
|
|
|
|
},
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2018-01-26 15:37:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Blitter#createMultiple
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-01 05:48:56 +00:00
|
|
|
* @param {integer} quantity - The quantity of Bob objects to create.
|
2018-03-23 15:54:12 +00:00
|
|
|
* @param {(string|integer|Phaser.Textures.Frame|string[]|integer[]|Phaser.Textures.Frame[])} [frame] - The Frame the Bobs will use. It must be part of the Blitter Texture.
|
2018-02-01 05:48:56 +00:00
|
|
|
* @param {boolean} [visible=true] - [description]
|
2018-01-26 15:37:34 +00:00
|
|
|
*
|
2018-02-01 05:48:56 +00:00
|
|
|
* @return {Phaser.GameObjects.Blitter.Bob[]} An array of Bob objects that were created.
|
2018-01-26 15:37:34 +00:00
|
|
|
*/
|
2017-02-23 03:54:54 +00:00
|
|
|
createMultiple: function (quantity, frame, visible)
|
2017-01-24 12:55:45 +00:00
|
|
|
{
|
2018-01-20 16:35:47 +00:00
|
|
|
if (frame === undefined) { frame = this.frame.name; }
|
2017-02-23 03:54:54 +00:00
|
|
|
if (visible === undefined) { visible = true; }
|
|
|
|
|
|
|
|
if (!Array.isArray(frame))
|
|
|
|
{
|
|
|
|
frame = [ frame ];
|
|
|
|
}
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
var bobs = [];
|
|
|
|
var _this = this;
|
2017-01-24 12:55:45 +00:00
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
frame.forEach(function (singleFrame)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < quantity; i++)
|
|
|
|
{
|
|
|
|
bobs.push(_this.create(0, 0, singleFrame, visible));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return bobs;
|
|
|
|
},
|
|
|
|
|
2018-01-26 15:37:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Blitter#childCanRender
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-01 05:48:56 +00:00
|
|
|
* @param {Phaser.GameObjects.Blitter.Bob} child - [description]
|
2018-01-26 15:37:34 +00:00
|
|
|
*
|
2018-02-01 05:48:56 +00:00
|
|
|
* @return {boolean} [description]
|
2018-01-26 15:37:34 +00:00
|
|
|
*/
|
2017-02-23 03:54:54 +00:00
|
|
|
childCanRender: function (child)
|
2017-01-24 12:55:45 +00:00
|
|
|
{
|
2017-02-23 03:54:54 +00:00
|
|
|
return (child.visible && child.alpha > 0);
|
|
|
|
},
|
|
|
|
|
2018-01-26 15:37:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Blitter#getRenderList
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-14 12:25:17 +00:00
|
|
|
* @return {Phaser.GameObjects.Blitter.Bob[]} An array of Bob objects that will be rendered this frame.
|
2018-01-26 15:37:34 +00:00
|
|
|
*/
|
2017-02-23 03:54:54 +00:00
|
|
|
getRenderList: function ()
|
|
|
|
{
|
|
|
|
if (this.dirty)
|
2017-01-22 22:54:06 +00:00
|
|
|
{
|
2017-02-23 03:54:54 +00:00
|
|
|
this.renderList = this.children.list.filter(this.childCanRender, this);
|
|
|
|
this.dirty = false;
|
2017-01-22 22:54:06 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
return this.renderList;
|
|
|
|
},
|
2017-01-22 22:54:06 +00:00
|
|
|
|
2018-01-26 15:37:34 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Blitter#clear
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-02-23 03:54:54 +00:00
|
|
|
clear: function ()
|
2017-02-13 23:57:32 +00:00
|
|
|
{
|
2017-02-23 03:54:54 +00:00
|
|
|
this.children.removeAll();
|
|
|
|
this.dirty = true;
|
2017-02-13 23:57:32 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Blitter;
|