Skip to content

Commit 6661952

Browse files
committed
Masses of changes to get tilemap layer display dimensions filtering down to linked child layers. Also allowed offset to work correctly, and fixed the display sizes with offset bounds. Exposed offset as layer.x/y. Need to duplicate in canvas variation.
1 parent d994641 commit 6661952

4 files changed

Lines changed: 224 additions & 109 deletions

File tree

src/Phaser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var Phaser = Phaser || {
1515
* @constant
1616
* @type {string}
1717
*/
18-
VERSION: '2.7.0 Beta',
18+
VERSION: '2.7.0 Beta 2',
1919

2020
/**
2121
* An array of Phaser game instances.

src/pixi/extras/Tilemap.js

Lines changed: 29 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,30 @@ PIXI.Tilemap = function (texture, displayWidth, displayHeight, mapWidth, mapHeig
2424
PIXI.DisplayObjectContainer.call(this);
2525

2626
/**
27-
* The clipping limit of the display area.
27+
* The texture of the Tilemap
2828
*
29-
* @property displayWidth
30-
* @type integer
29+
* @property texture
30+
* @type Texture
3131
*/
32-
this.displayWidth = displayWidth;
32+
this.texture = texture;
3333

3434
/**
3535
* The clipping limit of the display area.
3636
*
37-
* @property displayHeight
37+
* @property _displayWidth
3838
* @type integer
39+
* @private
3940
*/
40-
this.displayHeight = displayHeight;
41+
this._displayWidth = displayWidth;
4142

4243
/**
43-
* The texture of the Tilemap
44+
* The clipping limit of the display area.
4445
*
45-
* @property texture
46-
* @type Texture
46+
* @property _displayHeight
47+
* @type integer
48+
* @private
4749
*/
48-
this.texture = texture;
50+
this._displayHeight = displayHeight;
4951

5052
/**
5153
* The width of a single tile in pixels.
@@ -289,26 +291,26 @@ PIXI.Tilemap.prototype._renderBatch = function (renderSession) {
289291
}
290292

291293
// calculate the destination location of the tile in screen units (-1..1)
292-
buffer[ c ] = buffer[ c + 4 ] = lft;
293-
buffer[ c + 1 ] = buffer[ c + 9 ] = bot;
294-
buffer[ c + 8 ] = buffer[ c + 12] = oldR = x + wide;
295-
buffer[ c + 5 ] = buffer[ c + 13] = oldT = y - high;
294+
buffer[ c ] = buffer[ c + 4 ] = lft;
295+
buffer[ c + 1 ] = buffer[ c + 9 ] = bot;
296+
buffer[ c + 8 ] = buffer[ c + 12 ] = oldR = x + wide;
297+
buffer[ c + 5 ] = buffer[ c + 13 ] = oldT = y - high;
296298

297299
// calculate the uv coordinates of the tile source image
298300
if (t.fd === 1)
299301
{
300302
// flipped diagonally, swap x,y axes
301-
buffer[ c + 14] = buffer[ c + 6 ] = uvl;
302-
buffer[ c + 15] = buffer[ c + 11] = uvt;
303-
buffer[ c + 10] = buffer[ c + 2 ] = uvl + t.sw * iTextureWide;
304-
buffer[ c + 7 ] = buffer[ c + 3 ] = uvt + t.sh * iTextureHigh;
303+
buffer[ c + 14 ] = buffer[ c + 6 ] = uvl;
304+
buffer[ c + 15 ] = buffer[ c + 11 ] = uvt;
305+
buffer[ c + 10 ] = buffer[ c + 2 ] = uvl + t.sw * iTextureWide;
306+
buffer[ c + 7 ] = buffer[ c + 3 ] = uvt + t.sh * iTextureHigh;
305307
}
306308
else
307309
{
308-
buffer[ c + 2 ] = buffer[ c + 6 ] = uvl;
309-
buffer[ c + 3 ] = buffer[ c + 11] = uvt;
310-
buffer[ c + 10] = buffer[ c + 14] = uvl + t.sw * iTextureWide;
311-
buffer[ c + 7 ] = buffer[ c + 15] = uvt + t.sh * iTextureHigh;
310+
buffer[ c + 2 ] = buffer[ c + 6 ] = uvl;
311+
buffer[ c + 3 ] = buffer[ c + 11 ] = uvt;
312+
buffer[ c + 10 ] = buffer[ c + 14 ] = uvl + t.sw * iTextureWide;
313+
buffer[ c + 7 ] = buffer[ c + 15 ] = uvt + t.sh * iTextureHigh;
312314
}
313315

314316
// advance the buffer index
@@ -333,9 +335,9 @@ PIXI.Tilemap.prototype._renderBatch = function (renderSession) {
333335
};
334336

335337
/**
336-
* render the entire tilemap using a fast webgl batched tile render
338+
* Render the entire tilemap using a fast WebGL batched tile render.
337339
*
338-
* @param {[type]} renderSession [description]
340+
* @param {Object} renderSession - The PIXI RenderSession.
339341
*/
340342
PIXI.Tilemap.prototype._renderWholeTilemap = function (renderSession) {
341343

@@ -351,9 +353,9 @@ PIXI.Tilemap.prototype._renderWholeTilemap = function (renderSession) {
351353
gl.uniform2f(shader.uOffset, renderSession.offset.x / this.game.width * 2, -renderSession.offset.y / this.game.height * 2);
352354

353355
// set the clipping limits
354-
gl.uniform2f(shader.uClipOffset, this.offset.x / this.game.width * 2, this.offset.y / this.game.height * 2);
355-
gl.uniform2f(shader.uClipLoc, this.offset.x, this.offset.y);
356-
gl.uniform2f(shader.uClipLimit, this.displayWidth, this.game.height - this.displayHeight);
356+
gl.uniform2f(shader.uClipOffset, this._offset.x / this.game.width * 2, this._offset.y / this.game.height * 2);
357+
gl.uniform2f(shader.uClipLoc, this._offset.x, this._offset.y);
358+
gl.uniform2f(shader.uClipLimit, this._offset.x + this._displayWidth, this.game.height - (this._offset.y + this._displayHeight));
357359

358360
// set the offset in screen units to the center of the screen
359361
// and flip the GL y coordinate to be zero at the top
@@ -399,67 +401,3 @@ PIXI.Tilemap.prototype.onTextureUpdate = function () {
399401
this.updateFrame = true;
400402

401403
};
402-
403-
/**
404-
* Returns the bounds of the map as a rectangle. The bounds calculation takes the worldTransform into account.
405-
*
406-
* @method getBounds
407-
* @param matrix {Matrix} the transformation matrix of the sprite
408-
* @return {Rectangle} the framing rectangle
409-
*/
410-
PIXI.Tilemap.prototype.getBounds = function (matrix) {
411-
412-
var worldTransform = matrix || this.worldTransform;
413-
414-
var a = worldTransform.a;
415-
var b = worldTransform.b;
416-
var c = worldTransform.c;
417-
var d = worldTransform.d;
418-
var tx = worldTransform.tx;
419-
var ty = worldTransform.ty;
420-
421-
var maxX = -Infinity;
422-
var maxY = -Infinity;
423-
424-
var minX = Infinity;
425-
var minY = Infinity;
426-
427-
var vertices = [
428-
0, 0,
429-
this.mapWide * this.tileWide, 0,
430-
this.mapWide * this.tileWide, this.mapHigh * this.tileHigh,
431-
0, this.mapHigh * this.tileHigh
432-
];
433-
434-
for (var i = 0, n = vertices.length; i < n; i += 2)
435-
{
436-
var rawX = vertices[i], rawY = vertices[i + 1];
437-
var x = (a * rawX) + (c * rawY) + tx;
438-
var y = (d * rawY) + (b * rawX) + ty;
439-
440-
minX = x < minX ? x : minX;
441-
minY = y < minY ? y : minY;
442-
443-
maxX = x > maxX ? x : maxX;
444-
maxY = y > maxY ? y : maxY;
445-
}
446-
447-
if (minX === -Infinity || maxY === Infinity)
448-
{
449-
return PIXI.EmptyRectangle;
450-
}
451-
452-
var bounds = this._bounds;
453-
454-
bounds.x = minX;
455-
bounds.width = maxX - minX;
456-
457-
bounds.y = minY;
458-
bounds.height = maxY - minY;
459-
460-
// store a reference so that if this function gets called again in the render cycle we do not have to recalculate
461-
this._currentBounds = bounds;
462-
463-
return bounds;
464-
465-
};

src/tilemap/Tilemap.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -601,15 +601,30 @@ Phaser.Tilemap.prototype = {
601601
console.log('Tilemap.createLayer', this.layers[index].name, width, 'x', height, 'tileset', this.tilesets[0].name, 'index:', index);
602602
}
603603

604+
var rootLayer;
605+
606+
if (this.game.renderType === Phaser.WEBGL)
607+
{
608+
// use WebGL variant of TilemapLayer
609+
rootLayer = group.add(new Phaser.TilemapLayerGL(this.game, this, index, width, height));
610+
}
611+
else
612+
{
613+
rootLayer = group.add(new Phaser.TilemapLayer(this.game, this, index, width, height));
614+
}
615+
604616
// attempt to create internal layers for multiple tilesets in a layer
605617
// it is currently assumed that each base layer will use tileset[0] (so 'i' starts at 1 in the for loop)
606618
// (a 'base' layer is any layer except the so-called 'internal' layers created here)
607619
for (var i = 1; i < this.tilesets.length; i++)
608620
{
609621
var ts = this.tilesets[i];
610622
var li = this.layers[index];
623+
var name = 'layer' + index + '_internal_' + i;
611624

612-
if (!this.createInternalLayer('layer' + index + '_internal_' + i, ts, li, ts.tileWidth, ts.tileHeight, group))
625+
var childLayer = this.createInternalLayer(name, ts, li, ts.tileWidth, ts.tileHeight, group);
626+
627+
if (!childLayer)
613628
{
614629
if (this.enableDebug)
615630
{
@@ -619,6 +634,8 @@ Phaser.Tilemap.prototype = {
619634
}
620635
else
621636
{
637+
rootLayer.linkedLayers.push(childLayer);
638+
622639
if (this.enableDebug)
623640
{
624641
// we removed all tiles belonging to the tileset in the base layer, and placed them in a new internal layer
@@ -627,15 +644,11 @@ Phaser.Tilemap.prototype = {
627644
}
628645
}
629646

630-
if (this.game.renderType === Phaser.WEBGL)
631-
{
632-
// use WebGL variant of TilemapLayer
633-
return group.add(new Phaser.TilemapLayerGL(this.game, this, index, width, height));
634-
}
635-
else
636-
{
637-
return group.add(new Phaser.TilemapLayer(this.game, this, index, width, height));
638-
}
647+
// Automatically updates all children too
648+
rootLayer.displayWidth = width;
649+
rootLayer.displayHeight = height;
650+
651+
return rootLayer;
639652

640653
},
641654

@@ -729,7 +742,7 @@ Phaser.Tilemap.prototype = {
729742
output.push(row);
730743
}
731744

732-
if ( emptyFlag )
745+
if (emptyFlag)
733746
{
734747
// none of those tiles are used in this layer, so we don't actually need it
735748
return;
@@ -752,20 +765,20 @@ Phaser.Tilemap.prototype = {
752765
h = this.game.height;
753766
}
754767

755-
var output;
768+
var displayLayer;
756769

757770
if (this.game.renderType === Phaser.WEBGL)
758771
{
759-
output = new Phaser.TilemapLayerGL(this.game, this, this.layers.length - 1, w, h);
772+
displayLayer = new Phaser.TilemapLayerGL(this.game, this, this.layers.length - 1, w, h);
760773
}
761774
else
762775
{
763-
output = new Phaser.TilemapLayer(this.game, this, this.layers.length - 1, w, h);
776+
displayLayer = new Phaser.TilemapLayer(this.game, this, this.layers.length - 1, w, h);
764777
}
765778

766-
output.name = name;
779+
displayLayer.name = name;
767780

768-
return group.add(output);
781+
return group.add(displayLayer);
769782

770783
},
771784

0 commit comments

Comments
 (0)