GuideWeak.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const { ccclass, property } = cc._decorator;
  2. /**
  3. * @author 薛鸿潇
  4. */
  5. @ccclass
  6. export default class GuideWeak extends cc.Component {
  7. onLoad() {
  8. gData.guideWeakData.guideStyle = this;
  9. this.InitTouch();
  10. mk.event.register('close-panel', this.onClosePanel, this);
  11. }
  12. update(dt) {
  13. if (!gData.guideWeakData.enable) return;
  14. if (!gData.guideWeakData.data_config.triggerTime) return;
  15. if (gData.guideWeakData.cur_guide_id) return;
  16. if (!mk.guide.isGuideComplete()) return;
  17. if (gData.gameData.gameStyle.node.parent.childrenCount > 1) return;
  18. gData.guideWeakData.silent_time += dt;
  19. if (gData.guideWeakData.silent_time >= gData.guideWeakData.data_config.triggerTime) {
  20. gData.guideWeakData.initGuideId();
  21. gData.guideWeakData.openPanelByBtn();
  22. }
  23. }
  24. /**
  25. * 界面关闭
  26. */
  27. public onClosePanel(panel_name: string = '') {
  28. cc.log(panel_name)
  29. if (gData.guideWeakData.panel_name_config[panel_name] == gData.guideWeakData.cur_guide_id) {
  30. gData.guideWeakData.cur_guide_id = 0;
  31. }
  32. }
  33. /** 玩家的屏幕触摸操作 */
  34. private onClickCall(touch) {
  35. gData.guideWeakData.silent_time = 0;
  36. }
  37. ///////////////// 自定义触摸监听 /////////////////
  38. private _eventManager = cc["internal"]["eventManager"];
  39. private _touchListener: any;
  40. private InitTouch() {
  41. const EventListener = cc["EventListener"];
  42. this._touchListener = EventListener.create({
  43. event: EventListener.TOUCH_ONE_BY_ONE,
  44. swallowTouches: false,//是否吞噬touch事件
  45. owner: this.node,
  46. mask: null,
  47. onTouchBegan: this.onTouchStart.bind(this),
  48. onTouchMoved: null,
  49. onTouchEnded: this.onTouchEnded.bind(this),
  50. onTouchCancelled: null,
  51. });
  52. this._eventManager.addListener(this._touchListener, this.node);
  53. }
  54. private onTouchStart(touch: cc.Touch, event: cc.Event.EventTouch): boolean {
  55. // cc.log("touch start");
  56. //此处必须返回true(表示接触到了节点),否则TOUCH_MOVE,TOUCH_END,TOUCH_CANCEL不触发。
  57. const point_world_pos = touch.getLocation();
  58. let localPoint = this.node.parent.convertToNodeSpaceAR(new cc.Vec2(point_world_pos.x, point_world_pos.y));
  59. let result = this.pointInPoly(localPoint, this.node);
  60. return result;
  61. }
  62. private onTouchEnded(touch: cc.Touch, event: cc.Event.EventTouch): void {
  63. // cc.log("touch end");
  64. this.onClickCall(touch);
  65. }
  66. /**
  67. * 点是否在节点宽高范围内
  68. * @param point 点击的点
  69. * @param node
  70. * @returns
  71. */
  72. private pointInPoly(point: cc.Vec2 | cc.Vec3, node: cc.Node) {
  73. const self_node_pos = node.getPosition();
  74. if (point.x >= (self_node_pos.x - (this.node.width / 2)) &&
  75. point.x <= (self_node_pos.x + (this.node.width / 2)) &&
  76. point.y >= (self_node_pos.y - (this.node.height / 2)) &&
  77. point.y <= (self_node_pos.y + (this.node.height / 2))) {
  78. return true;
  79. }
  80. return false;
  81. }
  82. protected onDestroy(): void {
  83. // super.onDestroy();
  84. this._eventManager.removeListener(this._touchListener, this.node);
  85. mk.event.remove('close-panel', this.onClosePanel, this);
  86. }
  87. }