Skip to content

Commit ff09543

Browse files
committed
Merge pull request phaserjs#1608 from nkholski/dev
Added support for tiles rotated and/or flipped in Tiled
2 parents 4d8f9e8 + a0ea73a commit ff09543

3 files changed

Lines changed: 81 additions & 3 deletions

File tree

src/tilemap/Tile.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@ Phaser.Tile = function (layer, index, x, y, width, height) {
3737
* @property {number} y - The y map coordinate of this tile.
3838
*/
3939
this.y = y;
40+
41+
/**
42+
* @property {number} rotation - The rotation angle of this tile.
43+
*/
44+
this.rotation = 0;
4045

46+
/**
47+
* @property {boolean} flipped - Whether this tile is flipped (mirrored) or not.
48+
*/
49+
this.flipped = false;
50+
4151
/**
4252
* @property {number} x - The x map coordinate of this tile.
4353
*/

src/tilemap/TilemapLayer.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,21 @@ Phaser.TilemapLayer.prototype.renderRegion = function (scrollX, scrollY, left, t
833833

834834
if (set)
835835
{
836-
set.draw(context, tx, ty, index);
836+
if (tile.rotation || tile.flipped)
837+
{
838+
context.save();
839+
context.translate(tx + tile.centerX, ty + tile.centerY);
840+
context.rotate(tile.rotation);
841+
if (tile.flipped)
842+
{
843+
context.scale(-1, 1);
844+
}
845+
set.draw(context, -tile.centerX, -tile.centerY, index);
846+
context.restore();
847+
}
848+
else{
849+
set.draw(context, tx, ty, index);
850+
}
837851
}
838852
else if (this.debugSettings.missingImageFill)
839853
{

src/tilemap/TilemapParser.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ Phaser.TilemapParser = {
243243
var x = 0;
244244
var row = [];
245245
var output = [];
246+
var rotation, flipped, flippedVal, gid;
246247

247248
// Loop through the data field in the JSON.
248249

@@ -252,10 +253,63 @@ Phaser.TilemapParser = {
252253

253254
for (var t = 0, len = json.layers[i].data.length; t < len; t++)
254255
{
256+
rotation = 0;
257+
flipped = false;
258+
gid = json.layers[i].data[t];
259+
if (gid > 0x20000000) // if true the current tile is flipped or rotated (Tiled TMX format)
260+
{
261+
flippedVal=0;
262+
if(gid>0x80000000) // FlippedX
263+
{
264+
gid-=0x80000000;
265+
flippedVal+=4;
266+
}
267+
if(gid>0x40000000) // FlippedY
268+
{
269+
gid-=0x40000000;
270+
flippedVal+=2;
271+
}
272+
if(gid>0x20000000) // FlippedAD
273+
{
274+
gid-=0x20000000;
275+
flippedVal+=1;
276+
}
277+
278+
switch (flippedVal)
279+
{
280+
case 5:
281+
rotation = Math.PI/2;
282+
break;
283+
case 6:
284+
rotation = Math.PI;
285+
break;
286+
case 3:
287+
rotation = 3*Math.PI/2;
288+
break;
289+
case 4:
290+
rotation = 0;
291+
flipped = true;
292+
break;
293+
case 7:
294+
rotation = Math.PI/2;
295+
flipped = true;
296+
break;
297+
case 2:
298+
rotation = Math.PI;
299+
flipped = true;
300+
break;
301+
case 1:
302+
rotation = 3*Math.PI/2;
303+
flipped = true;
304+
break;
305+
}
306+
}
255307
// index, x, y, width, height
256-
if (json.layers[i].data[t] > 0)
308+
if (gid > 0)
257309
{
258-
row.push(new Phaser.Tile(layer, json.layers[i].data[t], x, output.length, json.tilewidth, json.tileheight));
310+
row.push(new Phaser.Tile(layer, gid, x, output.length, json.tilewidth, json.tileheight));
311+
row[row.length - 1].rotation = rotation;
312+
row[row.length - 1].flipped = flipped;
259313
}
260314
else
261315
{

0 commit comments

Comments
 (0)