Skip to content

Commit 901b5f8

Browse files
committed
Upgraded Tilemap object layer parser from v2
- Added ObjectLayer class to make the structure of an object layer clear - Tilemap.objects is now an array of objects. This allows object layer properties to be parsed.
1 parent c7c94cd commit 901b5f8

3 files changed

Lines changed: 67 additions & 17 deletions

File tree

src/gameobjects/tilemap/Tilemap.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,6 @@ var Tilemap = new Class({
116116
*/
117117
this.images = mapData.images;
118118

119-
/**
120-
* An array of collision data. Specifically, any polyline objects defined in object layers.
121-
* @property {array} collision
122-
*/
123-
this.collision = mapData.collision; // Note: this probably isn't useful anymore
124-
125119
/**
126120
* @property {LayerData[]} layers - An array of Tilemap layer data.
127121
*/
@@ -134,8 +128,8 @@ var Tilemap = new Class({
134128
this.tilesets = mapData.tilesets;
135129

136130
/**
137-
* An array of Tiled Object Layers.
138-
* @property {array} objects
131+
* An array of ObjectLayer instances parsed from Tiled object layers.
132+
* @property {ObjectLayer[]} objects
139133
*/
140134
this.objects = mapData.objects;
141135

@@ -402,18 +396,20 @@ var Tilemap = new Class({
402396
if (spriteConfig === undefined) { spriteConfig = {}; }
403397
if (scene === undefined) { scene = this.scene; }
404398

405-
if (!this.objects[name])
399+
var objectLayer = this.getObjectLayer(name);
400+
if (!objectLayer)
406401
{
407402
console.warn('Cannot create from object. Invalid objectgroup name given: ' + name);
408403
return;
409404
}
410405

406+
var objects = objectLayer.objects;
411407
var sprites = [];
412408

413-
for (var i = 0; i < this.objects[name].length; i++)
409+
for (var i = 0; i < objects.length; i++)
414410
{
415411
var found = false;
416-
var obj = this.objects[name][i];
412+
var obj = objects[i];
417413

418414
if (obj.gid !== undefined && typeof id === 'number' && obj.gid === id ||
419415
obj.id !== undefined && typeof id === 'number' && obj.id === id ||
@@ -665,6 +661,19 @@ var Tilemap = new Class({
665661
return index !== null ? this.layers[index] : null;
666662
},
667663

664+
/**
665+
* Gets the ObjectLayer from this.objects that has the given `name`, or null if no ObjectLayer
666+
* is found with that name.
667+
*
668+
* @param {string} [name] - The name of the object layer from Tiled.
669+
* @return {ObjectLayer|null} The corresponding ObjectLayer within this.objects or null.
670+
*/
671+
getObjectLayer: function (name)
672+
{
673+
var index = this.getIndex(this.objects, name);
674+
return index !== null ? this.objects[index] : null;
675+
},
676+
668677
/**
669678
* Gets the LayerData index of the given `layer` within this.layers, or null if an invalid
670679
* `layer` is given.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var Class = require('../../../utils/Class');
2+
var GetFastValue = require('../../../utils/object/GetFastValue');
3+
4+
var ObjectLayer = new Class({
5+
6+
initialize:
7+
8+
9+
/**
10+
* A class for representing a Tiled object layer in a map. This mirrors the structure of a Tiled
11+
* object layer, except:
12+
* - "x" & "y" properties are ignored since these cannot be changed in Tiled.
13+
* - "offsetx" & "offsety" are applied to the individual object coordinates directly, so they
14+
* are ignored as well.
15+
* - "draworder" is ignored.
16+
*
17+
* @class ObjectLayer
18+
* @constructor
19+
*
20+
* @param {object} [config] - [description]
21+
*/
22+
function ObjectLayer (config)
23+
{
24+
if (config === undefined) { config = {}; }
25+
26+
this.name = GetFastValue(config, 'name', 'object layer');
27+
this.opacity = GetFastValue(config, 'opacity', 1);
28+
this.properties = GetFastValue(config, 'properties', {});
29+
this.propertyTypes = GetFastValue(config, 'propertytypes', {});
30+
this.type = GetFastValue(config, 'type', 'objectgroup');
31+
this.visible = GetFastValue(config, 'visible', true);
32+
this.objects = GetFastValue(config, 'objects', []);
33+
}
34+
35+
});
36+
37+
module.exports = ObjectLayer;
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
var GetFastValue = require('../../../../utils/object/GetFastValue');
22
var ParseObject = require('./ParseObject');
3+
var ObjectLayer = require('../../mapdata/ObjectLayer');
34

45
var ParseObjectLayers = function (json)
56
{
6-
var objects = {};
7+
var objectLayers = [];
78

89
for (var i = 0; i < json.layers.length; i++)
910
{
@@ -13,21 +14,24 @@ var ParseObjectLayers = function (json)
1314
}
1415

1516
var curo = json.layers[i];
16-
var layerName = curo.name;
1717
var offsetX = GetFastValue(curo, 'offsetx', 0);
1818
var offsetY = GetFastValue(curo, 'offsety', 0);
19-
20-
objects[layerName] = [];
19+
var objects = [];
2120

2221
for (var j = 0; j < curo.objects.length; j++)
2322
{
2423
var parsedObject = ParseObject(curo.objects[j], offsetX, offsetY);
2524

26-
objects[layerName].push(parsedObject);
25+
objects.push(parsedObject);
2726
}
27+
28+
var objectLayer = new ObjectLayer(curo);
29+
objectLayer.objects = objects;
30+
31+
objectLayers.push(objectLayer);
2832
}
2933

30-
return objects;
34+
return objectLayers;
3135
};
3236

3337
module.exports = ParseObjectLayers;

0 commit comments

Comments
 (0)