Skip to content

Commit 37767eb

Browse files
committed
Tidy: fix v2 linting errors and move Tiled parser bits into modules
1 parent ed5cdf0 commit 37767eb

7 files changed

Lines changed: 376 additions & 313 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var Extend = require('../../../../utils/object/Extend');
2+
3+
// Copy properties from tileset to tiles
4+
var AssignTileProperties = function (mapData)
5+
{
6+
var layerData;
7+
var tile;
8+
var sid;
9+
var set;
10+
var row;
11+
12+
// go through each of the map data layers
13+
for (var i = 0; i < mapData.layers.length; i++)
14+
{
15+
layerData = mapData.layers[i];
16+
17+
set = null;
18+
19+
// rows of tiles
20+
for (var j = 0; j < layerData.data.length; j++)
21+
{
22+
row = layerData.data[j];
23+
24+
// individual tiles
25+
for (var k = 0; k < row.length; k++)
26+
{
27+
tile = row[k];
28+
29+
if (tile === null || tile.index < 0)
30+
{
31+
continue;
32+
}
33+
34+
// find the relevant tileset
35+
sid = mapData.tiles[tile.index][2];
36+
set = mapData.tilesets[sid];
37+
38+
// Ensure that a tile's size matches its tileset
39+
tile.width = set.tileWidth;
40+
tile.height = set.tileHeight;
41+
42+
// if that tile type has any properties, add them to the tile object
43+
if (set.tileProperties && set.tileProperties[tile.index - set.firstgid])
44+
{
45+
tile.properties = Extend(
46+
tile.properties, set.tileProperties[tile.index - set.firstgid]
47+
);
48+
}
49+
}
50+
}
51+
}
52+
};
53+
54+
module.exports = AssignTileProperties;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Master list of tiles -> x, y, index in tileset
2+
var BuildTilesetIndex = function (mapData)
3+
{
4+
var tiles = [];
5+
6+
for (var i = 0; i < mapData.tilesets.length; i++)
7+
{
8+
var set = mapData.tilesets[i];
9+
10+
var x = set.tileMargin;
11+
var y = set.tileMargin;
12+
13+
var count = 0;
14+
var countX = 0;
15+
var countY = 0;
16+
17+
for (var t = set.firstgid; t < set.firstgid + set.total; t++)
18+
{
19+
// Can add extra properties here as needed
20+
tiles[t] = [ x, y, i ];
21+
22+
x += set.tileWidth + set.tileSpacing;
23+
24+
count++;
25+
26+
if (count === set.total)
27+
{
28+
break;
29+
}
30+
31+
countX++;
32+
33+
if (countX === set.columns)
34+
{
35+
x = set.tileMargin;
36+
y += set.tileHeight + set.tileSpacing;
37+
38+
countX = 0;
39+
countY++;
40+
41+
if (countY === set.rows)
42+
{
43+
break;
44+
}
45+
}
46+
}
47+
}
48+
49+
return tiles;
50+
};
51+
52+
module.exports = BuildTilesetIndex;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var GetFastValue = require('../../../../utils/object/GetFastValue');
2+
3+
var ParseImageLayers = function (json)
4+
{
5+
var images = [];
6+
7+
for (var i = 0; i < json.layers.length; i++)
8+
{
9+
if (json.layers[i].type !== 'imagelayer')
10+
{
11+
continue;
12+
}
13+
14+
var curi = json.layers[i];
15+
16+
images.push({
17+
name: curi.name,
18+
image: curi.image,
19+
x: GetFastValue(curi, 'offsetx', 0) + curi.x,
20+
y: GetFastValue(curi, 'offsety', 0) + curi.y,
21+
alpha: curi.opacity,
22+
visible: curi.visible,
23+
properties: GetFastValue(curi, 'properties', {})
24+
});
25+
}
26+
27+
return images;
28+
};
29+
30+
module.exports = ParseImageLayers;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var GetFastValue = require('../../../../utils/object/GetFastValue');
2+
var ParseObject = require('./ParseObject');
3+
4+
// Objects & Collision Data (polylines, etc)
5+
var ParseTilesets = function (json)
6+
{
7+
var objects = {};
8+
var collision = {};
9+
10+
for (var i = 0; i < json.layers.length; i++)
11+
{
12+
if (json.layers[i].type !== 'objectgroup')
13+
{
14+
continue;
15+
}
16+
17+
var curo = json.layers[i];
18+
var layerName = curo.name;
19+
var offsetX = GetFastValue(curo, 'offsetx', 0);
20+
var offsetY = GetFastValue(curo, 'offsety', 0);
21+
22+
objects[layerName] = [];
23+
collision[layerName] = [];
24+
25+
for (var j = 0; j < curo.objects.length; j++)
26+
{
27+
var parsedObject = ParseObject(curo.objects[j], offsetX, offsetY);
28+
29+
// Matching v2 where only polylines were added to collision prop of the map
30+
if (parsedObject.polyline) { collision[layerName].push(parsedObject); }
31+
32+
objects[layerName].push(parsedObject);
33+
}
34+
}
35+
36+
return { objects: objects, collision: collision };
37+
};
38+
39+
module.exports = ParseTilesets;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
var Base64Decode = require('./Base64Decode');
2+
var GetFastValue = require('../../../../utils/object/GetFastValue');
3+
var LayerData = require('../../mapdata/LayerData');
4+
var ParseGID = require('./ParseGID');
5+
var Tile = require('../../Tile');
6+
7+
var ParseTileLayers = function (json, insertNull)
8+
{
9+
var tileLayers = [];
10+
11+
for (var i = 0; i < json.layers.length; i++)
12+
{
13+
if (json.layers[i].type !== 'tilelayer')
14+
{
15+
continue;
16+
}
17+
18+
var curl = json.layers[i];
19+
20+
// Base64 decode data if necessary. NOTE: uncompressed base64 only.
21+
if (curl.compression)
22+
{
23+
console.warn(
24+
'TilemapParser.parseTiledJSON - Layer compression is unsupported, skipping layer \''
25+
+ curl.name + '\''
26+
);
27+
continue;
28+
}
29+
else if (curl.encoding && curl.encoding === 'base64')
30+
{
31+
curl.data = Base64Decode(curl.data);
32+
delete curl.encoding; // Allow the same map to be parsed multiple times
33+
}
34+
35+
var layerData = new LayerData({
36+
name: curl.name,
37+
x: GetFastValue(curl, 'offsetx', 0) + curl.x,
38+
y: GetFastValue(curl, 'offsety', 0) + curl.y,
39+
width: curl.width,
40+
height: curl.height,
41+
tileWidth: json.tilewidth,
42+
tileHeight: json.tileheight,
43+
alpha: curl.opacity,
44+
visible: curl.visible,
45+
properties: GetFastValue(curl, 'properties', {})
46+
});
47+
48+
var x = 0;
49+
var row = [];
50+
var output = [];
51+
52+
// Loop through the data field in the JSON.
53+
54+
// This is an array containing the tile indexes, one after the other. -1 = no tile,
55+
// everything else = the tile index (starting at 1 for Tiled, 0 for CSV) If the map
56+
// contains multiple tilesets then the indexes are relative to that which the set starts
57+
// from. Need to set which tileset in the cache = which tileset in the JSON, if you do this
58+
// manually it means you can use the same map data but a new tileset.
59+
60+
for (var t = 0, len = curl.data.length; t < len; t++)
61+
{
62+
var gidInfo = ParseGID(curl.data[t]);
63+
64+
// index, x, y, width, height
65+
if (gidInfo.gid > 0)
66+
{
67+
var tile = new Tile(layerData, gidInfo.gid, x, output.length, json.tilewidth,
68+
json.tileheight);
69+
70+
tile.rotation = gidInfo.rotation;
71+
tile.flipped = gidInfo.flipped;
72+
tile.flippedHorizontal = gidInfo.flippedHorizontal;
73+
tile.flippedVertical = gidInfo.flippedVertical;
74+
tile.flippedAntiDiagonal = gidInfo.flippedAntiDiagonal;
75+
76+
row.push(tile);
77+
}
78+
else
79+
{
80+
var blankTile = insertNull
81+
? null
82+
: new Tile(layerData, -1, x, output.length, json.tilewidth, json.tileheight);
83+
row.push(blankTile);
84+
}
85+
86+
x++;
87+
88+
if (x === curl.width)
89+
{
90+
output.push(row);
91+
x = 0;
92+
row = [];
93+
}
94+
}
95+
96+
layerData.data = output;
97+
98+
tileLayers.push(layerData);
99+
}
100+
101+
return tileLayers;
102+
};
103+
104+
module.exports = ParseTileLayers;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
var Tileset = require('../../Tileset');
2+
var ImageCollection = require('../../ImageCollection');
3+
var ParseObject = require('./ParseObject');
4+
5+
// Tilesets & Image Collections
6+
var ParseTilesets = function (json)
7+
{
8+
var tilesets = [];
9+
var imageCollections = [];
10+
var lastSet = null;
11+
var stringID;
12+
13+
for (var i = 0; i < json.tilesets.length; i++)
14+
{
15+
// name, firstgid, width, height, margin, spacing, properties
16+
var set = json.tilesets[i];
17+
18+
if (set.source)
19+
{
20+
console.warn('Phaser can\'t load external tilesets. Use the Embed Tileset button and then export the map again.');
21+
}
22+
else if (set.image)
23+
{
24+
var newSet = new Tileset(set.name, set.firstgid, set.tilewidth, set.tileheight, set.margin, set.spacing);
25+
26+
// Properties stored per-tile in object with string indexes starting at "0"
27+
if (set.tileproperties)
28+
{
29+
newSet.tileProperties = set.tileproperties;
30+
}
31+
32+
// Object & terrain shapes stored per-tile in object with string indexes starting at "0"
33+
if (set.tiles)
34+
{
35+
newSet.tileData = set.tiles;
36+
37+
// Parse the objects into Phaser format to match handling of other Tiled objects
38+
for (stringID in newSet.tileData)
39+
{
40+
var objectGroup = newSet.tileData[stringID].objectgroup;
41+
if (objectGroup && objectGroup.objects)
42+
{
43+
objectGroup.objects = objectGroup.objects.map(ParseObject);
44+
}
45+
}
46+
}
47+
48+
// For a normal sliced tileset the row/count/size information is computed when updated.
49+
// This is done (again) after the image is set.
50+
newSet.updateTileData(set.imagewidth, set.imageheight);
51+
52+
tilesets.push(newSet);
53+
}
54+
else
55+
{
56+
var newCollection = new ImageCollection(set.name, set.firstgid, set.tilewidth,
57+
set.tileheight, set.margin, set.spacing, set.properties);
58+
59+
for (stringID in set.tiles)
60+
{
61+
var image = set.tiles[stringID].image;
62+
var gid = set.firstgid + parseInt(stringID, 10);
63+
newCollection.addImage(gid, image);
64+
}
65+
66+
imageCollections.push(newCollection);
67+
}
68+
69+
// We've got a new Tileset, so set the lastgid into the previous one
70+
if (lastSet)
71+
{
72+
lastSet.lastgid = set.firstgid - 1;
73+
}
74+
75+
lastSet = set;
76+
}
77+
78+
return { tilesets: tilesets, imageCollections: imageCollections };
79+
};
80+
81+
module.exports = ParseTilesets;

0 commit comments

Comments
 (0)