123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- /****************************************************************************
- 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;
- /**
- * <p>cc.TMXTiledMap knows how to parse and render a TMX map.</p>
- *
- * <p>It adds support for the TMX tiled map format used by http://www.mapeditor.org <br />
- * It supports isometric, hexagonal and orthogonal tiles.<br />
- * It also supports object groups, objects, and properties.</p>
- *
- * <p>Features: <br />
- * - Each tile will be treated as an cc.Sprite<br />
- * - The sprites are created on demand. They will be created only when you call "layer.getTileAt(position)" <br />
- * - Each tile can be rotated / moved / scaled / tinted / "opacitied", since each tile is a cc.Sprite<br />
- * - Tiles can be added/removed in runtime<br />
- * - The z-order of the tiles can be modified in runtime<br />
- * - Each tile has an anchorPoint of (0,0) <br />
- * - The anchorPoint of the TMXTileMap is (0,0) <br />
- * - The TMX layers will be added as a child <br />
- * - The TMX layers will be aliased by default <br />
- * - The tileset image will be loaded using the cc.TextureCache <br />
- * - Each tile will have a unique tag<br />
- * - Each tile will have a unique z value. top-left: z=1, bottom-right: z=max z<br />
- * - Each object group will be treated as an cc.MutableArray <br />
- * - Object class which will contain all the properties in a dictionary<br />
- * - Properties can be assigned to the Map, Layer, Object Group, and Object</p>
- *
- * <p>Limitations: <br />
- * - It only supports one tileset per layer. <br />
- * - Embeded images are not supported <br />
- * - It only supports the XML format (the JSON format is not supported)</p>
- *
- * <p>Technical description: <br />
- * Each layer is created using an cc.TMXLayer (subclass of cc.SpriteBatchNode). If you have 5 layers, then 5 cc.TMXLayer will be created, <br />
- * unless the layer visibility is off. In that case, the layer won't be created at all. <br />
- * You can obtain the layers (cc.TMXLayer objects) at runtime by: <br />
- * - map.getChildByTag(tag_number); // 0=1st layer, 1=2nd layer, 2=3rd layer, etc...<br />
- * - map.getLayer(name_of_the_layer); </p>
- *
- * <p>Each object group is created using a cc.TMXObjectGroup which is a subclass of cc.MutableArray.<br />
- * You can obtain the object groups at runtime by: <br />
- * - map.getObjectGroup(name_of_the_object_group); </p>
- *
- * <p>Each object is a cc.TMXObject.</p>
- *
- * <p>Each property is stored as a key-value pair in an cc.MutableDictionary.<br />
- * You can obtain the properties at runtime by: </p>
- *
- * <p>map.getProperty(name_of_the_property); <br />
- * layer.getProperty(name_of_the_property); <br />
- * objectGroup.getProperty(name_of_the_property); <br />
- * object.getProperty(name_of_the_property);</p>
- * @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;
- };
|