Skip to content

Commit 58e44f7

Browse files
committed
SpriteBatch converted. It's an extended Group and Batch merged and works amazingly :) Ported over the maggots demo to test and wow!
1 parent 3e99391 commit 58e44f7

8 files changed

Lines changed: 180 additions & 0 deletions

File tree

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ module.exports = function (grunt) {
9595
'src/gameobjects/Button.js',
9696
'src/gameobjects/Graphics.js',
9797
'src/gameobjects/RenderTexture.js',
98+
'src/gameobjects/SpriteBatch.js',
9899

99100
'src/system/Canvas.js',
100101
'src/system/StageScaleMode.js',

build/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<script src="$path/src/gameobjects/Button.js"></script>
142142
<script src="$path/src/gameobjects/Graphics.js"></script>
143143
<script src="$path/src/gameobjects/RenderTexture.js"></script>
144+
<script src="$path/src/gameobjects/SpriteBatch.js"></script>
144145
145146
<script src="$path/src/system/Canvas.js"></script>
146147
<script src="$path/src/system/StageScaleMode.js"></script>

examples/assets/sprites/maggot.png

5.2 KB
Loading

examples/wip/spritebatch1.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
3+
4+
function preload() {
5+
6+
game.load.image('maggot', 'assets/sprites/maggot.png');
7+
8+
}
9+
10+
var batch;
11+
var dudeBoundsPadding = 100;
12+
var dudeBounds = new Phaser.Rectangle(-dudeBoundsPadding, -dudeBoundsPadding, 800 + dudeBoundsPadding * 2, 600 + dudeBoundsPadding * 2);
13+
var tick = 0;
14+
15+
function create() {
16+
17+
batch = game.add.spriteBatch();
18+
19+
var total = (game.renderType === Phaser.WEBGL) ? 10000 : 100;
20+
21+
for (var i = 0; i < total; i++)
22+
{
23+
var dude = batch.create(game.world.randomX, game.world.randomY, 'maggot');
24+
25+
dude.anchor.set(0.5);
26+
dude.scale.set(0.8 + Math.random() * 0.3);
27+
dude.direction = Math.random() * Math.PI * 2;
28+
dude.turningSpeed = Math.random() - 0.8;
29+
dude.speed = (2 + Math.random() * 2) * 0.2;
30+
dude.offset = Math.random() * 100;
31+
}
32+
33+
}
34+
35+
function update() {
36+
37+
batch.forEach(updateMaggot, this, false);
38+
39+
tick += 0.1;
40+
41+
}
42+
43+
function updateMaggot(dude) {
44+
45+
dude.scale.y = 0.95 + Math.sin(tick + dude.offset) * 0.05
46+
dude.direction += dude.turningSpeed * 0.01;
47+
dude.position.x += Math.sin(dude.direction) * (dude.speed * dude.scale.y);
48+
dude.position.y += Math.cos(dude.direction) * (dude.speed * dude.scale.y);
49+
dude.rotation = -dude.direction + Math.PI;
50+
51+
// wrap the dudes by testing their bounds..
52+
if (dude.position.x < dudeBounds.x)
53+
dude.position.x += dudeBounds.width;
54+
else if (dude.position.x > dudeBounds.x + dudeBounds.width)
55+
dude.position.x -= dudeBounds.width;
56+
57+
if (dude.position.y < dudeBounds.y)
58+
dude.position.y += dudeBounds.height;
59+
else if (dude.position.y > dudeBounds.y + dudeBounds.height)
60+
dude.position.y -= dudeBounds.height;
61+
62+
}

labs/code/004 maggot batch.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
3+
4+
function preload() {
5+
6+
game.load.image('maggot', 'assets/sprites/maggot.png');
7+
8+
}
9+
10+
var batch;
11+
var dudeBoundsPadding = 100;
12+
var dudeBounds = new Phaser.Rectangle(-dudeBoundsPadding, -dudeBoundsPadding, 800 + dudeBoundsPadding * 2, 600 + dudeBoundsPadding * 2);
13+
var tick = 0;
14+
15+
function create() {
16+
17+
batch = game.add.spriteBatch();
18+
19+
var total = (game.renderType === Phaser.WEBGL) ? 10000 : 100;
20+
21+
for (var i = 0; i < total; i++)
22+
{
23+
var dude = batch.create(game.world.randomX, game.world.randomY, 'maggot');
24+
25+
dude.anchor.set(0.5);
26+
dude.scale.set(0.8 + Math.random() * 0.3);
27+
dude.direction = Math.random() * Math.PI * 2;
28+
dude.turningSpeed = Math.random() - 0.8;
29+
dude.speed = (2 + Math.random() * 2) * 0.2;
30+
dude.offset = Math.random() * 100;
31+
}
32+
33+
}
34+
35+
function update() {
36+
37+
batch.forEach(updateMaggot, this, false);
38+
39+
tick += 0.1;
40+
41+
}
42+
43+
function updateMaggot(dude) {
44+
45+
dude.scale.y = 0.95 + Math.sin(tick + dude.offset) * 0.05
46+
dude.direction += dude.turningSpeed * 0.01;
47+
dude.position.x += Math.sin(dude.direction) * (dude.speed * dude.scale.y);
48+
dude.position.y += Math.cos(dude.direction) * (dude.speed * dude.scale.y);
49+
dude.rotation = -dude.direction + Math.PI;
50+
51+
// wrap the dudes by testing their bounds..
52+
if (dude.position.x < dudeBounds.x)
53+
dude.position.x += dudeBounds.width;
54+
else if (dude.position.x > dudeBounds.x + dudeBounds.width)
55+
dude.position.x -= dudeBounds.width;
56+
57+
if (dude.position.y < dudeBounds.y)
58+
dude.position.y += dudeBounds.height;
59+
else if (dude.position.y > dudeBounds.y + dudeBounds.height)
60+
dude.position.y -= dudeBounds.height;
61+
62+
}

src/Phaser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var Phaser = Phaser || {
3535
CANVAS_FILTER: 14,
3636
WEBGL_FILTER: 15,
3737
ELLIPSE: 16,
38+
SPRITEBATCH: 16,
3839

3940
NONE: 0,
4041
LEFT: 1,

src/gameobjects/GameObjectFactory.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,24 @@ Phaser.GameObjectFactory.prototype = {
109109

110110
},
111111

112+
/**
113+
* A Group is a container for display objects that allows for fast pooling, recycling and collision checks.
114+
*
115+
* @method Phaser.GameObjectFactory#spriteBatch
116+
* @param {any} parent - The parent Group or DisplayObjectContainer that will hold this group, if any.
117+
* @param {string} [name='group'] - A name for this Group. Not used internally but useful for debugging.
118+
* @param {boolean} [addToStage=false] - If set to true this Group will be added directly to the Game.Stage instead of Game.World.
119+
* @return {Phaser.Group} The newly created group.
120+
*/
121+
spriteBatch: function (parent, name, addToStage) {
122+
123+
if (typeof name === 'undefined') { name = 'group'; }
124+
if (typeof addToStage === 'undefined') { addToStage = false; }
125+
126+
return new Phaser.SpriteBatch(this.game, parent, name, addToStage);
127+
128+
},
129+
112130
/**
113131
* Creates a new Sound object.
114132
*

src/gameobjects/SpriteBatch.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2014 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* Phaser SpriteBatch constructor.
9+
* @class Phaser.SpriteBatch
10+
* @classdesc A Group is a container for display objects that allows for fast pooling and object recycling. Groups can be nested within other Groups and have their own local transforms.
11+
* @constructor
12+
* @param {Phaser.Game} game - A reference to the currently running game.
13+
* @param {Phaser.Group|Phaser.Sprite} parent - The parent Group, DisplayObject or DisplayObjectContainer that this Group will be added to. If undefined or null it will use game.world.
14+
* @param {string} [name=group] - A name for this Group. Not used internally but useful for debugging.
15+
* @param {boolean} [addToStage=false] - If set to true this Group will be added directly to the Game.Stage instead of Game.World.
16+
*/
17+
Phaser.SpriteBatch = function (game, parent, name, addToStage) {
18+
19+
PIXI.SpriteBatch.call(this);
20+
21+
Phaser.Group.call(this, game, parent, name, addToStage);
22+
23+
/**
24+
* @property {number} type - Internal Phaser Type value.
25+
* @protected
26+
*/
27+
this.type = Phaser.SPRITEBATCH;
28+
29+
};
30+
31+
// Phaser.SpriteBatch.prototype = Object.create(Phaser.Group.prototype);
32+
33+
Phaser.SpriteBatch.prototype = Phaser.Utils.extend(true, Phaser.SpriteBatch.prototype, Phaser.Group.prototype, PIXI.SpriteBatch.prototype);
34+
35+
Phaser.SpriteBatch.prototype.constructor = Phaser.SpriteBatch;

0 commit comments

Comments
 (0)