/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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.
****************************************************************************/
'use strict';
const codec = require('../compression/ZipUtils');
const zlib = require('../compression/zlib.min');
const js = require('../core/platform/js');
require('../core/platform/CCSAXParser');
function uint8ArrayToUint32Array (uint8Arr) {
if(uint8Arr.length % 4 !== 0)
return null;
let arrLen = uint8Arr.length /4;
let retArr = window.Uint32Array? new Uint32Array(arrLen) : [];
for(let i = 0; i < arrLen; i++){
let offset = i * 4;
retArr[i] = uint8Arr[offset] + uint8Arr[offset + 1] * (1 << 8) + uint8Arr[offset + 2] * (1 << 16) + uint8Arr[offset + 3] * (1<<24);
}
return retArr;
}
// 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 TMXLayerInfo
*/
/**
* Properties of the layer info.
* @property {Object} properties
*/
cc.TMXLayerInfo = 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.v2(0,0);
};
cc.TMXLayerInfo.prototype = {
constructor: cc.TMXLayerInfo,
/**
* Gets the Properties.
* @return {Object}
*/
getProperties () {
return this.properties;
},
/**
* Set the Properties.
* @param {object} value
*/
setProperties (value) {
this.properties = value;
}
};
/**
* cc.TMXImageLayerInfo contains the information about the image layers.
* This information is obtained from the TMX file.
* @class TMXImageLayerInfo
*/
cc.TMXImageLayerInfo = function () {
this.name= "";
this.visible = true;
this.width = 0;
this.height = 0;
this.offset = cc.v2(0,0);
this._opacity = 0;
this._trans = new cc.Color(255, 255, 255, 255);
this.sourceImage = null;
};
/**
*
cc.TMXObjectGroupInfo contains the information about the object group like:
* - group name
* - group size
* - group opacity at creation time (it can be modified at runtime)
* - Whether the group is visible
*
* This information is obtained from the TMX file.
* @class TMXObjectGroupInfo
*/
/**
* Properties of the ObjectGroup info.
* @property {Array} properties
*/
cc.TMXObjectGroupInfo = function () {
this.properties = {};
this.name = "";
this._objects = [];
this.visible = true;
this._opacity = 0;
this._color = new cc.Color(255, 255, 255, 255);
this.offset = cc.v2(0,0);
this._draworder = 'topdown';
};
cc.TMXObjectGroupInfo.prototype = {
constructor: cc.TMXObjectGroupInfo,
/**
* Gets the Properties.
* @return {Array}
*/
getProperties () {
return this.properties;
},
/**
* Set the Properties.
* @param {object} value
*/
setProperties (value) {
this.properties = value;
}
};
/**
* 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 TMXTilesetInfo
*/
/**
* Tileset name
* @property {string} name
*/
/**
* First grid
* @property {number} firstGid
*/
/**
* Spacing
* @property {number} spacing
*/
/**
* Margin
* @property {number} margin
*/
/**
* Texture containing the tiles (should be sprite sheet / texture atlas)
* @property {any} sourceImage
*/
/**
* Size in pixels of the image
* @property {cc.Size} imageSize
*/
cc.TMXTilesetInfo = function () {
// Tileset name
this.name = "";
// First grid
this.firstGid = 0;
// Spacing
this.spacing = 0;
// Margin
this.margin = 0;
// Texture containing the tiles (should be sprite sheet / texture atlas)
this.sourceImage = null;
// Size in pixels of the image
this.imageSize = cc.size(0, 0);
this.tileOffset = cc.v2(0, 0);
this._tileSize = cc.size(0, 0);
};
cc.TMXTilesetInfo.prototype = {
constructor: cc.TMXTilesetInfo,
/**
* Return rect
* @param {Number} gid
* @return {Rect}
*/
rectForGID (gid, result) {
let rect = result || cc.rect(0, 0, 0, 0);
rect.width = this._tileSize.width;
rect.height = this._tileSize.height;
gid &= cc.TiledMap.TileFlag.FLIPPED_MASK;
gid = gid - parseInt(this.firstGid, 10);
let max_x = parseInt((this.imageSize.width - this.margin * 2 + this.spacing) / (this._tileSize.width + this.spacing), 10);
rect.x = parseInt((gid % max_x) * (this._tileSize.width + this.spacing) + this.margin, 10);
rect.y = parseInt(parseInt(gid / max_x, 10) * (this._tileSize.height + this.spacing) + this.margin, 10);
return rect;
}
};
function strToHAlign (value) {
const hAlign = cc.Label.HorizontalAlign;
switch (value) {
case 'center':
return hAlign.CENTER;
case 'right':
return hAlign.RIGHT;
default:
return hAlign.LEFT;
}
}
function strToVAlign (value) {
const vAlign = cc.Label.VerticalAlign;
switch (value) {
case 'center':
return vAlign.CENTER;
case 'bottom':
return vAlign.BOTTOM;
default:
return vAlign.TOP;
}
}
function strToColor (value) {
if (!value) {
return cc.color(0, 0, 0, 255);
}
value = (value.indexOf('#') !== -1) ? value.substring(1) : value;
if (value.length === 8) {
let a = parseInt(value.substr(0, 2), 16) || 255;
let r = parseInt(value.substr(2, 2), 16) || 0;
let g = parseInt(value.substr(4, 2), 16) || 0;
let b = parseInt(value.substr(6, 2), 16) || 0;
return cc.color(r, g, b, a);
} else {
let r = parseInt(value.substr(0, 2), 16) || 0;
let g = parseInt(value.substr(2, 2), 16) || 0;
let b = parseInt(value.substr(4, 2), 16) || 0;
return cc.color(r, g, b, 255);
}
}
function getPropertyList (node, map) {
let res = [];
let properties = node.getElementsByTagName("properties");
for (let i = 0; i < properties.length; ++i) {
let property = properties[i].getElementsByTagName("property");
for (let j = 0; j < property.length; ++j) {
res.push(property[j]);
}
}
map = map || {};
for (let i = 0; i < res.length; i++) {
let element = res[i];
let name = element.getAttribute('name');
let type = element.getAttribute('type') || 'string';
let value = element.getAttribute('value');
if (type === 'int') {
value = parseInt(value);
}
else if (type === 'float') {
value = parseFloat(value);
}
else if (type === 'bool') {
value = value === 'true';
}
else if (type === 'color') {
value = strToColor(value);
}
map[name] = value;
}
return map;
}
/**
* 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 TMXMapInfo
*/
/**
* Properties of the map info.
* @property {Array} properties
*/
/**
* Map orientation.
* @property {Number} orientation
*/
/**
* Parent element.
* @property {Object} parentElement
*/
/**
* Parent GID.
* @property {Number} parentGID
*/
/**
* Layer attributes.
* @property {Object} layerAttrs
*/
/**
* Is reading storing characters stream.
* @property {Boolean} storingCharacters
*/
/**
* Current string stored from characters stream.
* @property {String} currentString
*/
/**
* Width of the map
* @property {Number} mapWidth
*/
/**
* Height of the map
* @property {Number} mapHeight
*/
/**
* Width of a tile
* @property {Number} tileWidth
*/
/**
* Height of a tile
* @property {Number} tileHeight
*/
/**
* @example
* 1.
* //create a TMXMapInfo with file name
* let tmxMapInfo = new cc.TMXMapInfo("res/orthogonal-test1.tmx");
* 2.
* //create a TMXMapInfo with content string and resource path
* let resources = "res/TileMaps";
* let filePath = "res/TileMaps/orthogonal-test1.tmx";
* let xmlStr = cc.resources.get(filePath);
* let tmxMapInfo = new cc.TMXMapInfo(xmlStr, resources);
*/
/**
* Creates a TMX Format with a tmx file or content string
*/
cc.TMXMapInfo = function (tmxFile, tsxMap, textures, textureSizes, imageLayerTextures) {
this.properties = [];
this.orientation = null;
this.parentElement = null;
this.parentGID = null;
this.layerAttrs = 0;
this.storingCharacters = false;
this.currentString = null;
this.renderOrder = cc.TiledMap.RenderOrder.RightDown;
this._supportVersion = [1, 2, 0];
this._parser = new cc.SAXParser();
this._objectGroups = [];
this._allChildren = [];
this._mapSize = cc.size(0, 0);
this._tileSize = cc.size(0, 0);
this._layers = [];
this._tilesets = [];
this._imageLayers = [];
this._tileProperties = {};
this._tileAnimations = {};
this._tsxMap = null;
// map of textures indexed by name
this._textures = null;
// hex map values
this._staggerAxis = null;
this._staggerIndex = null;
this._hexSideLength = 0;
this._imageLayerTextures = null;
this.initWithXML(tmxFile, tsxMap, textures, textureSizes, imageLayerTextures);
};
cc.TMXMapInfo.prototype = {
constructor: cc.TMXMapInfo,
/**
* Gets Map orientation.
* @return {Number}
*/
getOrientation () {
return this.orientation;
},
/**
* Set the Map orientation.
* @param {Number} value
*/
setOrientation (value) {
this.orientation = value;
},
/**
* Gets the staggerAxis of map.
* @return {cc.TiledMap.StaggerAxis}
*/
getStaggerAxis () {
return this._staggerAxis;
},
/**
* Set the staggerAxis of map.
* @param {cc.TiledMap.StaggerAxis} value
*/
setStaggerAxis (value) {
this._staggerAxis = value;
},
/**
* Gets stagger index
* @return {cc.TiledMap.StaggerIndex}
*/
getStaggerIndex () {
return this._staggerIndex;
},
/**
* Set the stagger index.
* @param {cc.TiledMap.StaggerIndex} value
*/
setStaggerIndex (value) {
this._staggerIndex = value;
},
/**
* Gets Hex side length.
* @return {Number}
*/
getHexSideLength () {
return this._hexSideLength;
},
/**
* Set the Hex side length.
* @param {Number} value
*/
setHexSideLength (value) {
this._hexSideLength = value;
},
/**
* Map width & height
* @return {Size}
*/
getMapSize () {
return cc.size(this._mapSize.width, this._mapSize.height);
},
/**
* Map width & height
* @param {Size} value
*/
setMapSize (value) {
this._mapSize.width = value.width;
this._mapSize.height = value.height;
},
_getMapWidth () {
return this._mapSize.width;
},
_setMapWidth (width) {
this._mapSize.width = width;
},
_getMapHeight () {
return this._mapSize.height;
},
_setMapHeight (height) {
this._mapSize.height = height;
},
/**
* Tiles width & height
* @return {Size}
*/
getTileSize () {
return cc.size(this._tileSize.width, this._tileSize.height);
},
/**
* Tiles width & height
* @param {Size} value
*/
setTileSize (value) {
this._tileSize.width = value.width;
this._tileSize.height = value.height;
},
_getTileWidth () {
return this._tileSize.width;
},
_setTileWidth (width) {
this._tileSize.width = width;
},
_getTileHeight () {
return this._tileSize.height;
},
_setTileHeight (height) {
this._tileSize.height = height;
},
/**
* Layers
* @return {Array}
*/
getLayers () {
return this._layers;
},
/**
* Layers
* @param {cc.TMXLayerInfo} value
*/
setLayers (value) {
this._allChildren.push(value);
this._layers.push(value);
},
/**
* ImageLayers
* @return {Array}
*/
getImageLayers () {
return this._imageLayers;
},
/**
* ImageLayers
* @param {cc.TMXImageLayerInfo} value
*/
setImageLayers (value) {
this._allChildren.push(value);
this._imageLayers.push(value);
},
/**
* tilesets
* @return {Array}
*/
getTilesets () {
return this._tilesets;
},
/**
* tilesets
* @param {cc.TMXTilesetInfo} value
*/
setTilesets (value) {
this._tilesets.push(value);
},
/**
* ObjectGroups
* @return {Array}
*/
getObjectGroups () {
return this._objectGroups;
},
/**
* ObjectGroups
* @param {cc.TMXObjectGroup} value
*/
setObjectGroups (value) {
this._allChildren.push(value);
this._objectGroups.push(value);
},
getAllChildren () {
return this._allChildren;
},
/**
* parent element
* @return {Object}
*/
getParentElement () {
return this.parentElement;
},
/**
* parent element
* @param {Object} value
*/
setParentElement (value) {
this.parentElement = value;
},
/**
* parent GID
* @return {Number}
*/
getParentGID () {
return this.parentGID;
},
/**
* parent GID
* @param {Number} value
*/
setParentGID (value) {
this.parentGID = value;
},
/**
* Layer attribute
* @return {Object}
*/
getLayerAttribs () {
return this.layerAttrs;
},
/**
* Layer attribute
* @param {Object} value
*/
setLayerAttribs (value) {
this.layerAttrs = value;
},
/**
* Is reading storing characters stream
* @return {Boolean}
*/
getStoringCharacters () {
return this.storingCharacters;
},
/**
* Is reading storing characters stream
* @param {Boolean} value
*/
setStoringCharacters (value) {
this.storingCharacters = value;
},
/**
* Properties
* @return {Array}
*/
getProperties () {
return this.properties;
},
/**
* Properties
* @param {object} value
*/
setProperties (value) {
this.properties = value;
},
/**
* initializes a TMX format with an XML string and a TMX resource path
* @param {String} tmxString
* @param {Object} tsxMap
* @param {Object} textures
* @return {Boolean}
*/
initWithXML (tmxString, tsxMap, textures, textureSizes, imageLayerTextures) {
this._tilesets.length = 0;
this._layers.length = 0;
this._imageLayers.length = 0;
this._tsxMap = tsxMap;
this._textures = textures;
this._imageLayerTextures = imageLayerTextures;
this._textureSizes = textureSizes;
this._objectGroups.length = 0;
this._allChildren.length = 0;
this.properties.length = 0;
this._tileProperties = {};
this._tileAnimations = {};
// tmp vars
this.currentString = "";
this.storingCharacters = false;
this.layerAttrs = cc.TMXLayerInfo.ATTRIB_NONE;
this.parentElement = cc.TiledMap.NONE;
return this.parseXMLString(tmxString);
},
/**
* Initializes parsing of an XML string, either a tmx (Map) string or tsx (Tileset) string
* @param {String} xmlString
* @param {Number} tilesetFirstGid
* @return {Element}
*/
parseXMLString (xmlStr, tilesetFirstGid) {
let mapXML = this._parser._parseXML(xmlStr);
let i;
// PARSE