Merge pull request #347 from natural-law/import-tiled

Implement the component TMXTiledMap.
This commit is contained in:
Jare Guo
2016-02-19 13:39:57 +08:00
11 changed files with 332 additions and 66 deletions
-45
View File
@@ -1,45 +0,0 @@
/**
* Class for tiled map asset handling.
* @class TiledMapAsset
* @extends RawAsset
* @constructor
*/
var TiledMapAsset = cc.Class({
name: 'cc.TiledMapAsset',
extends: cc.RawAsset,
statics: {
createNodeByInfo: function (info, callback) {
if (CC_EDITOR) {
var Url = require('fire-url');
cc.TiledMapWrapper.preloadTmx( info.url , function (err, textures) {
if (err) {
callback(err);
return;
}
var node;
try {
node = new cc.TMXTiledMap(info.url);
node._file = info.url;
}
catch(e) {
return callback(e);
}
var wrapper = cc.getWrapper(node);
wrapper.name = Url.basenameNoExt(info.url);
wrapper._textures = textures;
return callback(null, node);
}.bind(this) );
}
}
}
});
cc.TiledMapAsset = TiledMapAsset;
module.exports = TiledMapAsset;
-1
View File
@@ -37,4 +37,3 @@ require('../textures/CCTexture2D');
require('./CCTTFFont');
require('./CCSpriteAnimation');
require('./CCSpriteAtlas');
require('./CCTiledMapAsset');
+1 -1
View File
@@ -1,6 +1,6 @@
require('./CCComponent');
require('./CCComponentInSG');
require('./CCComponentEventHandler')
require('./CCComponentEventHandler');
module.exports = [
require('./CCSprite'),
@@ -121,7 +121,7 @@ cc.TMX_ORIENTATION_ISO = 2;
* var xmlStr = cc.loader.getRes(filePath);
* var tmxTiledMap = new cc.TMXTiledMap(xmlStr, resources);
*/
cc.TMXTiledMap = _ccsg.Node.extend(/** @lends cc.TMXTiledMap# */{
_ccsg.TMXTiledMap = _ccsg.Node.extend(/** @lends cc.TMXTiledMap# */{
properties: null,
mapOrientation: null,
objectGroups: null,
@@ -135,7 +135,7 @@ cc.TMXTiledMap = _ccsg.Node.extend(/** @lends cc.TMXTiledMap# */{
/**
* Creates a TMX Tiled Map with a TMX file or content string. <br/>
* Constructor of cc.TMXTiledMap
* Constructor of _ccsg.TMXTiledMap
* @param {String} tmxFile tmxFile fileName or content string
* @param {String} resourcePath If tmxFile is a file name ,it is not required.If tmxFile is content string ,it is must required.
*/
@@ -260,17 +260,18 @@ cc.TMXTiledMap = _ccsg.Node.extend(/** @lends cc.TMXTiledMap# */{
},
/**
* Initializes the instance of cc.TMXTiledMap with tmxFile
* Initializes the instance of _ccsg.TMXTiledMap with tmxFile
* @param {String} tmxFile
* @return {Boolean} Whether the initialization was successful.
* @example
* //example
* var map = new cc.TMXTiledMap()
* var map = new _ccsg.TMXTiledMap()
* map.initWithTMXFile("hello.tmx");
*/
initWithTMXFile:function (tmxFile) {
if(!tmxFile || tmxFile.length === 0)
throw new Error("cc.TMXTiledMap.initWithTMXFile(): tmxFile should be non-null or non-empty string.");
if(!tmxFile || tmxFile.length === 0) {
return false;
}
this.width = 0;
this.height = 0;
var mapInfo = new cc.TMXMapInfo(tmxFile);
@@ -279,13 +280,13 @@ cc.TMXTiledMap = _ccsg.Node.extend(/** @lends cc.TMXTiledMap# */{
var locTilesets = mapInfo.getTilesets();
if(!locTilesets || locTilesets.length === 0)
cc.log("cc.TMXTiledMap.initWithTMXFile(): Map not found. Please check the filename.");
cc.log("_ccsg.TMXTiledMap.initWithTMXFile(): Map not found. Please check the filename.");
this._buildWithMapInfo(mapInfo);
return true;
},
/**
* Initializes the instance of cc.TMXTiledMap with tmxString
* Initializes the instance of _ccsg.TMXTiledMap with tmxString
* @param {String} tmxString
* @param {String} resourcePath
* @return {Boolean} Whether the initialization was successful.
@@ -297,7 +298,7 @@ cc.TMXTiledMap = _ccsg.Node.extend(/** @lends cc.TMXTiledMap# */{
var mapInfo = new cc.TMXMapInfo(tmxString, resourcePath);
var locTilesets = mapInfo.getTilesets();
if(!locTilesets || locTilesets.length === 0)
cc.log("cc.TMXTiledMap.initWithXML(): Map not found. Please check the filename.");
cc.log("_ccsg.TMXTiledMap.initWithXML(): Map not found. Please check the filename.");
this._buildWithMapInfo(mapInfo);
return true;
},
@@ -349,7 +350,7 @@ cc.TMXTiledMap = _ccsg.Node.extend(/** @lends cc.TMXTiledMap# */{
*/
getLayer:function (layerName) {
if(!layerName || layerName.length === 0)
throw new Error("cc.TMXTiledMap.getLayer(): layerName should be non-null or non-empty string.");
throw new Error("_ccsg.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];
@@ -367,7 +368,7 @@ cc.TMXTiledMap = _ccsg.Node.extend(/** @lends cc.TMXTiledMap# */{
*/
getObjectGroup:function (groupName) {
if(!groupName || groupName.length === 0)
throw new Error("cc.TMXTiledMap.getObjectGroup(): groupName should be non-null or non-empty string.");
throw new Error("_ccsg.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];
@@ -449,7 +450,7 @@ cc.TMXTiledMap = _ccsg.Node.extend(/** @lends cc.TMXTiledMap# */{
}
});
var _p = cc.TMXTiledMap.prototype;
var _p = _ccsg.TMXTiledMap.prototype;
// Extended properties
/** @expose */
@@ -468,12 +469,12 @@ cc.defineGetterSetter(_p, "tileHeight", _p._getTileHeight, _p._setTileHeight);
/**
* Creates a TMX Tiled Map with a TMX file or content string.
* Implementation cc.TMXTiledMap
* @deprecated since v3.0 please use new cc.TMXTiledMap(tmxFile,resourcePath) instead.
* Implementation _ccsg.TMXTiledMap
* @deprecated since v3.0 please use new _ccsg.TMXTiledMap(tmxFile,resourcePath) instead.
* @param {String} tmxFile tmxFile fileName or content string
* @param {String} resourcePath If tmxFile is a file name ,it is not required.If tmxFile is content string ,it is must required.
* @return {cc.TMXTiledMap|undefined}
* @return {_ccsg.TMXTiledMap|undefined}
*/
cc.TMXTiledMap.create = function (tmxFile,resourcePath) {
return new cc.TMXTiledMap(tmxFile,resourcePath);
_ccsg.TMXTiledMap.create = function (tmxFile,resourcePath) {
return new _ccsg.TMXTiledMap(tmxFile,resourcePath);
};
+281
View File
@@ -0,0 +1,281 @@
/****************************************************************************
Copyright (c) 2016 Chukong Technologies 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.
****************************************************************************/
/**
* Renders a TMX Tile Map in the scene.
* @class TiledMap
* @extends _ComponentInSG
*/
var TiledMap = cc.Class({
name: 'cc.TiledMap',
extends: require('./../core/components/CCComponentInSG'),
editor: CC_EDITOR && {
menu: 'i18n:MAIN_MENU.component.renderers/TiledMap'
},
properties: {
/**
* The tmx file.
* @property {string} file
* @default ""
*/
_tmxFile: {
default: '',
url: cc.TiledMapAsset
},
tmxFile : {
get: function () {
return this._tmxFile;
},
set: function (value, force) {
if (this._tmxFile !== value || (CC_EDITOR && force)) {
this._tmxFile = value;
this._applyFile();
}
},
url: cc.TiledMapAsset
}
},
/**
* Gets the map size.
* @method getMapSize
* @return {cc.Size}
*/
getMapSize:function () {
return this._sgNode.getMapSize();
},
/**
* Set the map size.
* @method setMapSize
* @param {cc.Size} Var
*/
setMapSize:function (Var) {
this._sgNode.setMapSize(Var);
},
/**
* Gets the tile size.
* @method getTileSize
* @return {cc.Size}
*/
getTileSize:function () {
return this._sgNode.getTileSize();
},
/**
* Set the tile size
* @method setTileSize
* @param {cc.Size} Var
*/
setTileSize:function (Var) {
this._sgNode.setTileSize(Var);
},
/**
* map orientation
* @method getMapOrientation
* @return {Number}
*/
getMapOrientation:function () {
return this._sgNode.getMapOrientation();
},
/**
* map orientation
* @method setMapOrientation
* @param {Number} Var
*/
setMapOrientation:function (Var) {
this._sgNode.setMapOrientation(Var);
},
/**
* object groups
* @method getObjectGroups
* @return {Array}
*/
getObjectGroups:function () {
return this._sgNode.getObjectGroups();
},
/**
* object groups
* @method setObjectGroups
* @param {Array} Var
*/
setObjectGroups:function (Var) {
this._sgNode.setObjectGroups(Var);
},
/**
* Gets the map properties
* @method getProperties
* @return {object}
*/
getProperties:function () {
return this._sgNode.getProperties();
},
/**
* Set the map properties
* @method setProperties
* @param {object} Var
*/
setProperties:function (Var) {
this._sgNode.setProperties(Var);
},
/**
* Initializes the instance of cc.TiledMap with tmxFile
* @method initWithTMXFile
* @param {String} tmxFile
* @return {Boolean} Whether the initialization was successful.
*/
initWithTMXFile:function (tmxFile) {
return this._sgNode.initWithTMXFile(tmxFile);
},
/**
* Initializes the instance of cc.TiledMap with tmxString
* @method initWithXML
* @param {String} tmxString
* @param {String} resourcePath
* @return {Boolean} Whether the initialization was successful.
*/
initWithXML:function(tmxString, resourcePath){
return this._sgNode.initWithXML(tmxString, resourcePath);
},
/**
* Return All layers array.
* @method allLayers
* @returns {Array}
*/
allLayers: function () {
return this._sgNode.allLayers();
},
/**
* return the TMXLayer for the specific layer
* @method getLayer
* @param {String} layerName
* @return {cc.TMXLayer}
*/
getLayer:function (layerName) {
return this._sgNode.getLayer(layerName);
},
/**
* Return the TMXObjectGroup for the specific group
* @method getObjectGroup
* @param {String} groupName
* @return {cc.TMXObjectGroup}
*/
getObjectGroup:function (groupName) {
return this._sgNode.getObjectGroup(groupName);
},
/**
* Return the value for the specific property name
* @method getProperty
* @param {String} propertyName
* @return {String}
*/
getProperty:function (propertyName) {
return this._sgNode.getProperty(propertyName);
},
/**
* Return properties dictionary for tile GID
* @method getPropertiesForGID
* @param {Number} GID
* @return {object}
*/
getPropertiesForGID: function(GID) {
return this._sgNode.getPropertiesForGID(GID);
},
onEnable: function () {
if (this._sgNode && this._tmxFile) {
this._sgNode.setVisible(true);
}
},
_preloadTmx: function(file, cb) {
cc.loader.load(file, function (err) {
if (err) {
if (cb) cb(err);
return;
}
var mapInfo = new cc.TMXMapInfo(file);
var sets = mapInfo.getTilesets();
if (sets) {
var textures = sets.map(function (set) {
return set.sourceImage;
});
cc.loader.load(textures, function (err) {
cb(err, textures);
});
}
else {
if (cb) cb();
}
});
},
_applyFile: function () {
var sgNode = this._sgNode;
var file = this._tmxFile;
var self = this;
if (file) {
this._preloadTmx(file, function (err, results) {
if (err) throw err;
sgNode.initWithTMXFile(file);
if (self.enabledInHierarchy && !sgNode.isVisible()) {
sgNode.setVisible(true);
}
});
} else {
sgNode.setVisible(false);
}
},
_createSgNode: function () {
return new _ccsg.TMXTiledMap();
},
_initSgNode: function () {
this._applyFile();
}
});
cc.TiledMap = module.exports = TiledMap;
+13
View File
@@ -0,0 +1,13 @@
/**
* Class for tiled map asset handling.
* @class TiledMapAsset
* @extends RawAsset
* @constructor
*/
var TiledMapAsset = cc.Class({
name: 'cc.TiledMapAsset',
extends: cc.RawAsset,
});
cc.TiledMapAsset = TiledMapAsset;
module.exports = TiledMapAsset;
+13
View File
@@ -0,0 +1,13 @@
'use strict';
class TiledMapMeta extends Editor.metas['raw-asset'] {
constructor ( assetdb ) {
super( assetdb );
}
static defaultType() { return 'tiled-map'; }
}
TiledMapMeta.prototype.export = null;
module.exports = TiledMapMeta;
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

+3
View File
@@ -30,4 +30,7 @@ if (!(CC_EDITOR && Editor.isCoreLevel)) {
require('./cocos2d/particle/CCParticleSystem');
}
require('./cocos2d/tilemap/CCTiledMapAsset');
require('./cocos2d/tilemap/CCTiledMap');
require('./extensions/spine');
+1 -1
View File
@@ -134,7 +134,7 @@ var srcs = [
'./cocos2d/menus/CCMenu.js',
'./cocos2d/tilemap/CCTGAlib.js',
'./cocos2d/tilemap/CCTMXTiledMap.js',
'./cocos2d/tilemap/CCSGTMXTiledMap.js',
'./cocos2d/tilemap/CCTMXXMLParser.js',
'./cocos2d/tilemap/CCTMXObjectGroup.js',
'./cocos2d/tilemap/CCTMXLayer.js',
+2 -1
View File
@@ -262,7 +262,8 @@ window._ccsg = {
Sprite: cc.Sprite,
ParticleSystem: cc.ParticleSystem,
Label: cc.Label,
EditBox: cc.EditBox
EditBox: cc.EditBox,
TMXTiledMap: cc.TMXTiledMap
};
// rename cc.Class to cc._Class