MapManager.ts
1.87 KB
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
import { MapData } from "./MapData";
import { MapView } from "./MapView";
export class MapManager {
private static _ins: MapManager = null;
static get ins(): MapManager {
if (this._ins == null) {
this._ins = new MapManager();
}
return this._ins;
}
private _cache: Map<string, MapData> = new Map<string, MapData>();
/**
*
* @param mapFileName
* @param finishCallBack 加载完毕回调,参数 data:MapData
* @param thisObj
*/
loadMap(mapFileName: string, finishCallBack: Function, thisObj: any) {
let mapUrl = "map/" + mapFileName;
if (this._cache.has(mapUrl)) {
finishCallBack.call(thisObj, this._cache.get(mapUrl).clone());
}
else {
cc.resources.load(mapUrl, cc.TiledMapAsset, (error: Error, assets: cc.TiledMapAsset) => {
if (!error) {
let mapNode = new cc.Node('tiledmapNode:' + mapUrl);
let tiled = mapNode.addComponent(cc.TiledMap);
tiled.tmxAsset = assets;
let data = new MapData();
data.init(tiled);
this._cache.set(mapUrl, data);
// cc.resources.release(mapUrl, cc.TiledMapAsset); //释放资源,只保留mapData数据 //释放后每次加载新地图都会把图集再加载一次,先不释放
finishCallBack.call(thisObj, data.clone());
}
else {
cc.error(error.message);
}
});
}
}
/**传入数据,产生一个显示对象 */
renderMap(data: MapData, prefabCollection: cc.Node, preview: boolean = false): MapView {
let view = new MapView('mapView_' + data.id.toString());
view.init(data, prefabCollection, preview);
return view;
}
}