/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011 Zynga Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
/**
Orthogonal orientation
* @constant
* @type Number
*/
cc.TMX_ORIENTATION_ORTHO = 0;
/**
* Hexagonal orientation
* @constant
* @type Number
*/
cc.TMX_ORIENTATION_HEX = 1;
/**
* Isometric orientation
* @constant
* @type Number
*/
cc.TMX_ORIENTATION_ISO = 2;
/**
*
cc.TMXTiledMap knows how to parse and render a TMX map.
*
* It adds support for the TMX tiled map format used by http://www.mapeditor.org
* It supports isometric, hexagonal and orthogonal tiles.
* It also supports object groups, objects, and properties.
*
* Features:
* - Each tile will be treated as an cc.Sprite
* - The sprites are created on demand. They will be created only when you call "layer.getTileAt(position)"
* - Each tile can be rotated / moved / scaled / tinted / "opacitied", since each tile is a cc.Sprite
* - Tiles can be added/removed in runtime
* - The z-order of the tiles can be modified in runtime
* - Each tile has an anchorPoint of (0,0)
* - The anchorPoint of the TMXTileMap is (0,0)
* - The TMX layers will be added as a child
* - The TMX layers will be aliased by default
* - The tileset image will be loaded using the cc.TextureCache
* - Each tile will have a unique tag
* - Each tile will have a unique z value. top-left: z=1, bottom-right: z=max z
* - Each object group will be treated as an cc.MutableArray
* - Object class which will contain all the properties in a dictionary
* - Properties can be assigned to the Map, Layer, Object Group, and Object
*
* Limitations:
* - It only supports one tileset per layer.
* - Embeded images are not supported
* - It only supports the XML format (the JSON format is not supported)
*
* Technical description:
* Each layer is created using an cc.TMXLayer (subclass of cc.SpriteBatchNode). If you have 5 layers, then 5 cc.TMXLayer will be created,
* unless the layer visibility is off. In that case, the layer won't be created at all.
* You can obtain the layers (cc.TMXLayer objects) at runtime by:
* - map.getChildByTag(tag_number); // 0=1st layer, 1=2nd layer, 2=3rd layer, etc...
* - map.getLayer(name_of_the_layer);
*
* Each object group is created using a cc.TMXObjectGroup which is a subclass of cc.MutableArray.
* You can obtain the object groups at runtime by:
* - map.getObjectGroup(name_of_the_object_group);
*
* Each object is a cc.TMXObject.
*
* Each property is stored as a key-value pair in an cc.MutableDictionary.
* You can obtain the properties at runtime by:
*
* map.getProperty(name_of_the_property);
* layer.getProperty(name_of_the_property);
* objectGroup.getProperty(name_of_the_property);
* object.getProperty(name_of_the_property);
* @class
* @extends cc.Node
*/
cc.TMXTiledMap = cc.NodeRGBA.extend(/** @lends cc.TMXTiledMap# */{
//the map's size property measured in tiles
_mapSize:null,
_tileSize:null,
_properties:null,
_objectGroups:null,
_mapOrientation:null,
//tile properties
_tileProperties:null,
ctor:function(){
cc.Node.prototype.ctor.call(this);
this._mapSize = cc.SizeZero();
this._tileSize = cc.SizeZero();
this._properties = null;
this._objectGroups = null;
this._mapOrientation = null;
this._tileProperties = null;
},
/**
* @return {cc.Size}
*/
getMapSize:function () {
return cc.size(this._mapSize.width, this._mapSize.height);
},
/**
* @param {cc.Size} Var
*/
setMapSize:function (Var) {
this._mapSize.width = Var.width;
this._mapSize.height = Var.height;
},
/**
* @return {cc.Size}
*/
getTileSize:function () {
return cc.size(this._tileSize.width, this._tileSize.height);
},
/**
* @param {cc.Size} Var
*/
setTileSize:function (Var) {
this._tileSize.width = Var.width;
this._tileSize.height = Var.height;
},
/**
* map orientation
* @return {Number}
*/
getMapOrientation:function () {
return this._mapOrientation;
},
/**
* @param {Number} Var
*/
setMapOrientation:function (Var) {
this._mapOrientation = Var;
},
/**
* object groups
* @return {Array}
*/
getObjectGroups:function () {
return this._objectGroups;
},
/**
* @param {Array} Var
*/
setObjectGroups:function (Var) {
this._objectGroups = Var;
},
/**
* properties
* @return {object}
*/
getProperties:function () {
return this._properties;
},
/**
* @param {object} Var
*/
setProperties:function (Var) {
this._properties = Var;
},
/**
* @param {String} tmxFile
* @param {String} [resourcePath=]
* @return {Boolean}
* @example
* //example
* var map = new cc.TMXTiledMap()
* map.initWithTMXFile("hello.tmx");
*/
initWithTMXFile:function (tmxFile,resourcePath) {
if(!tmxFile || tmxFile.length == 0)
throw "cc.TMXTiledMap.initWithTMXFile(): tmxFile should be non-null or non-empty string.";
this.setContentSize(0, 0);
var mapInfo = cc.TMXMapInfo.create(tmxFile,resourcePath);
if (!mapInfo)
return false;
var locTilesets = mapInfo.getTilesets();
if(!locTilesets || locTilesets.length === 0)
cc.log("cc.TMXTiledMap.initWithTMXFile(): Map not found. Please check the filename.");
this._buildWithMapInfo(mapInfo);
return true;
},
initWithXML:function(tmxString, resourcePath){
this.setContentSize(0, 0);
var mapInfo = cc.TMXMapInfo.createWithXML(tmxString, resourcePath);
var locTilesets = mapInfo.getTilesets();
if(!locTilesets || locTilesets.length === 0)
cc.log("cc.TMXTiledMap.initWithXML(): Map not found. Please check the filename.");
this._buildWithMapInfo(mapInfo);
return true;
},
_buildWithMapInfo:function (mapInfo) {
this._mapSize = mapInfo.getMapSize();
this._tileSize = mapInfo.getTileSize();
this._mapOrientation = mapInfo.getOrientation();
this._objectGroups = mapInfo.getObjectGroups();
this._properties = mapInfo.getProperties();
this._tileProperties = mapInfo.getTileProperties();
var idx = 0;
var layers = mapInfo.getLayers();
if (layers) {
var layerInfo = null;
for (var i = 0, len = layers.length; i < len; i++) {
layerInfo = layers[i];
if (layerInfo && layerInfo.visible) {
var child = this._parseLayer(layerInfo, mapInfo);
this.addChild(child, idx, idx);
// update content size with the max size
var childSize = child.getContentSize();
var currentSize = this.getContentSize();
this.setContentSize(cc.size(Math.max(currentSize.width, childSize.width), Math.max(currentSize.height, childSize.height)));
idx++;
}
}
}
},
allLayers: function () {
var retArr = [], locChildren = this._children;
for(var i = 0, len = locChildren.length;i< len;i++){
var layer = locChildren[i];
if(layer && layer instanceof cc.TMXLayer)
retArr.push(layer);
}
return retArr;
},
/**
* return the TMXLayer for the specific layer
* @param {String} layerName
* @return {cc.TMXLayer}
*/
getLayer:function (layerName) {
if(!layerName || layerName.length === 0)
throw "cc.TMXTiledMap.getLayer(): layerName should be non-null or non-empty string.";
var locChildren = this._children;
for (var i = 0; i < locChildren.length; i++) {
var layer = locChildren[i];
if (layer && layer.getLayerName() == layerName)
return layer;
}
// layer not found
return null;
},
/**
* Return the TMXObjectGroup for the specific group
* @param {String} groupName
* @return {cc.TMXObjectGroup}
*/
getObjectGroup:function (groupName) {
if(!groupName || groupName.length === 0)
throw "cc.TMXTiledMap.getObjectGroup(): groupName should be non-null or non-empty string.";
if (this._objectGroups) {
for (var i = 0; i < this._objectGroups.length; i++) {
var objectGroup = this._objectGroups[i];
if (objectGroup && objectGroup.getGroupName() == groupName) {
return objectGroup;
}
}
}
// objectGroup not found
return null;
},
/**
* Return the value for the specific property name
* @param {String} propertyName
* @return {String}
*/
getProperty:function (propertyName) {
return this._properties[propertyName.toString()];
},
/**
* Return properties dictionary for tile GID
* @param {Number} GID
* @return {object}
*/
propertiesForGID:function (GID) {
return this._tileProperties[GID];
},
_parseLayer:function (layerInfo, mapInfo) {
var tileset = this._tilesetForLayer(layerInfo, mapInfo);
var layer = cc.TMXLayer.create(tileset, layerInfo, mapInfo);
// tell the layerinfo to release the ownership of the tiles map.
layerInfo.ownTiles = false;
layer.setupTiles();
return layer;
},
_tilesetForLayer:function (layerInfo, mapInfo) {
var size = layerInfo._layerSize;
var tilesets = mapInfo.getTilesets();
if (tilesets) {
for (var i = tilesets.length - 1; i >= 0; i--) {
var tileset = tilesets[i];
if (tileset) {
for (var y = 0; y < size.height; y++) {
for (var x = 0; x < size.width; x++) {
var pos = x + size.width * y;
var gid = layerInfo._tiles[pos];
if (gid != 0) {
// Optimization: quick return
// if the layer is invalid (more than 1 tileset per layer) an cc.Assert will be thrown later
if (((gid & cc.TMX_TILE_FLIPPED_MASK)>>>0) >= tileset.firstGid) {
return tileset;
}
}
}
}
}
}
}
// If all the tiles are 0, return empty tileset
cc.log("cocos2d: Warning: TMX Layer " + layerInfo.name + " has no tiles");
return null;
}
});
/**
* Creates a TMX Tiled Map with a TMX file.
* Implementation cc.TMXTiledMap
* @param {String} tmxFile
* @param {String} resourcePath
* @return {cc.TMXTiledMap|undefined}
* @example
* //example
* var map = cc.TMXTiledMap.create("hello.tmx");
*/
cc.TMXTiledMap.create = function (tmxFile, resourcePath) {
var ret = new cc.TMXTiledMap();
if (ret.initWithTMXFile(tmxFile,resourcePath)) {
return ret;
}
return null;
};
/**
* initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources
* @param {String} tmxString
* @param {String} resourcePath
* @return {cc.TMXTiledMap|undefined}
*/
cc.TMXTiledMap.createWithXML = function(tmxString, resourcePath){
var tileMap = new cc.TMXTiledMap();
if(tileMap.initWithXML(tmxString,resourcePath))
return tileMap;
return null;
};