MapMgr.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { _decorator, Size, TiledMap, TiledObjectGroup, Vec2, Vec3 } from 'cc';
  2. import { ModulerBase } from './ModulerBase';
  3. import { ObstacleMgr } from './ObstacleMgr';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('MapMgr')
  6. export class MapMgr extends ModulerBase {
  7. private _tiledMap:TiledMap = null;
  8. private _tiledSize: Size = null;
  9. private _pathPoints: Vec3[] = [];
  10. tiledObjectGroup: TiledObjectGroup = null;
  11. protected onLoad(): void {
  12. this._tiledMap = this.getComponent(TiledMap);
  13. this._tiledSize = this._tiledMap.getTileSize();
  14. // this.tiledObjectGroup = this.getComponentInChildren(TiledObjectGroup);
  15. // this.tiledObjectGroup.getObjects();
  16. }
  17. start() {
  18. this._parseMap();
  19. }
  20. test(){
  21. }
  22. update(deltaTime: number) {
  23. }
  24. private _parseMap(){
  25. //获取瓦片地图的对象
  26. const objects = this._tiledMap.getObjectGroup("PATH").getObjects();
  27. //遍历所有对象 解析地图中的信息
  28. for (const obj of objects) {
  29. if(obj.name.includes("PT")){
  30. //截取字符串的第二个(0,1,2,3)=>字符"PT"后面的第一个
  31. const i = obj.name.slice(2);
  32. this._pathPoints[Number(i) - 1] = new Vec3(
  33. obj.x + this._tiledSize.width/2,
  34. obj.y - this._tiledSize.height/2)
  35. }
  36. else if(obj.name.includes("Obj")){
  37. //空地
  38. }
  39. else if(obj.name.includes("Ob")){
  40. //障碍物
  41. this.getModuler(ObstacleMgr).creatorObs(this.getCenterByObj(obj),0)
  42. }
  43. else if(obj.name.includes("T")){
  44. //赠送的炮台
  45. }
  46. }
  47. }
  48. getCenterByObj(obj): Vec3{
  49. return new Vec3(obj.x + obj.width / 2,
  50. obj.y - obj.height / 2
  51. )
  52. }
  53. getCenterByPos(pos: Vec2): Vec3{
  54. //计算点击到了第几行 第几列格子
  55. return this.getPosByTiled(this.getTiledByPos(pos));
  56. }
  57. getPosByTiled(tiled: Vec2): Vec3{
  58. const x = (tiled.x + 0.5) * this._tiledSize.width;
  59. const y = (tiled.y + 0.5) * this._tiledSize.height;
  60. return new Vec3(x,y);
  61. }
  62. //获取某个坐标在地图的第几行 第几列的格子
  63. getTiledByPos(pos: Vec2): Vec2{
  64. const x = Math.floor(pos.x / this._tiledSize.width);
  65. const y = Math.floor(pos.y / this._tiledSize.height);
  66. return new Vec2(x,y);
  67. }
  68. getPath(): Vec3[]{
  69. return this._pathPoints;
  70. }
  71. // getPath(): Vec3[]{
  72. // const objects = this._tiledMap.getObjectGroup("PATH").getObjects();
  73. // const points: Vec3[] = [];
  74. // for (const obj of objects) {
  75. // if(obj.name.startsWith("PT")){
  76. // //截取字符串的第二个(0,1,2,3)=>字符"PT"后面的第一个
  77. // const i = obj.name.slice(2);
  78. // points[Number(i) - 1] = new Vec3(
  79. // obj.x + this._tiledSize.width/2,
  80. // obj.y - this._tiledSize.height/2)
  81. // }
  82. // }
  83. // return points;
  84. // }
  85. }