MapMgr.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { _decorator, Size, Sprite, TiledMap, TiledObjectGroup, Vec2, Vec3 } from 'cc';
  2. import { ModulerBase } from './ModulerBase';
  3. import { ObstacleMgr } from './ObstacleMgr';
  4. import { gameResMgr } from '../GameResMgr';
  5. import { TowerMgr } from './TowerMgr';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('MapMgr')
  8. export class MapMgr extends ModulerBase {
  9. private _tiledMap:TiledMap = null;
  10. private _tiledSize: Size = null;
  11. private _pathPoints: Vec3[] = [];
  12. private _style: number = 1;
  13. private _mapBg: Sprite = null;
  14. private _pathBg: Sprite = null;
  15. tiledObjectGroup: TiledObjectGroup = null;
  16. protected onLoad(): void {
  17. this._tiledMap = this.getComponent(TiledMap);
  18. const bg = this.node.parent.getChildByName("Bg");
  19. this._mapBg = bg.getComponent(Sprite);
  20. this._pathBg = bg.getChildByName("PathBg").getComponent(Sprite);
  21. // this.tiledObjectGroup = this.getComponentInChildren(TiledObjectGroup);
  22. // this.tiledObjectGroup.getObjects();
  23. }
  24. start() {
  25. //给TiledMap设置瓦片地图资源
  26. const tmx = gameResMgr.getTmx();
  27. this._tiledMap.tmxAsset = tmx;
  28. this._style = Number(this._tiledMap.getProperty("style"));
  29. this._tiledSize = this._tiledMap.getTileSize();
  30. //设置关卡背景图 和 路径背景图
  31. this._mapBg.spriteFrame = gameResMgr.getMapBg(this._style);
  32. this._pathBg.spriteFrame = gameResMgr.getPathBg();
  33. this._parseMap();
  34. }
  35. test(){
  36. }
  37. update(deltaTime: number) {
  38. }
  39. private _parseMap(){
  40. //获取瓦片地图的对象
  41. const objects = this._tiledMap.getObjectGroup("PATH").getObjects();
  42. //遍历所有对象 解析地图中的信息
  43. for (const obj of objects) {
  44. if(obj.name.includes("PT")){
  45. //截取字符串的第二个(0,1,2,3)=>字符"PT"后面的第一个
  46. const i = obj.name.slice(2);
  47. this._pathPoints[Number(i) - 1] = new Vec3(
  48. obj.x + this._tiledSize.width/2,
  49. obj.y - this._tiledSize.height/2)
  50. }
  51. else if(obj.name.includes("Obj")){
  52. //空地
  53. }
  54. else if(obj.name.includes("Ob")){
  55. //障碍物 位置,哪个障碍物
  56. //获取O字母前面的数字
  57. const index: string = obj.name.slice(0, obj.name.indexOf("O"));
  58. this.getModuler(ObstacleMgr).creatorObs(
  59. this.getCenterByObj(obj),
  60. this._style,
  61. Number(index));
  62. //记录一下 占了哪个范围 后续障碍被拆除 就是可以种炮台的格子
  63. }
  64. else if(obj.name.includes("T")){
  65. //赠送的炮台
  66. const id: string = obj.name.slice(0, obj.name.indexOf("T"));
  67. //通过id创建炮台位置
  68. const pos = this.getCenterByObj(obj);
  69. //通过id创建炮台
  70. this.getModuler(TowerMgr).creatTower(pos, Number(id));
  71. }
  72. }
  73. }
  74. getCenterByObj(obj): Vec3{
  75. return new Vec3(obj.x + obj.width / 2,
  76. obj.y - obj.height / 2
  77. )
  78. }
  79. getCenterByPos(pos: Vec2): Vec3{
  80. //计算点击到了第几行 第几列格子
  81. return this.getPosByTiled(this.getTiledByPos(pos));
  82. }
  83. getPosByTiled(tiled: Vec2): Vec3{
  84. const x = (tiled.x + 0.5) * this._tiledSize.width;
  85. const y = (tiled.y + 0.5) * this._tiledSize.height;
  86. return new Vec3(x,y);
  87. }
  88. //获取某个坐标在地图的第几行 第几列的格子
  89. getTiledByPos(pos: Vec2): Vec2{
  90. const x = Math.floor(pos.x / this._tiledSize.width);
  91. const y = Math.floor(pos.y / this._tiledSize.height);
  92. return new Vec2(x,y);
  93. }
  94. getPath(): Vec3[]{
  95. return this._pathPoints;
  96. }
  97. // getPath(): Vec3[]{
  98. // const objects = this._tiledMap.getObjectGroup("PATH").getObjects();
  99. // const points: Vec3[] = [];
  100. // for (const obj of objects) {
  101. // if(obj.name.startsWith("PT")){
  102. // //截取字符串的第二个(0,1,2,3)=>字符"PT"后面的第一个
  103. // const i = obj.name.slice(2);
  104. // points[Number(i) - 1] = new Vec3(
  105. // obj.x + this._tiledSize.width/2,
  106. // obj.y - this._tiledSize.height/2)
  107. // }
  108. // }
  109. // return points;
  110. // }
  111. }