Skip to content

Commit 016c572

Browse files
committed
Added Support for Infinite Tiled Maps.
1 parent 173bbeb commit 016c572

3 files changed

Lines changed: 140 additions & 45 deletions

File tree

src/tilemaps/mapdata/MapData.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ var MapData = new Class({
5555
*/
5656
this.height = GetFastValue(config, 'height', 0);
5757

58+
/**
59+
* [description]
60+
*
61+
* @name Phaser.Tilemaps.MapData#infinite
62+
* @type {boolean}
63+
* @since 3.0.0
64+
*/
65+
this.infinite = GetFastValue(config, 'infinite', false);
66+
5867
/**
5968
* [description]
6069
*

src/tilemaps/parsers/tiled/ParseJSONTiled.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var ParseTilesets = require('./ParseTilesets');
1212
var ParseObjectLayers = require('./ParseObjectLayers');
1313
var BuildTilesetIndex = require('./BuildTilesetIndex');
1414
var AssignTileProperties = require('./AssignTileProperties');
15+
var GetFastValue = require('../../../utils/object/GetFastValue');
1516

1617
/**
1718
* @namespace Phaser.Tilemaps.Parsers.Tiled
@@ -52,7 +53,8 @@ var ParseJSONTiled = function (name, json, insertNull)
5253
orientation: json.orientation,
5354
format: Formats.TILED_JSON,
5455
version: json.version,
55-
properties: json.properties
56+
properties: json.properties,
57+
infinite: GetFastValue(json, 'infinite', false)
5658
});
5759

5860
mapData.layers = ParseTileLayers(json, insertNull);

src/tilemaps/parsers/tiled/ParseTileLayers.js

Lines changed: 128 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var Tile = require('../../Tile');
2323
*/
2424
var ParseTileLayers = function (json, insertNull)
2525
{
26+
var infiniteMap = GetFastValue(json, 'infinite', false);
2627
var tileLayers = [];
2728

2829
for (var i = 0; i < json.layers.length; i++)
@@ -49,63 +50,146 @@ var ParseTileLayers = function (json, insertNull)
4950
delete curl.encoding; // Allow the same map to be parsed multiple times
5051
}
5152

52-
var layerData = new LayerData({
53-
name: curl.name,
54-
x: GetFastValue(curl, 'offsetx', 0) + curl.x,
55-
y: GetFastValue(curl, 'offsety', 0) + curl.y,
56-
width: curl.width,
57-
height: curl.height,
58-
tileWidth: json.tilewidth,
59-
tileHeight: json.tileheight,
60-
alpha: curl.opacity,
61-
visible: curl.visible,
62-
properties: GetFastValue(curl, 'properties', {})
63-
});
64-
65-
var x = 0;
66-
var row = [];
67-
var output = [];
68-
69-
// Loop through the data field in the JSON.
70-
7153
// This is an array containing the tile indexes, one after the other. -1 = no tile,
7254
// everything else = the tile index (starting at 1 for Tiled, 0 for CSV) If the map
7355
// contains multiple tilesets then the indexes are relative to that which the set starts
7456
// from. Need to set which tileset in the cache = which tileset in the JSON, if you do this
7557
// manually it means you can use the same map data but a new tileset.
7658

77-
for (var t = 0, len = curl.data.length; t < len; t++)
78-
{
79-
var gidInfo = ParseGID(curl.data[t]);
59+
var layerData;
60+
var gidInfo;
61+
var tile;
62+
var blankTile;
8063

81-
// index, x, y, width, height
82-
if (gidInfo.gid > 0)
83-
{
84-
var tile = new Tile(layerData, gidInfo.gid, x, output.length, json.tilewidth,
85-
json.tileheight);
64+
var output = [];
65+
var x = 0;
8666

87-
// Turning Tiled's FlippedHorizontal, FlippedVertical and FlippedAntiDiagonal
88-
// propeties into flipX, flipY and rotation
89-
tile.rotation = gidInfo.rotation;
90-
tile.flipX = gidInfo.flipped;
67+
if(infiniteMap)
68+
{
69+
var layerOffsetX = GetFastValue(curl, 'startx', 0) + curl.x;
70+
var layerOffsetY = GetFastValue(curl, 'starty', 0) + curl.y;
71+
layerData = new LayerData({
72+
name: curl.name,
73+
x: layerOffsetX,
74+
y: layerOffsetY,
75+
width: curl.width,
76+
height: curl.height,
77+
tileWidth: json.tilewidth,
78+
tileHeight: json.tileheight,
79+
alpha: curl.opacity,
80+
visible: curl.visible,
81+
properties: GetFastValue(curl, 'properties', {})
82+
});
83+
84+
for (var c = 0; c < curl.height; c++)
85+
{
86+
output.push([ null ]);
9187

92-
row.push(tile);
88+
for (var j = 0; j < curl.width; j++)
89+
{
90+
output[c][j] = null;
91+
}
9392
}
94-
else
93+
94+
for (c = 0, len = curl.chunks.length; c < len; c++)
9595
{
96-
var blankTile = insertNull
97-
? null
98-
: new Tile(layerData, -1, x, output.length, json.tilewidth, json.tileheight);
99-
row.push(blankTile);
96+
var chunk = curl.chunks[c];
97+
98+
var offsetX = (chunk.x - layerOffsetX);
99+
var offsetY = (chunk.y - layerOffsetY);
100+
101+
var y = 0;
102+
103+
for (var t = 0, len2 = chunk.data.length; t < len2; t++)
104+
{
105+
var newOffsetX = x + offsetX;
106+
var newOffsetY = y + offsetY;
107+
108+
gidInfo = ParseGID(chunk.data[t]);
109+
110+
// index, x, y, width, height
111+
if (gidInfo.gid > 0)
112+
{
113+
tile = new Tile(layerData, gidInfo.gid, newOffsetX, newOffsetY, json.tilewidth,
114+
json.tileheight);
115+
116+
// Turning Tiled's FlippedHorizontal, FlippedVertical and FlippedAntiDiagonal
117+
// propeties into flipX, flipY and rotation
118+
tile.rotation = gidInfo.rotation;
119+
tile.flipX = gidInfo.flipped;
120+
121+
output[newOffsetY][newOffsetX] = tile;
122+
}
123+
else
124+
{
125+
blankTile = insertNull
126+
? null
127+
: new Tile(layerData, -1, newOffsetX, newOffsetY, json.tilewidth, json.tileheight);
128+
129+
output[newOffsetY][newOffsetX] = blankTile;
130+
}
131+
132+
x++;
133+
134+
if(x === chunk.width)
135+
{
136+
y++;
137+
x = 0;
138+
}
139+
}
100140
}
101-
102-
x++;
103-
104-
if (x === curl.width)
141+
}
142+
else
143+
{
144+
layerData = new LayerData({
145+
name: curl.name,
146+
x: GetFastValue(curl, 'offsetx', 0) + curl.x,
147+
y: GetFastValue(curl, 'offsety', 0) + curl.y,
148+
width: curl.width,
149+
height: curl.height,
150+
tileWidth: json.tilewidth,
151+
tileHeight: json.tileheight,
152+
alpha: curl.opacity,
153+
visible: curl.visible,
154+
properties: GetFastValue(curl, 'properties', {})
155+
});
156+
157+
var row = [];
158+
159+
// Loop through the data field in the JSON.
160+
for (var k = 0, len = curl.data.length; k < len; k++)
105161
{
106-
output.push(row);
107-
x = 0;
108-
row = [];
162+
gidInfo = ParseGID(curl.data[k]);
163+
164+
// index, x, y, width, height
165+
if (gidInfo.gid > 0)
166+
{
167+
tile = new Tile(layerData, gidInfo.gid, x, output.length, json.tilewidth,
168+
json.tileheight);
169+
170+
// Turning Tiled's FlippedHorizontal, FlippedVertical and FlippedAntiDiagonal
171+
// propeties into flipX, flipY and rotation
172+
tile.rotation = gidInfo.rotation;
173+
tile.flipX = gidInfo.flipped;
174+
175+
row.push(tile);
176+
}
177+
else
178+
{
179+
blankTile = insertNull
180+
? null
181+
: new Tile(layerData, -1, x, output.length, json.tilewidth, json.tileheight);
182+
row.push(blankTile);
183+
}
184+
185+
x++;
186+
187+
if (x === curl.width)
188+
{
189+
output.push(row);
190+
x = 0;
191+
row = [];
192+
}
109193
}
110194
}
111195

0 commit comments

Comments
 (0)