Skip to content

Commit 0d02f7d

Browse files
committed
Extend Tiled importer to handle offsetx and offsety for the various layers
1 parent d3aa842 commit 0d02f7d

2 files changed

Lines changed: 17 additions & 20 deletions

File tree

v3/src/gameobjects/tilemap/parsers/parsetiledjson/ParseObject.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ var ParseGID = require('./ParseGID');
55
var pointToArray = function (p) { return [ p.x, p.y ]; };
66
var commonObjectProps = [ 'id', 'name', 'type', 'rotation', 'properties', 'visible', 'x', 'y' ];
77

8-
var ParseObject = function (tiledObject)
8+
var ParseObject = function (tiledObject, offsetX, offsetY)
99
{
10+
if (offsetX === undefined) { offsetX = 0; }
11+
if (offsetY === undefined) { offsetY = 0; }
12+
1013
var parsedObject = Pick(tiledObject, commonObjectProps);
1114

15+
parsedObject.x += offsetX;
16+
parsedObject.y += offsetY;
17+
1218
if (tiledObject.gid)
1319
{
1420
// Object tiles

v3/src/gameobjects/tilemap/parsers/parsetiledjson/index.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ var ParseJSONTiled = function (key, json, insertNull)
5555

5656
var layerData = new LayerData({
5757
name: curl.name,
58-
x: curl.x,
59-
y: curl.y,
58+
x: GetFastValue(curl, 'offsetx', 0) + curl.x,
59+
y: GetFastValue(curl, 'offsety', 0) + curl.y,
6060
width: curl.width,
6161
height: curl.height,
6262
tileWidth: json.tilewidth,
@@ -69,7 +69,6 @@ var ParseJSONTiled = function (key, json, insertNull)
6969
var x = 0;
7070
var row = [];
7171
var output = [];
72-
var orientation;
7372
var gid;
7473

7574
// Loop through the data field in the JSON.
@@ -133,25 +132,15 @@ var ParseJSONTiled = function (key, json, insertNull)
133132

134133
var curi = json.layers[i];
135134

136-
var image = {
137-
135+
images.push({
138136
name: curi.name,
139137
image: curi.image,
140-
x: curi.x,
141-
y: curi.y,
138+
x: GetFastValue(curi, 'offsetx', 0) + curi.x,
139+
y: GetFastValue(curi, 'offsety', 0) + curi.y,
142140
alpha: curi.opacity,
143141
visible: curi.visible,
144-
properties: {}
145-
146-
};
147-
148-
if (curi.properties)
149-
{
150-
image.properties = curi.properties;
151-
}
152-
153-
images.push(image);
154-
142+
properties: GetFastValue(curi.properties, {})
143+
});
155144
}
156145

157146
mapData.images = images;
@@ -237,13 +226,15 @@ var ParseJSONTiled = function (key, json, insertNull)
237226

238227
var curo = json.layers[i];
239228
var layerName = curo.name;
229+
var offsetX = GetFastValue(curo, 'offsetx', 0);
230+
var offsetY = GetFastValue(curo, 'offsety', 0);
240231

241232
objects[layerName] = [];
242233
collision[layerName] = [];
243234

244235
for (var j = 0; j < curo.objects.length; j++)
245236
{
246-
var parsedObject = ParseObject(curo.objects[j]);
237+
var parsedObject = ParseObject(curo.objects[j], offsetX, offsetY);
247238

248239
// Matching v2 where only polylines were added to collision prop of the map
249240
if (parsedObject.polyline) { collision[layerName].push(parsedObject); }

0 commit comments

Comments
 (0)