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 = new Map(); /** * * @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; } }