Skip to content

Commit 115afbc

Browse files
committed
Added in Sprite game object.
1 parent 060ea99 commit 115afbc

6 files changed

Lines changed: 180 additions & 1 deletion

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: 'd280f080-e252-11e6-bada-f9ef445cacef'
2+
build: '56117780-e25d-11e6-9df1-9b17ba01ef95'
33
};
44
module.exports = CHECKSUM;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2016 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var CONST = require('../../const');
8+
var GameObject = require('../GameObject');
9+
var Children = require('../../components/Children');
10+
11+
var Sprite = function (state, x, y, key, frame)
12+
{
13+
var _texture = state.game.textures.get(key);
14+
var _frame = _texture.get(frame);
15+
16+
GameObject.call(this, state, x, y, _texture, _frame);
17+
18+
this.type = CONST.SPRITE;
19+
20+
this.children = new Children(this);
21+
};
22+
23+
Sprite.prototype = Object.create(GameObject.prototype);
24+
Sprite.prototype.constructor = Sprite;
25+
26+
Sprite.prototype.renderCanvas = require('./SpriteCanvasRenderer');
27+
Sprite.prototype.renderWebGL = require('./SpriteWebGLRenderer');
28+
29+
Object.defineProperties(Sprite.prototype, {
30+
31+
width: {
32+
33+
enumerable: true,
34+
35+
get: function ()
36+
{
37+
return this.transform._scaleX * this.frame.realWidth;
38+
},
39+
40+
set: function (value)
41+
{
42+
this.scaleX = value / this.frame.realWidth;
43+
}
44+
45+
},
46+
47+
height: {
48+
49+
enumerable: true,
50+
51+
get: function ()
52+
{
53+
return this.transform._scaleY * this.frame.realHeight;
54+
},
55+
56+
set: function (value)
57+
{
58+
this.scaleY = value / this.frame.realHeight;
59+
}
60+
61+
}
62+
63+
});
64+
65+
module.exports = Sprite;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
var SpriteCanvasRenderer = function (renderer, src, interpolationPercentage)
3+
{
4+
var frame = src.frame;
5+
var alpha = src.color.worldAlpha * 255 << 24;
6+
7+
// Skip rendering?
8+
9+
if (src.skipRender || !src.visible || alpha === 0 || !frame.cutWidth || !frame.cutHeight)
10+
{
11+
return;
12+
}
13+
14+
var data = src.transform.getCanvasTransformData(interpolationPercentage, renderer);
15+
var tint = src.color._glTint;
16+
var bg = src.color._glBg;
17+
18+
renderer.drawImage(frame, src.blendMode, data, alpha, tint, bg);
19+
20+
// Render children
21+
for (var i = 0; i < src.children.list.length; i++)
22+
{
23+
var child = src.children.list[i];
24+
25+
child.renderCanvas(renderer, child, interpolationPercentage);
26+
}
27+
};
28+
29+
module.exports = SpriteCanvasRenderer;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2016 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var Sprite = require('./Sprite');
8+
var FactoryContainer = require('../../gameobjects/FactoryContainer');
9+
10+
var SpriteFactory = {
11+
12+
KEY: 'sprite',
13+
14+
/**
15+
* Create a new Sprite with specific position and sprite sheet key.
16+
*
17+
* At its most basic a Sprite consists of a set of coordinates and a texture that is used when rendered.
18+
* They also contain additional properties allowing for physics motion (via Sprite.body), input handling (via Sprite.input),
19+
* events (via Sprite.events), animation (via Sprite.animations), camera culling and more. Please see the Examples for use cases.
20+
*
21+
* @method Phaser.GameObject.Factory#sprite
22+
* @param {number} [x=0] - The x coordinate of the sprite. The coordinate is relative to any parent container this sprite may be in.
23+
* @param {number} [y=0] - The y coordinate of the sprite. The coordinate is relative to any parent container this sprite may be in.
24+
* @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.
25+
* @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.
26+
* @param {Phaser.Group} [group] - Optional Group to add the object to. If not specified it will be added to the World group.
27+
* @return {Phaser.Sprite} The newly created Sprite object.
28+
*/
29+
add: function (x, y, key, frame, group)
30+
{
31+
if (group === undefined) { group = this.state; }
32+
33+
// console.log('ImageFactory.add', key, x, y, frame, group);
34+
// console.log('into State', this.state);
35+
36+
return group.children.add(new Sprite(this.state, x, y, key, frame));
37+
},
38+
39+
make: function (x, y, key, frame)
40+
{
41+
// console.log('ImageFactory.make', key, x, y, frame);
42+
43+
return new Sprite(this.state, x, y, key, frame);
44+
}
45+
46+
};
47+
48+
module.exports = FactoryContainer.register(SpriteFactory);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
var SpriteWebGLRenderer = function (renderer, src, interpolationPercentage)
3+
{
4+
var frame = src.frame;
5+
var alpha = src.color.worldAlpha * 255 << 24;
6+
7+
// Skip rendering?
8+
9+
if (src.skipRender || !src.visible || alpha === 0 || !frame.cutWidth || !frame.cutHeight)
10+
{
11+
return;
12+
}
13+
14+
var transform = src.transform;
15+
16+
renderer.setBlendMode(src.color._blendMode);
17+
18+
renderer.spriteBatch.add(
19+
frame,
20+
transform._anchorX, transform._anchorY,
21+
transform.world.tx, transform.world.ty,
22+
transform._worldScaleX, transform._worldScaleY,
23+
transform._worldRotation,
24+
src.color._glTint
25+
);
26+
27+
// Render children
28+
for (var i = 0; i < src.children.list.length; i++)
29+
{
30+
var child = src.children.list[i];
31+
32+
child.renderWebGL(renderer, child, interpolationPercentage);
33+
}
34+
};
35+
36+
module.exports = SpriteWebGLRenderer;

v3/src/phaser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var Phaser = {
4343

4444
require('./gameobjects/blitter/BlitterFactory');
4545
require('./gameobjects/image/ImageFactory');
46+
require('./gameobjects/sprite/SpriteFactory');
4647
require('./gameobjects/container/ContainerFactory');
4748

4849
// Merge in the consts

0 commit comments

Comments
 (0)