TaskMask.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. targetButton: {
  5. default: null,
  6. type: cc.Button,
  7. visible: false,
  8. serializable: false,
  9. },
  10. stencil: cc.Node,
  11. stencil_tip: cc.Node,
  12. stencil_skip: cc.Node,
  13. //转换坐标的偏移量
  14. offset: {
  15. default: null,
  16. visible: false,
  17. serializable: false,
  18. },
  19. //需要偏移第几个节点
  20. offsetCount: {
  21. default: -1,
  22. type: cc.Integer,
  23. visible: false,
  24. serializable: false,
  25. },
  26. collider: cc.PolygonCollider
  27. },
  28. random(max, min) {
  29. return Math.floor(Math.random() * (max - min + 1) + min);
  30. },
  31. onLoad() {
  32. // this.node.on(cc.Node.EventType.TOUCH_START, this.TouchStartFunction, this);
  33. // this.node.on(cc.Node.EventType.TOUCH_END, this.TouchEndFunction, this);
  34. // this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.TouchCancelFunction, this);
  35. //箭头动画
  36. // this.stencil_tip.opacity = 255;
  37. this.stencil_tip.stopAllActions();
  38. var s = cc.sequence(cc.scaleTo(0.5, 1.5), cc.scaleTo(0.5, 1.0));
  39. var repeat = cc.repeatForever(s);
  40. this.stencil_tip.runAction(repeat);
  41. this.collider.node.on(cc.Node.EventType.TOUCH_START, function (touch, event) {
  42. console.log("Hit!");
  43. // // 返回世界坐标
  44. // let touchLoc = touch.getLocation();
  45. // console.log('this.collider.world.points=', this.collider.world.points);
  46. // // https://docs.cocos.com/creator/api/zh/classes/Intersection.html 检测辅助类
  47. // if (cc.Intersection.pointInPolygon(touchLoc, this.collider.world.points)) {
  48. // console.log("Hit!");
  49. // }
  50. // else {
  51. // console.log("No hit");
  52. // }
  53. if (this.targetButton) {
  54. cc.Component.EventHandler.emitEvents(this.targetButton.clickEvents, event);
  55. this.count++;
  56. if (this.count < this.targetNodes.length) {
  57. this._setMaskPosAndEvent();
  58. } else {
  59. GlobalD.GameControl.isTaskInProgress = false;
  60. this.node.destroy();
  61. }
  62. }
  63. }, this);
  64. },
  65. /**
  66. * 初始设置目标参数
  67. * @method onInitTaskMask
  68. * @param {[Node]} _targetNodes 透明区域指向的位置节点对象数组
  69. * @param {string} _targetTaskName 建造的建筑名称
  70. */
  71. onInitTaskMask(_targetNodes, _targetTaskName) {
  72. GlobalD.GameControl.isTaskInProgress = true;
  73. // this.nodeLength = _targetNodes.length;
  74. this.targetNodes = _targetNodes;
  75. this.count = 0;
  76. this._setMaskPosAndEvent();
  77. if (_targetTaskName)
  78. this.targetTaskName = _targetTaskName;
  79. },
  80. onRemove() {
  81. console.log('移除的名称:',this.targetTaskName);
  82. if (this.targetTaskName) {
  83. if(this.targetTaskName == 'Highway'){
  84. task.removeTaskNode('taskPrefabTiledTileHighway');
  85. //设置任务状态
  86. task.onShadowArchitectureReset();
  87. task.showManagerhide();
  88. task._setTaskIconCountClick(1);
  89. setTimeout(() => {
  90. task.taskCallBack();
  91. }, 500);
  92. }else if(this.targetTaskName == 'Farmland'){
  93. // removeName = task.taskCursorName[4];
  94. task.Farming();
  95. task.onShadowArchitectureReset();
  96. }else if(this.targetTaskName == 'House'){
  97. // removeName = task.taskCursorName[4];
  98. task.taskShare();
  99. task.onShadowArchitectureReset();
  100. }
  101. }
  102. GlobalD.GameControl.isTaskInProgress = false;
  103. this.node.destroy();
  104. },
  105. //设置提示位置和绑定对应的按钮事件
  106. _setMaskPosAndEvent() {
  107. let pt = this.stencil.parent.convertToNodeSpaceAR(this.targetNodes[this.count].parent.convertToWorldSpaceAR(this.targetNodes[this.count].position));
  108. //设置最后一个点的偏移量
  109. //铺路时候,场景的位置不准确需要修正
  110. // console.log(this.offset, this.offsetCount);
  111. if (this.offset && this.offsetCount == this.count)
  112. pt = cc.v2(pt.x + this.offset.x, pt.y + this.offset.y);
  113. this.stencil.position = pt;
  114. let _size = this.targetNodes[this.count].width > this.targetNodes[this.count].height ? this.targetNodes[this.count].width : this.targetNodes[this.count].height;
  115. this.stencil.width = _size + 5;
  116. this.stencil.height = _size + 5;
  117. this.stencil_tip.position = cc.v2(pt.x, pt.y + _size * 0.4);
  118. //跳过当前建造
  119. this.stencil_skip.position = cc.v2(this.stencil_tip.position.x, this.stencil_tip.position.y + 150);
  120. let _targetButton = this.targetNodes[this.count].getComponent(cc.Button);
  121. if (_targetButton)
  122. this.targetButton = _targetButton;
  123. else
  124. console.error('遮挡模板的按钮对象不存在;');
  125. },
  126. isInCircle: function (pos, cPos, r) {
  127. console.log('pospos:', pos, cPos, r, pos.sub(cPos).mag());
  128. if (pos.sub(cPos).mag() <= r) {
  129. return true;
  130. }
  131. return false;
  132. },
  133. TouchEndFunction(event) {
  134. let pt = this.stencil.parent.convertToNodeSpaceAR(event.getLocation());
  135. let x = this.stencil.position.x;
  136. let y = this.stencil.position.y;
  137. // let rect = cc.rect(0, 0, this.stencil.width, this.stencil.height);
  138. //点中空洞,返回false,触摸事件继续派发
  139. // if (rect.contains(pt)) {
  140. if (this.isInCircle(pt, cc.v2(x, y), this.stencil.width)) {
  141. console.log('点击区域');
  142. return;
  143. if (this.targetButton) {
  144. cc.Component.EventHandler.emitEvents(this.targetButton.clickEvents, event);
  145. this.count++;
  146. if (this.count < this.targetNodes.length) {
  147. this._setMaskPosAndEvent();
  148. } else {
  149. GlobalD.GameControl.isTaskInProgress = false;
  150. this.node.destroy();
  151. }
  152. }
  153. return false;
  154. }
  155. // console.log('点击区域无效:', pt);
  156. },
  157. // update (dt) {},
  158. onButtonEvent(target, event) {
  159. console.log(target, event);
  160. }
  161. });