Skip to content

Commit 0fa54b0

Browse files
committed
TileSprites now emit outOfBounds and enterBounds events accordingly.
TileSprites working with physics bodies again.
1 parent 9c8f01c commit 0fa54b0

7 files changed

Lines changed: 112 additions & 73 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Updated
121121
* Emitters now bring the particle they are about to emit to the top of the Group before doing so. Avoids particles hidden behind others.
122122
* ArcadePhysics.Body.setSize corrected to take the parameters as positive, not negative values.
123123
* ArcadePhysics.World.seperate will now check gravity totals to determine separation order. You can set World.forceX to true to always separate on X first and skip this check.
124+
* TileSprites now emit outOfBounds and enterBounds events accordingly.
124125

125126

126127
New Features

src/gameobjects/BitmapData.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ Phaser.BitmapData = function (game, key, width, height) {
101101
*/
102102
this.type = Phaser.BITMAPDATA;
103103

104-
this._dirty = false;
104+
/**
105+
* @property {boolean} dirty - If dirty this BitmapData will be re-rendered.
106+
*/
107+
this.dirty = false;
105108

106109
}
107110

@@ -140,7 +143,7 @@ Phaser.BitmapData.prototype = {
140143

141144
this.context.clearRect(0, 0, this.width, this.height);
142145

143-
this._dirty = true;
146+
this.dirty = true;
144147

145148
},
146149

@@ -161,7 +164,7 @@ Phaser.BitmapData.prototype = {
161164
this.imageData = this.context.getImageData(0, 0, width, height);
162165
}
163166

164-
this._dirty = true;
167+
this.dirty = true;
165168

166169
},
167170

@@ -209,7 +212,7 @@ Phaser.BitmapData.prototype = {
209212

210213
this.context.putImageData(this.imageData, 0, 0);
211214

212-
this._dirty = true;
215+
this.dirty = true;
213216
}
214217

215218
},
@@ -361,16 +364,14 @@ Phaser.BitmapData.prototype = {
361364
*/
362365
render: function () {
363366

364-
if (this._dirty)
367+
console.log('bmd dity');
368+
if (this.game.renderType === Phaser.WEBGL && this.dirty)
365369
{
366370
// Only needed if running in WebGL, otherwise this array will never get cleared down
367-
if (this.game.renderType === Phaser.WEBGL)
368-
{
369-
// should use the rendersession
370-
PIXI.updateWebGLTexture(this.baseTexture, this.game.renderer.gl);
371-
}
371+
// should use the rendersession
372+
PIXI.updateWebGLTexture(this.baseTexture, this.game.renderer.gl);
372373

373-
this._dirty = false;
374+
this.dirty = false;
374375
}
375376

376377
}

src/gameobjects/Image.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Phaser.Image.prototype.update = function() {
184184
*/
185185
Phaser.Image.prototype.postUpdate = function() {
186186

187-
if (this.key instanceof Phaser.BitmapData && this.key._dirty)
187+
if (this.key instanceof Phaser.BitmapData)
188188
{
189189
this.key.render();
190190
}

src/gameobjects/Sprite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ Phaser.Sprite.prototype.update = function() {
308308
*/
309309
Phaser.Sprite.prototype.postUpdate = function() {
310310

311-
if (this.key instanceof Phaser.BitmapData && this.key._dirty)
311+
if (this.key instanceof Phaser.BitmapData)
312312
{
313313
this.key.render();
314314
}

src/gameobjects/TileSprite.js

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,25 @@ Phaser.TileSprite = function (game, x, y, width, height, key, frame) {
9898
*/
9999
this.world = new Phaser.Point(x, y);
100100

101+
/**
102+
* Should this Sprite be automatically culled if out of range of the camera?
103+
* A culled sprite has its renderable property set to 'false'.
104+
* Be advised this is quite an expensive operation, as it has to calculate the bounds of the object every frame, so only enable it if you really need it.
105+
*
106+
* @property {boolean} autoCull - A flag indicating if the Sprite should be automatically camera culled or not.
107+
* @default
108+
*/
109+
this.autoCull = false;
110+
111+
/**
112+
* If true the Sprite checks if it is still within the world each frame, when it leaves the world it dispatches Sprite.events.onOutOfBounds
113+
* and optionally kills the sprite (if Sprite.outOfBoundsKill is true). By default this is disabled because the Sprite has to calculate its
114+
* bounds every frame to support it, and not all games need it. Enable it by setting the value to true.
115+
* @property {boolean} checkWorldBounds
116+
* @default
117+
*/
118+
this.checkWorldBounds = false;
119+
101120
/**
102121
* @property {Phaser.Point} cameraOffset - If this object is fixedToCamera then this stores the x/y offset that its drawn at, from the top-left of the camera view.
103122
*/
@@ -163,26 +182,65 @@ Phaser.TileSprite.prototype.preUpdate = function() {
163182
return false;
164183
}
165184

166-
this.world.setTo(this.game.camera.x + this.worldTransform[2], this.game.camera.y + this.worldTransform[5]);
185+
this._cache[0] = this.world.x;
186+
this._cache[1] = this.world.y;
187+
this._cache[2] = this.rotation;
167188

168-
this.animations.update();
189+
if (!this.exists || !this.parent.exists)
190+
{
191+
// Reset the renderOrderID
192+
this._cache[3] = -1;
193+
return false;
194+
}
169195

170-
if (this._scroll.x !== 0)
196+
// Cache the bounds if we need it
197+
if (this.autoCull || this.checkWorldBounds)
171198
{
172-
this.tilePosition.x += this._scroll.x * this.game.time.physicsElapsed;
199+
this._bounds.copyFrom(this.getBounds());
173200
}
174201

175-
if (this._scroll.y !== 0)
202+
if (this.autoCull)
176203
{
177-
this.tilePosition.y += this._scroll.y * this.game.time.physicsElapsed;
204+
// Won't get rendered but will still get its transform updated
205+
this.renderable = this.game.world.camera.screenView.intersects(this._bounds);
178206
}
179207

208+
if (this.checkWorldBounds)
209+
{
210+
// The Sprite is already out of the world bounds, so let's check to see if it has come back again
211+
if (this._cache[5] === 1 && this.game.world.bounds.intersects(this._bounds))
212+
{
213+
this._cache[5] = 0;
214+
this.events.onEnterBounds.dispatch(this);
215+
}
216+
else if (this._cache[5] === 0 && !this.game.world.bounds.intersects(this._bounds))
217+
{
218+
// The Sprite WAS in the screen, but has now left.
219+
this._cache[5] = 1;
220+
this.events.onOutOfBounds.dispatch(this);
221+
}
222+
}
223+
224+
this.world.setTo(this.game.camera.x + this.worldTransform.tx, this.game.camera.y + this.worldTransform.ty);
225+
180226
if (this.visible)
181227
{
182228
this._cache[3] = this.game.stage.currentRenderOrderID++;
183229
}
184230

185-
if (this.exists && this.body)
231+
this.animations.update();
232+
233+
if (this._scroll.x !== 0)
234+
{
235+
this.tilePosition.x += this._scroll.x * this.game.time.physicsElapsed;
236+
}
237+
238+
if (this._scroll.y !== 0)
239+
{
240+
this.tilePosition.y += this._scroll.y * this.game.time.physicsElapsed;
241+
}
242+
243+
if (this.body)
186244
{
187245
this.body.preUpdate();
188246
}

src/tilemap/Tilemap.js

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -190,44 +190,6 @@ Phaser.Tilemap.prototype = {
190190

191191
return this.createBlankLayer(name, width, height, tileWidth, tileHeight, group);
192192

193-
/*
194-
var row;
195-
var output = [];
196-
197-
for (var y = 0; y < height; y++)
198-
{
199-
row = [];
200-
201-
for (var x = 0; x < width; x++)
202-
{
203-
row.push(null);
204-
}
205-
206-
output.push(row);
207-
}
208-
209-
this.layers.push({
210-
211-
name: name,
212-
x: 0,
213-
y: 0,
214-
width: width,
215-
height: height,
216-
widthInPixels: this.widthInPixels,
217-
heightInPixels: this.heightInPixels,
218-
alpha: 1,
219-
visible: true,
220-
properties: {},
221-
indexes: [],
222-
callbacks: [],
223-
bodies: [],
224-
data: output
225-
226-
});
227-
228-
this.currentLayer = this.layers.length - 1;
229-
*/
230-
231193
},
232194

233195
/**
@@ -258,6 +220,7 @@ Phaser.Tilemap.prototype = {
258220
* @param {number} [tileMargin=0] - The width of the tiles in the Tileset Image. If not given it will default to the map.tileWidth value.
259221
* @param {number} [tileSpacing=0] - The height of the tiles in the Tileset Image. If not given it will default to the map.tileHeight value.
260222
* @param {number} [gid=0] - If adding multiple tilesets to a blank/dynamic map, specify the starting GID the set will use here.
223+
* @return {Phaser.Tileset} Returns the Tileset object that was created or updated, or null if it failed.
261224
*/
262225
addTilesetImage: function (tileset, key, tileWidth, tileHeight, tileMargin, tileSpacing, gid) {
263226

@@ -275,7 +238,7 @@ Phaser.Tilemap.prototype = {
275238
}
276239
else
277240
{
278-
return false;
241+
return null;
279242
}
280243
}
281244

@@ -287,7 +250,7 @@ Phaser.Tilemap.prototype = {
287250
if (this.tilesets[tileset])
288251
{
289252
this.tilesets[tileset].setImage(this.game.cache.getImage(key));
290-
return true;
253+
return this.tilesets[tileset];
291254
}
292255
else
293256
{
@@ -335,9 +298,11 @@ Phaser.Tilemap.prototype = {
335298
}
336299
}
337300

301+
return newSet;
302+
338303
}
339304

340-
return false;
305+
return null;
341306

342307
},
343308

@@ -475,7 +440,7 @@ Phaser.Tilemap.prototype = {
475440
output.push(row);
476441
}
477442

478-
this.layers.push({
443+
var layer = {
479444

480445
name: name,
481446
x: 0,
@@ -492,11 +457,13 @@ Phaser.Tilemap.prototype = {
492457
bodies: [],
493458
data: output
494459

495-
});
460+
};
461+
462+
this.layers.push(layer);
496463

497464
this.currentLayer = this.layers.length - 1;
498465

499-
return group.add(new Phaser.TilemapLayer(this.game, this, this.layers.length - 1, width, height));
466+
return group.add(new Phaser.TilemapLayer(this.game, this, this.layers.length - 1, layer.widthInPixels, layer.heightInPixels));
500467

501468
},
502469

@@ -1013,12 +980,11 @@ Phaser.Tilemap.prototype = {
1013980
* @param {number} x - X position to place the tile (given in tile units, not pixels)
1014981
* @param {number} y - Y position to place the tile (given in tile units, not pixels)
1015982
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to modify.
983+
* @return {Phaser.Tile} The Tile object that was created or added to this map.
1016984
*/
1017985
putTile: function (tile, x, y, layer) {
1018986

1019-
console.log('putTile' ,layer);
1020987
layer = this.getLayer(layer);
1021-
console.log('putTile2', layer);
1022988

1023989
if (x >= 0 && x < this.layers[layer].width && y >= 0 && y < this.layers[layer].height)
1024990
{
@@ -1061,11 +1027,14 @@ Phaser.Tilemap.prototype = {
10611027
}
10621028

10631029
this.layers[layer].dirty = true;
1064-
console.log(this.layers[layer]);
10651030

10661031
this.calculateFaces(layer);
1032+
1033+
return this.layers[layer].data[y][x];
10671034
}
10681035

1036+
return null;
1037+
10691038
},
10701039

10711040
/**

src/tilemap/TilemapLayer.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Phaser.TilemapLayer = function (game, tilemap, index, width, height) {
4141
* @property {HTMLCanvasElement} canvas - The canvas to which this TilemapLayer draws.
4242
*/
4343
this.canvas = Phaser.Canvas.create(width, height, '', true);
44-
44+
4545
/**
4646
* @property {CanvasRenderingContext2D} context - The 2d context of the canvas.
4747
*/
@@ -62,7 +62,6 @@ Phaser.TilemapLayer = function (game, tilemap, index, width, height) {
6262
*/
6363
this.textureFrame = new Phaser.Frame(0, 0, 0, width, height, 'tilemapLayer', game.rnd.uuid());
6464

65-
// Phaser.Sprite.call(this, this.game, 0, 0, this.texture, this.textureFrame);
6665
Phaser.Image.call(this, this.game, 0, 0, this.texture, this.textureFrame);
6766

6867
/**
@@ -288,7 +287,6 @@ Phaser.TilemapLayer = function (game, tilemap, index, width, height) {
288287
};
289288

290289
Phaser.TilemapLayer.prototype = Object.create(Phaser.Image.prototype);
291-
// Phaser.TilemapLayer.prototype = Phaser.Utils.extend(true, Phaser.TilemapLayer.prototype, Phaser.Sprite.prototype, PIXI.Sprite.prototype);
292290
Phaser.TilemapLayer.prototype.constructor = Phaser.TilemapLayer;
293291

294292
/**
@@ -299,15 +297,27 @@ Phaser.TilemapLayer.prototype.constructor = Phaser.TilemapLayer;
299297
*/
300298
Phaser.TilemapLayer.prototype.postUpdate = function () {
301299

302-
// Phaser.Sprite.prototype.postUpdate.call(this);
303-
Phaser.Image.prototype.postUpdate.call(this);
300+
// Phaser.Image.prototype.postUpdate.call(this);
304301

305302
// Stops you being able to auto-scroll the camera if it's not following a sprite
306303
this.scrollX = this.game.camera.x * this.scrollFactorX;
307304
this.scrollY = this.game.camera.y * this.scrollFactorY;
308305

309306
this.render();
310307

308+
// Fixed to Camera?
309+
// if (this._cache[7] === 1)
310+
// {
311+
this.position.x = (this.game.camera.view.x + this.cameraOffset.x) / this.game.camera.scale.x;
312+
this.position.y = (this.game.camera.view.y + this.cameraOffset.y) / this.game.camera.scale.y;
313+
// }
314+
315+
// Update any Children
316+
// for (var i = 0, len = this.children.length; i < len; i++)
317+
// {
318+
// this.children[i].postUpdate();
319+
// }
320+
311321
}
312322

313323
/**
@@ -640,7 +650,7 @@ Phaser.TilemapLayer.prototype.render = function () {
640650
{
641651
tile = this._column[x];
642652

643-
set = this.map.tilesets[this.map.tiles[tile.index][2]]
653+
set = this.map.tilesets[this.map.tiles[tile.index][2]];
644654

645655
if (this.debug === false && tile.alpha !== this.context.globalAlpha)
646656
{

0 commit comments

Comments
 (0)