import { _decorator, Size, TiledMap, TiledObjectGroup, Vec2, Vec3 } from 'cc'; import { ModulerBase } from './ModulerBase'; import { ObstacleMgr } from './ObstacleMgr'; const { ccclass, property } = _decorator; @ccclass('MapMgr') export class MapMgr extends ModulerBase { private _tiledMap:TiledMap = null; private _tiledSize: Size = null; private _pathPoints: Vec3[] = []; tiledObjectGroup: TiledObjectGroup = null; protected onLoad(): void { this._tiledMap = this.getComponent(TiledMap); this._tiledSize = this._tiledMap.getTileSize(); // this.tiledObjectGroup = this.getComponentInChildren(TiledObjectGroup); // this.tiledObjectGroup.getObjects(); } start() { this._parseMap(); } test(){ } update(deltaTime: number) { } private _parseMap(){ //获取瓦片地图的对象 const objects = this._tiledMap.getObjectGroup("PATH").getObjects(); //遍历所有对象 解析地图中的信息 for (const obj of objects) { if(obj.name.includes("PT")){ //截取字符串的第二个(0,1,2,3)=>字符"PT"后面的第一个 const i = obj.name.slice(2); this._pathPoints[Number(i) - 1] = new Vec3( obj.x + this._tiledSize.width/2, obj.y - this._tiledSize.height/2) } else if(obj.name.includes("Obj")){ //空地 } else if(obj.name.includes("Ob")){ //障碍物 this.getModuler(ObstacleMgr).creatorObs(this.getCenterByObj(obj),0) } else if(obj.name.includes("T")){ //赠送的炮台 } } } getCenterByObj(obj): Vec3{ return new Vec3(obj.x + obj.width / 2, obj.y - obj.height / 2 ) } getCenterByPos(pos: Vec2): Vec3{ //计算点击到了第几行 第几列格子 return this.getPosByTiled(this.getTiledByPos(pos)); } getPosByTiled(tiled: Vec2): Vec3{ const x = (tiled.x + 0.5) * this._tiledSize.width; const y = (tiled.y + 0.5) * this._tiledSize.height; return new Vec3(x,y); } //获取某个坐标在地图的第几行 第几列的格子 getTiledByPos(pos: Vec2): Vec2{ const x = Math.floor(pos.x / this._tiledSize.width); const y = Math.floor(pos.y / this._tiledSize.height); return new Vec2(x,y); } getPath(): Vec3[]{ return this._pathPoints; } // getPath(): Vec3[]{ // const objects = this._tiledMap.getObjectGroup("PATH").getObjects(); // const points: Vec3[] = []; // for (const obj of objects) { // if(obj.name.startsWith("PT")){ // //截取字符串的第二个(0,1,2,3)=>字符"PT"后面的第一个 // const i = obj.name.slice(2); // points[Number(i) - 1] = new Vec3( // obj.x + this._tiledSize.width/2, // obj.y - this._tiledSize.height/2) // } // } // return points; // } }