/
CCTiledMap.js
589 lines (521 loc) · 15.4 KB
/
CCTiledMap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
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.
****************************************************************************/
require('./CCTMXXMLParser');
require('./CCTiledMapAsset');
require('./CCTiledLayer');
require('./CCTiledTile');
require('./CCTiledObjectGroup');
/**
* !#en The orientation of tiled map.
* !#zh Tiled Map 地图方向。
* @enum TiledMap.Orientation
* @static
*/
let Orientation = cc.Enum({
/**
* !#en Orthogonal orientation.
* !#zh 直角鸟瞰地图(90°地图)。
* @property ORTHO
* @type {Number}
* @static
*/
ORTHO: 0,
/**
* !#en Hexagonal orientation.
* !#zh 六边形地图
* @property HEX
* @type {Number}
* @static
*/
HEX: 1,
/**
* Isometric orientation.
* 等距斜视地图(斜45°地图)。
* @property ISO
* @type {Number}
* @static
*/
ISO: 2
});
/*
* The property type of tiled map.
* @enum TiledMap.Property
* @static
*/
let Property = cc.Enum({
/**
* @property NONE
* @type {Number}
* @static
*/
NONE: 0,
/**
* @property MAP
* @type {Number}
* @static
*/
MAP: 1,
/**
* @property LAYER
* @type {Number}
* @static
*/
LAYER: 2,
/**
* @property OBJECTGROUP
* @type {Number}
* @static
*/
OBJECTGROUP: 3,
/**
* @property OBJECT
* @type {Number}
* @static
*/
OBJECT: 4,
/**
* @property TILE
* @type {Number}
* @static
*/
TILE: 5
});
/*
* The tile flags of tiled map.
* @enum TiledMap.TileFlag
* @static
*/
let TileFlag = cc.Enum({
/**
* @property HORIZONTAL
* @type {Number}
* @static
*/
HORIZONTAL: 0x80000000,
/**
* @property VERTICAL
* @type {Number}
* @static
*/
VERTICAL: 0x40000000,
/**
* @property DIAGONAL
* @type {Number}
* @static
*/
DIAGONAL: 0x20000000,
/**
* @property FLIPPED_ALL
* @type {Number}
* @static
*/
FLIPPED_ALL: (0x80000000 | 0x40000000 | 0x20000000) >>> 0,
/**
* @property FLIPPED_MASK
* @type {Number}
* @static
*/
FLIPPED_MASK: (~(0x80000000 | 0x40000000 | 0x20000000)) >>> 0
});
/*
* !#en The stagger axis of Hex tiled map.
* !#zh 六边形地图的 stagger axis 值
* @enum TiledMap.StaggerAxis
* @static
*/
let StaggerAxis = cc.Enum({
/**
* @property STAGGERAXIS_X
* @type {Number}
* @static
*/
STAGGERAXIS_X : 0,
/**
* @property STAGGERAXIS_Y
* @type {Number}
* @static
*/
STAGGERAXIS_Y : 1
});
/*
* !#en The stagger index of Hex tiled map.
* !#zh 六边形地图的 stagger index 值
* @enum TiledMap.StaggerIndex
* @static
*/
let StaggerIndex = cc.Enum({
/**
* @property STAGGERINDEX_ODD
* @type {Number}
* @static
*/
STAGGERINDEX_ODD : 0,
/**
* @property STAGGERINDEX_EVEN
* @type {Number}
* @static
*/
STAGGERINDEX_EVEN : 1
});
let TMXObjectType = cc.Enum({
RECT : 0,
ELLIPSE : 1,
POLYGON : 2,
POLYLINE : 3,
IMAGE : 4
});
/**
* !#en Renders a TMX Tile Map in the scene.
* !#zh 在场景中渲染一个 tmx 格式的 Tile Map。
* @class TiledMap
* @extends Component
*/
let TiledMap = cc.Class({
name: 'cc.TiledMap',
extends: cc.Component,
editor: CC_EDITOR && {
executeInEditMode: true,
menu: 'i18n:MAIN_MENU.component.renderers/TiledMap',
},
ctor () {
this._layers = [];
this._groups = [];
this._properties = [];
this._tileProperties = [];
this._mapSize = cc.size(0, 0);
this._tileSize = cc.size(0, 0);
},
statics: {
Orientation: Orientation,
Property: Property,
TileFlag: TileFlag,
StaggerAxis: StaggerAxis,
StaggerIndex: StaggerIndex,
TMXObjectType: TMXObjectType
},
properties: {
_tmxFile: {
default: null,
type: cc.TiledMapAsset
},
/**
* !#en The TiledMap Asset.
* !#zh TiledMap 资源。
* @property {TiledMapAsset} tmxAsset
* @default ""
*/
tmxAsset : {
get () {
return this._tmxFile;
},
set (value, force) {
if (this._tmxFile !== value || (CC_EDITOR && force)) {
this._tmxFile = value;
this._applyFile();
}
},
type: cc.TiledMapAsset
}
},
/**
* !#en Gets the map size.
* !#zh 获取地图大小。
* @method getMapSize
* @return {Size}
* @example
* let mapSize = tiledMap.getMapSize();
* cc.log("Map Size: " + mapSize);
*/
getMapSize () {
return this._mapSize;
},
/**
* !#en Gets the tile size.
* !#zh 获取地图背景中 tile 元素的大小。
* @method getTileSize
* @return {Size}
* @example
* let tileSize = tiledMap.getTileSize();
* cc.log("Tile Size: " + tileSize);
*/
getTileSize () {
return this._tileSize;
},
/**
* !#en map orientation.
* !#zh 获取地图方向。
* @method getMapOrientation
* @return {Number}
* @example
* let mapOrientation = tiledMap.getMapOrientation();
* cc.log("Map Orientation: " + mapOrientation);
*/
getMapOrientation () {
return this._mapOrientation;
},
/**
* !#en object groups.
* !#zh 获取所有的对象层。
* @method getObjectGroups
* @return {TiledObjectGroup[]}
* @example
* let objGroups = titledMap.getObjectGroups();
* for (let i = 0; i < objGroups.length; ++i) {
* cc.log("obj: " + objGroups[i]);
* }
*/
getObjectGroups () {
return this._groups;
},
/**
* !#en Return the TMXObjectGroup for the specific group.
* !#zh 获取指定的 TMXObjectGroup。
* @method getObjectGroup
* @param {String} groupName
* @return {TiledObjectGroup}
* @example
* let group = titledMap.getObjectGroup("Players");
* cc.log("ObjectGroup: " + group);
*/
getObjectGroup (groupName) {
let groups = this._groups;
for (let i = 0, l = groups.length; i < l; i++) {
let group = groups[i];
if (group && group.getGroupName() === groupName) {
return group;
}
}
return null;
},
/**
* !#en Gets the map properties.
* !#zh 获取地图的属性。
* @method getProperties
* @return {Object[]}
* @example
* let properties = titledMap.getProperties();
* for (let i = 0; i < properties.length; ++i) {
* cc.log("Properties: " + properties[i]);
* }
*/
getProperties () {
return this._properties;
},
/**
* !#en Return All layers array.
* !#zh 返回包含所有 layer 的数组。
* @method getLayers
* @returns {TiledLayer[]}
* @example
* let layers = titledMap.allLayers();
* for (let i = 0; i < layers.length; ++i) {
* cc.log("Layers: " + layers[i]);
* }
*/
getLayers () {
return this._layers;
},
/**
* !#en return the cc.TiledLayer for the specific layer.
* !#zh 获取指定名称的 layer。
* @method getLayer
* @param {String} layerName
* @return {TiledLayer}
* @example
* let layer = titledMap.getLayer("Player");
* cc.log(layer);
*/
getLayer (layerName) {
let layers = this._layers;
for (let i = 0, l = layers.length; i < l; i++) {
let layer = layers[i];
if (layer && layer.getLayerName() === layerName) {
return layer;
}
}
return null;
},
/**
* !#en Return the value for the specific property name.
* !#zh 通过属性名称,获取指定的属性。
* @method getProperty
* @param {String} propertyName
* @return {String}
* @example
* let property = titledMap.getProperty("info");
* cc.log("Property: " + property);
*/
getProperty (propertyName) {
return this._properties[propertyName.toString()];
},
/**
* !#en Return properties dictionary for tile GID.
* !#zh 通过 GID ,获取指定的属性。
* @method getPropertiesForGID
* @param {Number} GID
* @return {Object}
* @example
* let properties = titledMap.getPropertiesForGID(GID);
* cc.log("Properties: " + properties);
*/
getPropertiesForGID (GID) {
return this._tileProperties[GID];
},
__preload () {
if (this._tmxFile) {
// refresh layer entities
this._applyFile();
}
},
onEnable () {
this.node.on(cc.Node.EventType.ANCHOR_CHANGED, this._syncAnchorPoint, this);
},
onDisable () {
this.node.off(cc.Node.EventType.ANCHOR_CHANGED, this._syncAnchorPoint, this);
},
_applyFile () {
let file = this._tmxFile;
if (file) {
let texValues = file.textures;
let texKeys = file.textureNames;
let textures = {};
for (let i = 0; i < texValues.length; ++i) {
textures[texKeys[i]] = texValues[i];
}
let tsxFileNames = file.tsxFileNames;
let tsxFiles = file.tsxFiles;
let tsxMap = {};
for (let i = 0; i < tsxFileNames.length; ++i) {
if (tsxFileNames[i].length > 0) {
tsxMap[tsxFileNames[i]] = tsxFiles[i].text;
}
}
let mapInfo = new cc.TMXMapInfo(file.tmxXmlStr, tsxMap, textures);
let tilesets = mapInfo.getTilesets();
if(!tilesets || tilesets.length === 0)
cc.logID(7241);
this._buildWithMapInfo(mapInfo);
}
else {
this._relseasMapInfo()
}
},
_relseasMapInfo () {
// remove the layers & object groups added before
let layers = this._layers;
for (let i = 0, l = layers.length; i < l; i++) {
layers[i].node.removeFromParent();
}
layers.length = 0;
let groups = this._groups;
for (let i = 0, l = groups.length; i < l; i++) {
groups[i].node.removeFromParent();
}
groups.length = 0;
},
_syncAnchorPoint () {
let anchor = this.node.getAnchorPoint();
for (let i = 0, l = this._layers.length; i < l; i++) {
this._layers[i].node.setAnchorPoint(anchor);
}
},
_buildWithMapInfo (mapInfo) {
this._mapSize = mapInfo.getMapSize();
this._tileSize = mapInfo.getTileSize();
this._mapOrientation = mapInfo.orientation;
this._properties = mapInfo.properties;
this._tileProperties = mapInfo.getTileProperties();
this._relseasMapInfo();
let layers = this._layers;
let groups = this._groups;
let node = this.node;
let layerInfos = mapInfo.getAllChildren();
if (layerInfos && layerInfos.length > 0) {
for (let i = 0, len = layerInfos.length; i < len; i++) {
let layerInfo = layerInfos[i];
let name = layerInfo.name;
let child = this.node.getChildByName(name);
if (!child) {
child = new cc.Node();
child.name = name;
node.addChild(child);
}
if (layerInfo instanceof cc.TMXLayerInfo && layerInfo.visible) {
let layer = child.getComponent(cc.TiledLayer);
if (!layer) {
layer = child.addComponent(cc.TiledLayer);
}
let tileset = this._tilesetForLayer(layerInfo, mapInfo);
layer._init(tileset, layerInfo, mapInfo);
// tell the layerinfo to release the ownership of the tiles map.
layerInfo.ownTiles = false;
// update content size with the max size
this.node.width = Math.max(this.node.width, child.width);
this.node.height = Math.max(this.node.height, child.height);
layers.push(layer);
}
else if (layerInfo instanceof cc.TMXObjectGroupInfo) {
let group = child.getComponent(cc.TiledObjectGroup);
if (!group) {
group = child.addComponent(cc.TiledObjectGroup);
}
group._init(layerInfo, mapInfo);
groups.push(group);
}
}
}
this._syncAnchorPoint();
},
_tilesetForLayer (layerInfo, mapInfo) {
let size = layerInfo._layerSize;
let tilesets = mapInfo.getTilesets();
if (tilesets) {
for (let i = tilesets.length - 1; i >= 0; i--) {
let tileset = tilesets[i];
if (tileset) {
for (let y = 0; y < size.height; y++) {
for (let x = 0; x < size.width; x++) {
let pos = x + size.width * y;
let 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.TiledMap.TileFlag.FLIPPED_MASK)>>>0) >= tileset.firstGid) {
return tileset;
}
}
}
}
}
}
}
// If all the tiles are 0, return empty tileset
cc.logID(7215, layerInfo.name);
return null;
}
});
cc.TiledMap = module.exports = TiledMap;
cc.js.obsolete(cc.TiledMap.prototype, 'cc.TiledMap.tmxFile', 'tmxAsset', true);
cc.js.get(cc.TiledMap.prototype, 'mapLoaded', function () {
cc.errorID(7203);
return [];
}, false);