/****************************************************************************
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.
****************************************************************************/
/**
* @constant
* @type Number
*/
cc.TMX_LAYER_ATTRIB_NONE = 1 << 0;
/**
* @constant
* @type Number
*/
cc.TMX_LAYER_ATTRIB_BASE64 = 1 << 1;
/**
* @constant
* @type Number
*/
cc.TMX_LAYER_ATTRIB_GZIP = 1 << 2;
/**
* @constant
* @type Number
*/
cc.TMX_LAYER_ATTRIB_ZLIB = 1 << 3;
/**
* @constant
* @type Number
*/
cc.TMX_PROPERTY_NONE = 0;
/**
* @constant
* @type Number
*/
cc.TMX_PROPERTY_MAP = 1;
/**
* @constant
* @type Number
*/
cc.TMX_PROPERTY_LAYER = 2;
/**
* @constant
* @type Number
*/
cc.TMX_PROPERTY_OBJECTGROUP = 3;
/**
* @constant
* @type Number
*/
cc.TMX_PROPERTY_OBJECT = 4;
/**
* @constant
* @type Number
*/
cc.TMX_PROPERTY_TILE = 5;
/**
* @constant
* @type Number
*/
cc.TMX_TILE_HORIZONTAL_FLAG = 0x80000000;
/**
* @constant
* @type Number
*/
cc.TMX_TILE_VERTICAL_FLAG = 0x40000000;
/**
* @constant
* @type Number
*/
cc.TMX_TILE_DIAGONAL_FLAG = 0x20000000;
/**
* @constant
* @type Number
*/
cc.TMX_TILE_FLIPPED_ALL = (cc.TMX_TILE_HORIZONTAL_FLAG | cc.TMX_TILE_VERTICAL_FLAG | cc.TMX_TILE_DIAGONAL_FLAG) >>> 0;
/**
* @constant
* @type Number
*/
cc.TMX_TILE_FLIPPED_MASK = (~(cc.TMX_TILE_FLIPPED_ALL)) >>> 0;
// Bits on the far end of the 32-bit global tile ID (GID's) are used for tile flags
/**
*
cc.TMXLayerInfo contains the information about the layers like:
* - Layer name
* - Layer size
* - Layer opacity at creation time (it can be modified at runtime)
* - Whether the layer is visible (if it's not visible, then the CocosNode won't be created)
*
* This information is obtained from the TMX file.
* @class
* @extends cc.Class
*/
cc.TMXLayerInfo = cc.Class.extend(/** @lends cc.TMXLayerInfo# */{
_properties:null,
name:"",
_layerSize:null,
_tiles:null,
visible:null,
_opacity:null,
ownTiles:true,
_minGID:100000,
_maxGID:0,
offset:null,
ctor:function () {
this._properties = [];
this.name = "";
this._layerSize = null;
this._tiles = [];
this.visible = true;
this._opacity = 0;
this.ownTiles = true;
this._minGID = 100000;
this._maxGID = 0;
this.offset = cc.PointZero();
},
/**
* @return {Array}
*/
getProperties:function () {
return this._properties;
},
/**
* @param {object} Var
*/
setProperties:function (Var) {
this._properties = Var;
}
});
/**
* cc.TMXTilesetInfo contains the information about the tilesets like:
* - Tileset name
* - Tileset spacing
* - Tileset margin
* - size of the tiles
* - Image used for the tiles
* - Image size
*
* This information is obtained from the TMX file.
* @class
* @extends cc.Class
*/
cc.TMXTilesetInfo = cc.Class.extend(/** @lends cc.TMXTilesetInfo# */{
/**
* Tileset name
*/
name:"",
/**
* First grid
*/
firstGid:0,
_tileSize:null,
/**
* Spacing
*/
spacing:0,
/**
* Margin
*/
margin:0,
/**
* Filename containing the tiles (should be sprite sheet / texture atlas)
*/
sourceImage:"",
/**
* Size in pixels of the image
*/
imageSize:null,
ctor:function () {
this._tileSize = cc.SizeZero();
this.imageSize = cc.SizeZero();
},
/**
* @param {Number} gid
* @return {cc.Rect}
*/
rectForGID:function (gid) {
var rect = cc.RectZero();
rect._size = this._tileSize;
gid &= cc.TMX_TILE_FLIPPED_MASK;
gid = gid - parseInt(this.firstGid, 10);
var max_x = parseInt((this.imageSize.width - this.margin * 2 + this.spacing) / (this._tileSize.width + this.spacing), 10);
rect._origin.x = parseInt((gid % max_x) * (this._tileSize.width + this.spacing) + this.margin, 10);
rect._origin.y = parseInt(parseInt(gid / max_x, 10) * (this._tileSize.height + this.spacing) + this.margin, 10);
return rect;
}
});
/**
* cc.TMXMapInfo contains the information about the map like:
*- Map orientation (hexagonal, isometric or orthogonal)
*- Tile size
*- Map size
*
* And it also contains:
* - Layers (an array of TMXLayerInfo objects)
* - Tilesets (an array of TMXTilesetInfo objects)
* - ObjectGroups (an array of TMXObjectGroupInfo objects)
*
* This information is obtained from the TMX file.
* @class
* @extends cc.SAXParser
*/
cc.TMXMapInfo = cc.SAXParser.extend(/** @lends cc.TMXMapInfo# */{
// map orientation
_orientation:null,
_mapSize:null,
_tileSize:null,
_layers:null,
_tileSets:null,
_objectGroups:null,
_parentElement:null,
_parentGID:null,
_layerAttribs:0,
_storingCharacters:false,
_properties:null,
// tmx filename
_TMXFileName:null,
//current string
_currentString:null,
// tile properties
_tileProperties:null,
_resources:"",
_currentFirstGID:0,
ctor:function () {
this._mapSize = cc.SizeZero();
this._tileSize = cc.SizeZero();
this._layers = [];
this._tileSets = [];
this._objectGroups = [];
this._properties = [];
this._tileProperties = {};
this._currentFirstGID = 0;
},
/**
* @return {Number}
*/
getOrientation:function () {
return this._orientation;
},
/**
* @param {Number} Var
*/
setOrientation:function (Var) {
this._orientation = Var;
},
/**
* Map width & height
* @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;
},
/**
* Tiles width & 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;
},
/**
* Layers
* @return {Array}
*/
getLayers:function () {
return this._layers;
},
/**
* @param {cc.TMXLayerInfo} Var
*/
setLayers:function (Var) {
this._layers.push(Var);
},
/**
* tilesets
* @return {Array}
*/
getTilesets:function () {
return this._tileSets;
},
/**
* @param {cc.TMXTilesetInfo} Var
*/
setTilesets:function (Var) {
this._tileSets.push(Var);
},
/**
* ObjectGroups
* @return {Array}
*/
getObjectGroups:function () {
return this._objectGroups;
},
/**
* @param {cc.TMXObjectGroup} Var
*/
setObjectGroups:function (Var) {
this._objectGroups.push(Var);
},
/**
* parent element
* @return {Number}
*/
getParentElement:function () {
return this._parentElement;
},
/**
* @param {Number} Var
*/
setParentElement:function (Var) {
this._parentElement = Var;
},
/**
* parent GID
* @return {Number}
*/
getParentGID:function () {
return this._parentGID;
},
/**
* @param {Number} Var
*/
setParentGID:function (Var) {
this._parentGID = Var;
},
/**
* layer attribute
* @return {Number}
*/
getLayerAttribs:function () {
return this._layerAttribs;
},
/**
* @param {Number} Var
*/
setLayerAttribs:function (Var) {
this._layerAttribs = Var;
},
/**
* is string characters?
* @return {Boolean}
*/
getStoringCharacters:function () {
return this._storingCharacters;
},
/**
* @param {Boolean} Var
*/
setStoringCharacters:function (Var) {
this._storingCharacters = Var;
},
/**
* Properties
* @return {Array}
*/
getProperties:function () {
return this._properties;
},
/**
* @param {object} Var
*/
setProperties:function (Var) {
this._properties = Var;
},
/**
* Initializes a TMX format with a tmx file
* @param {String} tmxFile
* @param {String} resourcePath
* @return {Element}
*/
initWithTMXFile:function (tmxFile, resourcePath) {
this._internalInit(tmxFile, resourcePath);
return this.parseXMLFile(this._TMXFileName);
//return this.parseXMLFile(cc.FileUtils.getInstance().fullPathForFilename(this._TMXFileName));
},
/**
* initializes a TMX format with an XML string and a TMX resource path
* @param {String} tmxString
* @param {String} resourcePath
* @return {Boolean}
*/
initWithXML:function (tmxString, resourcePath) {
this._internalInit(null, resourcePath);
return this.parseXMLString(tmxString);
},
/** Initalises parsing of an XML file, either a tmx (Map) file or tsx (Tileset) file
* @param {String} tmxFile
* @param {boolean} [isXmlString=false]
* @return {Element}
*/
parseXMLFile:function (tmxFile, isXmlString) {
isXmlString = isXmlString || false;
tmxFile = cc.FileUtils.getInstance().fullPathForFilename(tmxFile);
var mapXML = cc.SAXParser.getInstance().tmxParse(tmxFile, isXmlString);
var i, j;
// PARSE