setMap.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { cocosz } from "../Framework/CocosZ";
  2. import { ZindexLayer } from "../Framework/Constant";
  3. import { gameMgr } from "./gameMgr";
  4. const { ccclass, property } = cc._decorator;
  5. @ccclass
  6. export default class setMap extends cc.Component {
  7. @property([cc.SpriteFrame])
  8. dmmArr: cc.SpriteFrame[] = [];
  9. dikuai: cc.Node = null;
  10. protected onLoad(): void {
  11. gameMgr.setMapTs = this;
  12. // 躲猫猫图片
  13. gameMgr.dmmArr = this.dmmArr;
  14. // 地块
  15. this.dikuai = this.node.getChildByName("dikuai");
  16. if (this.dikuai) {
  17. this.dikuai.zIndex = cc.macro.MIN_ZINDEX;
  18. }
  19. // 获取坐标
  20. if ([1, 2, 4].includes(cocosz.gameMode) && this.node.getChildByName("point")) {
  21. let point = this.node.getChildByName("point");
  22. for (let i = 0; i < point.childrenCount; i++) {
  23. gameMgr.posList[i] = i;
  24. }
  25. }
  26. }
  27. protected onDestroy(): void {
  28. gameMgr.setMapTs = null;;
  29. }
  30. protected start(): void {
  31. for (let i = 0; i < this.node.children.length; i++) {
  32. // 初始zindex
  33. const child = this.node.children[i];
  34. }
  35. }
  36. time: number = -1;
  37. lateUpdate() {
  38. if (cocosz.isPause || gameMgr.isWin || gameMgr.isFail) return;
  39. if (this.time++ % 30 == 0) {
  40. this.checkAllNode();
  41. }
  42. }
  43. mainCameraPos: cc.Vec2 = cc.Vec2.ZERO;
  44. distanceX: number = 2500;
  45. distanceY: number = 1500;
  46. checkAllNode() {
  47. if (gameMgr && gameMgr.mainCamereRootNode) {
  48. this.mainCameraPos = gameMgr.mainCamereRootNode.getPosition();
  49. this.distanceX = cc.winSize.width / 2 / gameMgr.mainCamera.zoomRatio + 500;
  50. this.distanceY = cc.winSize.height / 2 / gameMgr.mainCamera.zoomRatio + 500;
  51. }
  52. // 地图节点
  53. let excludeArr = ["guide", "guidePath", "player", "colllider", "dikuai", "point", "itemPoint", "tree", "tipLayer", "jingyanLayer", "posLayer"];
  54. for (let i = 0; i < this.node.childrenCount; i++) {
  55. let child = this.node.children[i];
  56. if (excludeArr.includes(child.name) == false) {
  57. this.checkNode(child)
  58. }
  59. }
  60. // 地图地块
  61. let dikuai = this.node.getChildByName("dikuai");
  62. if (dikuai) {
  63. for (let i = 0; i < dikuai.childrenCount; i++) {
  64. let child = dikuai.children[i];
  65. this.checkNode(child);
  66. }
  67. }
  68. }
  69. checkNode(n: cc.Node, isRefresh: boolean = false) {
  70. if (isRefresh) {
  71. if (gameMgr && gameMgr.mainCamereRootNode) {
  72. this.mainCameraPos = gameMgr.mainCamereRootNode.getPosition();
  73. this.distanceX = cc.winSize.width / 2 / gameMgr.mainCamera.zoomRatio + 500;
  74. this.distanceY = cc.winSize.height / 2 / gameMgr.mainCamera.zoomRatio + 500;
  75. }
  76. }
  77. if (n && n.isValid && n.activeInHierarchy) {
  78. if (n.parent.name == this.node.name || n.parent.name == "dikuai") {
  79. if (n.opacity == 0 || n.opacity == 255) {
  80. if (((n.x + n.width * Math.abs(n.scaleX) / 2) < (this.mainCameraPos.x - this.distanceX)) || ((n.x - n.width * Math.abs(n.scaleX) / 2) > (this.mainCameraPos.x + this.distanceX) ||
  81. (n.y + n.height * Math.abs(n.scaleY) / 2) < (this.mainCameraPos.y - this.distanceY)) || ((n.y - n.height * Math.abs(n.scaleY) / 2) > (this.mainCameraPos.y + this.distanceY)
  82. )) {
  83. n.opacity = 0;
  84. return false;
  85. } else {
  86. n.opacity = 255;
  87. return true;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. // 释放资源节点
  94. release(call: Function) {
  95. // 删除dikaui子节点
  96. if (this.dikuai && this.dikuai.isValid && this.dikuai.childrenCount) {
  97. for (let i = 0; i < 2; i++) {
  98. let child = this.dikuai.children[i];
  99. if (child && child.isValid) {
  100. child.destroy();
  101. }
  102. }
  103. setTimeout(() => { this.release(call); });
  104. }
  105. // 删除普通节点
  106. else if (this.node.childrenCount) {
  107. for (let i = 0; i < 10; i++) {
  108. let child = this.node.children[i];
  109. if (child && child.isValid) {
  110. child.destroy();
  111. }
  112. }
  113. setTimeout(() => { this.release(call); });
  114. }
  115. // 结束后回调
  116. else {
  117. call && call();
  118. }
  119. }
  120. }