Added in Sprite game object.

This commit is contained in:
Richard Davey 2017-01-24 18:08:56 +00:00
parent 060ea99d49
commit 115afbc5fc
6 changed files with 180 additions and 1 deletions

View file

@ -1,4 +1,4 @@
var CHECKSUM = {
build: 'd280f080-e252-11e6-bada-f9ef445cacef'
build: '56117780-e25d-11e6-9df1-9b17ba01ef95'
};
module.exports = CHECKSUM;

View file

@ -0,0 +1,65 @@
/**
* @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');
var GameObject = require('../GameObject');
var Children = require('../../components/Children');
var Sprite = function (state, x, y, key, frame)
{
var _texture = state.game.textures.get(key);
var _frame = _texture.get(frame);
GameObject.call(this, state, x, y, _texture, _frame);
this.type = CONST.SPRITE;
this.children = new Children(this);
};
Sprite.prototype = Object.create(GameObject.prototype);
Sprite.prototype.constructor = Sprite;
Sprite.prototype.renderCanvas = require('./SpriteCanvasRenderer');
Sprite.prototype.renderWebGL = require('./SpriteWebGLRenderer');
Object.defineProperties(Sprite.prototype, {
width: {
enumerable: true,
get: function ()
{
return this.transform._scaleX * this.frame.realWidth;
},
set: function (value)
{
this.scaleX = value / this.frame.realWidth;
}
},
height: {
enumerable: true,
get: function ()
{
return this.transform._scaleY * this.frame.realHeight;
},
set: function (value)
{
this.scaleY = value / this.frame.realHeight;
}
}
});
module.exports = Sprite;

View file

@ -0,0 +1,29 @@
var SpriteCanvasRenderer = function (renderer, src, interpolationPercentage)
{
var frame = src.frame;
var alpha = src.color.worldAlpha * 255 << 24;
// Skip rendering?
if (src.skipRender || !src.visible || alpha === 0 || !frame.cutWidth || !frame.cutHeight)
{
return;
}
var data = src.transform.getCanvasTransformData(interpolationPercentage, renderer);
var tint = src.color._glTint;
var bg = src.color._glBg;
renderer.drawImage(frame, src.blendMode, data, alpha, tint, bg);
// Render children
for (var i = 0; i < src.children.list.length; i++)
{
var child = src.children.list[i];
child.renderCanvas(renderer, child, interpolationPercentage);
}
};
module.exports = SpriteCanvasRenderer;

View file

@ -0,0 +1,48 @@
/**
* @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 Sprite = require('./Sprite');
var FactoryContainer = require('../../gameobjects/FactoryContainer');
var SpriteFactory = {
KEY: 'sprite',
/**
* Create a new Sprite with specific position and sprite sheet key.
*
* At its most basic a Sprite consists of a set of coordinates and a texture that is used when rendered.
* They also contain additional properties allowing for physics motion (via Sprite.body), input handling (via Sprite.input),
* events (via Sprite.events), animation (via Sprite.animations), camera culling and more. Please see the Examples for use cases.
*
* @method Phaser.GameObject.Factory#sprite
* @param {number} [x=0] - The x coordinate of the sprite. The coordinate is relative to any parent container this sprite may be in.
* @param {number} [y=0] - The y coordinate of the sprite. The coordinate is relative to any parent container this sprite may be in.
* @param {string|Phaser.RenderTexture|Phaser.BitmapData|Phaser.Video|PIXI.Texture} [key] - The image used as a texture by this display object during rendering. If a string Phaser will get for an entry in the Image Cache. Or it can be an instance of a RenderTexture, BitmapData, Video or PIXI.Texture.
* @param {string|number} [frame] - If a Texture Atlas or Sprite Sheet is used this allows you to specify the frame to be used. Use either an integer for a Frame ID or a string for a frame name.
* @param {Phaser.Group} [group] - Optional Group to add the object to. If not specified it will be added to the World group.
* @return {Phaser.Sprite} The newly created Sprite object.
*/
add: function (x, y, key, frame, group)
{
if (group === undefined) { group = this.state; }
// console.log('ImageFactory.add', key, x, y, frame, group);
// console.log('into State', this.state);
return group.children.add(new Sprite(this.state, x, y, key, frame));
},
make: function (x, y, key, frame)
{
// console.log('ImageFactory.make', key, x, y, frame);
return new Sprite(this.state, x, y, key, frame);
}
};
module.exports = FactoryContainer.register(SpriteFactory);

View file

@ -0,0 +1,36 @@
var SpriteWebGLRenderer = function (renderer, src, interpolationPercentage)
{
var frame = src.frame;
var alpha = src.color.worldAlpha * 255 << 24;
// Skip rendering?
if (src.skipRender || !src.visible || alpha === 0 || !frame.cutWidth || !frame.cutHeight)
{
return;
}
var transform = src.transform;
renderer.setBlendMode(src.color._blendMode);
renderer.spriteBatch.add(
frame,
transform._anchorX, transform._anchorY,
transform.world.tx, transform.world.ty,
transform._worldScaleX, transform._worldScaleY,
transform._worldRotation,
src.color._glTint
);
// Render children
for (var i = 0; i < src.children.list.length; i++)
{
var child = src.children.list[i];
child.renderWebGL(renderer, child, interpolationPercentage);
}
};
module.exports = SpriteWebGLRenderer;

View file

@ -43,6 +43,7 @@ var Phaser = {
require('./gameobjects/blitter/BlitterFactory');
require('./gameobjects/image/ImageFactory');
require('./gameobjects/sprite/SpriteFactory');
require('./gameobjects/container/ContainerFactory');
// Merge in the consts