Tidying: making it easier to access collision objects from a tile

This commit is contained in:
Michael Hadley 2017-12-01 18:08:58 -06:00
parent 806c9d3237
commit 8adce2ed2e
4 changed files with 26 additions and 3 deletions

View file

@ -139,6 +139,14 @@ var Tile = new Class({
*/
this.collideDown = false;
/**
* An array of collision objects from Tiled. These are defined on the tileset. Each tile
* just gets a references to the objects stored in the tileset, so this is readonly.
* @property {array|null} collisionObjects
* @readonly
*/
this.collisionObjects = null;
/**
* Whether the tile's left edge is interesting for collisions.
* @property {boolean} faceLeft

View file

@ -1,4 +1,5 @@
var Extend = require('../../../../utils/object/Extend');
var GetValue = require('../../../../utils/object/GetValue');
// Copy properties from tileset to tiles
var AssignTileProperties = function (mapData)
@ -46,6 +47,17 @@ var AssignTileProperties = function (mapData)
tile.properties, set.tileProperties[tile.index - set.firstgid]
);
}
// If that tile type has any collision info, add that to the tile object
if (set.tileData && set.tileData[tile.index - set.firstgid])
{
var tileData = set.tileData[tile.index - set.firstgid];
var objects = GetValue(tileData, 'objectgroup.objects', null);
if (objects && objects.length > 0)
{
tile.collisionObjects = objects;
}
}
}
}
}

View file

@ -2,7 +2,7 @@ var GetFastValue = require('../../../../utils/object/GetFastValue');
var ParseObject = require('./ParseObject');
// Objects & Collision Data (polylines, etc)
var ParseTilesets = function (json)
var ParseObjectLayers = function (json)
{
var objects = {};
var collision = {};
@ -36,4 +36,4 @@ var ParseTilesets = function (json)
return { objects: objects, collision: collision };
};
module.exports = ParseTilesets;
module.exports = ParseObjectLayers;

View file

@ -40,7 +40,10 @@ var ParseTilesets = function (json)
var objectGroup = newSet.tileData[stringID].objectgroup;
if (objectGroup && objectGroup.objects)
{
objectGroup.objects = objectGroup.objects.map(ParseObject);
var parsedObjects = objectGroup.objects.map(
function (obj) { return ParseObject(obj); }
);
newSet.tileData[stringID].objectgroup.objects = parsedObjects;
}
}
}