MapMgr.ts 5.1 KB

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