import { _decorator, Rect, Size, Sprite, TiledMap, TiledObjectGroup, Vec2, Vec3 } from 'cc'; import { ModulerBase } from './ModulerBase'; import { ObstacleMgr } from './ObstacleMgr'; import { gameResMgr } from '../GameResMgr'; import { TowerMgr } from './TowerMgr'; import { TowerData } from '../../DataItem/ItemData'; import { dataMgr } from '../../Frames/DataManager'; const { ccclass, property } = _decorator; @ccclass('MapMgr') export class MapMgr extends ModulerBase { private _tiledMap:TiledMap = null; private _tiledSize: Size = null; private _pathPoints: Vec3[] = []; private _style: number = 1; private _mapBg: Sprite = null; private _pathBg: Sprite = null; tiledObjectGroup: TiledObjectGroup = null; //存储可种植的炮台区域 private _canTowerRect: Rect[] = []; protected onLoad(): void { this._tiledMap = this.getComponent(TiledMap); const bg = this.node.parent.getChildByName("Bg"); this._mapBg = bg.getComponent(Sprite); this._pathBg = bg.getChildByName("PathBg").getComponent(Sprite); // this.tiledObjectGroup = this.getComponentInChildren(TiledObjectGroup); // this.tiledObjectGroup.getObjects(); } start() { //给TiledMap设置瓦片地图资源 const tmx = gameResMgr.getTmx(); this._tiledMap.tmxAsset = tmx; this._style = Number(this._tiledMap.getProperty("style")); this._tiledSize = this._tiledMap.getTileSize(); //设置关卡背景图 和 路径背景图 this._mapBg.spriteFrame = gameResMgr.getMapBg(this._style); this._pathBg.spriteFrame = gameResMgr.getPathBg(); 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")){ //空地 const {x, y, width, height} = obj; this._canTowerRect.push(new Rect(x, y - height, width, height)); } else if(obj.name.includes("Ob")){ //障碍物 位置,哪个障碍物 //获取O字母前面的数字 const index: string = obj.name.slice(0, obj.name.indexOf("O")); this.getModuler(ObstacleMgr).creatorObs( this.getCenterByObj(obj), this._style, Number(index)); //记录一下 占了哪个范围 后续障碍被拆除 就是可以种炮台的格子 } else if(obj.name.includes("T")){ //赠送的炮台 const id: string = obj.name.slice(0, obj.name.indexOf("T")); //通过id创建炮台位置 const pos = this.getCenterByObj(obj); //通过id创建炮台 const data: TowerData = dataMgr.getData(Number(id), "TowerDt") this.getModuler(TowerMgr).creatTower(pos, data); } } } 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; } //判断点击坐标是否可种植炮台 canCreatorTower(pos: Vec2): Vec3{ for(const rect of this._canTowerRect){ if(rect.contains(pos)){ return this.getCenterByPos(pos); } } return null; } // 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; // } }