晚了没
import { LoadUtil } from "../../../../../utils/LoadUtil";
import Assets from "../../../../asset/Assets";
import Game from "../../../../Game";
export class CityMap
{
private static readonly FILE_NAME_MAP_TMX: string = 'map.tmx';
private static readonly FILE_NAME_MAP_TSX: string = 'map.tsx';
private static readonly FILE_NAME_MAP_TEXTURE: string = 'map.jpg';
private cityID: number;
private loadedCallback: Function = null;
private loadedCallbackTarget: any = null;
constructor(cityID: number)
{
this.cityID = cityID;
}
public load(callback: (error: string, asset: cc.TiledMapAsset) => void, target: any): void
{
this.loadedCallback = callback;
this.loadedCallbackTarget = target;
if (Assets.Feature.City['City' + this.cityID])
{
LoadUtil.load(Assets.Feature.City['City' + this.cityID].Map, this.onLocalMapAssetLoaded, this);
}
else
{
let cityBaseUrl = cc.path.join('resource', 'city', 'city' + this.cityID);
let cityMapTmxUrl = cc.path.join(cityBaseUrl, CityMap.FILE_NAME_MAP_TMX);
let cityMapTsxUrl = cc.path.join(cityBaseUrl, 'map.tsx');
let cityMapTextureUrl = cc.path.join(cityBaseUrl, CityMap.FILE_NAME_MAP_TEXTURE);
let mapPathMap: LoadUtil.PathMap = {
tmx: {
url: cc.path.join(Game.ResourceServer, cityMapTmxUrl),
savePath: cc.path.join(Game.ResourceLocalPath, cityMapTmxUrl)
},
tsx: {
url: cc.path.join(Game.ResourceServer, cityMapTsxUrl),
savePath: cc.path.join(Game.ResourceLocalPath, cityMapTsxUrl)
},
texture: {
url: cc.path.join(Game.ResourceServer, cityMapTextureUrl),
savePath: cc.path.join(Game.ResourceLocalPath, cityMapTextureUrl)
}
}
LoadUtil.load(mapPathMap, this.onRemoteMapAssetLoaded, this);
}
}
private onLocalMapAssetLoaded(result: LoadUtil.Result): void
{
let tmxAsset = result.asset as cc.TiledMapAsset;
this.loadedCallback.call(this.loadedCallbackTarget, null, tmxAsset);
}
private onRemoteMapAssetLoaded(results: LoadUtil.ResultMap): void
{
let tmxAsset = new cc.TiledMapAsset();
let mapTmxString: string = results.tmx.asset;
tmxAsset.tmxXmlStr = mapTmxString;
let mapTsxString: string = results.tsx.asset;
let tsxAsset = new cc.TextAsset();
tsxAsset.text = mapTsxString;
tmxAsset.tsxFiles = [tsxAsset];
tmxAsset.tsxFileNames = [CityMap.FILE_NAME_MAP_TSX];
let mapTexture: cc.Texture2D = results.texture.asset;
tmxAsset.textures = [mapTexture];
tmxAsset.textureNames = [CityMap.FILE_NAME_MAP_TEXTURE]
this.loadedCallback.call(this.loadedCallbackTarget, null, tmxAsset);
}
public release(): void { }
}