/*global FM*/
/**
* Object representing a tile map generated by Tiled Map Editor.
* Parses the .tmx file content and facilitates the access to its data.
* @class TmxMap
* @constructor
* @author Simon Chauvin
*/
FM.TmxMap = function () {
"use strict";
/**
* The map.
* @type XMLNode
* @private
*/
this.map = null;
/**
* Version number of the tile map.
* @type string
* @public
*/
this.version = "unknown";
/**
* Whether the tile map is orthogonal or isometric.
* @type string
* @public
*/
this.orientation = "orthogonal";
/**
* Number of tiles along the x-axis.
* @type int
* @public
*/
this.width = 0;
/**
* Number of tiles along the y-axis.
* @type int
* @public
*/
this.height = 0;
/**
* Width of a tile.
* @type int
* @public
*/
this.tileWidth = 0;
/**
* Height of a tile.
* @type int
* @public
*/
this.tileHeight = 0;
/**
* Custom properties of the map.
* @type FM.TmxPropertySet
* @public
*/
this.properties = null;
/**
* The different layers of the map.
* @type Array
* @public
*/
this.layers = [];
/**
* The different tile sets of the tile map.
* @type Array
* @public
*/
this.tileSets = [];
/**
* The different groups of objects defined in the tile map.
* @type Array
* @public
*/
this.objectGroups = [];
};
FM.TmxMap.prototype.constructor = FM.TmxMap;
/**
* Load the tile map and create javascript objects.
* @method FM.TmxMap#load
* @memberOf FM.TmxMap
* @param {string} pSource The string of the .tmx file content.
*/
FM.TmxMap.prototype.load = function (pSource) {
"use strict";
var xmlDoc, parser;
if (window.DOMParser) {
parser = new DOMParser();
xmlDoc = parser.parseFromString(pSource, "text/xml");
} else {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(pSource);
}
this.map = xmlDoc.getElementsByTagName("map")[0];
this.version = this.map.getAttribute("version") || "unknown";
this.orientation = this.map.getAttribute("orientation") || "orthogonal";
this.width = parseInt(this.map.getAttribute("width"));
this.height = parseInt(this.map.getAttribute("height"));
this.tileWidth = parseInt(this.map.getAttribute("tilewidth"));
this.tileHeight = parseInt(this.map.getAttribute("tileheight"));
var properties = this.map.getElementsByTagName("properties")[0],
tileSets = this.map.getElementsByTagName("tileset"),
layers = this.map.getElementsByTagName("layer"),
objectGroups = this.map.getElementsByTagName("objectgroup"),
property,
tileSet,
layer,
objectGroup,
i;
//Load properties
if (properties) {
for (i = 0; i < properties.childNodes.length; i = i + 1) {
if (properties.hasChildNodes() === true) {
property = properties.childNodes[i];
if (property.nodeType === 1) {
if (this.properties) {
this.properties.add(property);
} else {
this.properties = new FM.TmxPropertySet();
this.properties.add(property);
}
}
}
}
}
//Load tilesets
if (tileSets) {
for (i = 0; i < tileSets.length; i = i + 1) {
tileSet = tileSets[i];
this.tileSets[tileSet.getAttribute("name")] = new FM.TmxTileSet();
this.tileSets[tileSet.getAttribute("name")].load(tileSet, this);
}
}
//Load layer
if (layers) {
for (i = 0; i < layers.length; i = i + 1) {
layer = layers[i];
this.layers[layer.getAttribute("name")] = new FM.TmxLayer();
this.layers[layer.getAttribute("name")].load(layer, this);
}
}
//Load object group
if (objectGroups) {
for (i = 0; i < objectGroups.length; i = i + 1) {
objectGroup = objectGroups[i];
this.objectGroups[objectGroup.getAttribute("name")] = new FM.TmxObjectGroup();
this.objectGroups[objectGroup.getAttribute("name")].load(objectGroup, this);
}
}
};
/**
* Retrieve a tile set from its name.
* @method FM.TmxMap#getTileSet
* @memberOf FM.TmxMap
* @param {string} pName The name of the tile set to retrieve.
* @return {FM.TmxTileSet} Tile set corresponding to the given name.
*/
FM.TmxMap.prototype.getTileSet = function (pName) {
"use strict";
return this.tileSets[pName];
};
/**
* Retrieve a layer from its name.
* @method FM.TmxMap#getLayer
* @memberOf FM.TmxMap
* @param {string} pName Name of the layer to retrieve.
* @returns {FM.TmxLayer} Layer corresponding of the specified name.
*/
FM.TmxMap.prototype.getLayer = function (pName) {
"use strict";
return this.layers[pName];
};
/**
* Retrieve an object group from its name.
* @method FM.TmxMap#getObjectGroup
* @memberOf FM.TmxMap
* @param {string} pName Name of the group to retrieve.
* @returns {FM.TmxGroup} Group of objects matching the given name.
*/
FM.TmxMap.prototype.getObjectGroup = function (pName) {
"use strict";
return this.objectGroups[pName];
};
/**
* Retrieve the tile set matching the specified id.
* Works only after TmxTileSet has been initialized with an image.
* @method FM.TmxMap#getGidOwner
* @memberOf FM.TmxMap
* @param {int} pGid The gid of the tileset to retrieve.
* @returns {FM.TmxTileSet} The tile set matching the gid provided, or null if none is found.
*/
FM.TmxMap.prototype.getGidOwner = function (pGid) {
"use strict";
var tileSet = null;
for (tileSet in this.tileSets) {
if (tileSet.hasGid(pGid)) {
return tileSet;
}
}
return null;
};