Guide.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import { DataEventId } from "../../data/GameData";
  2. import GuideVO from "./GuideVO";
  3. /**
  4. * @description 新手引导
  5. * @author 邹勇
  6. */
  7. const { ccclass, property } = cc._decorator;
  8. @ccclass
  9. export default class Guide extends cc.Component {
  10. @property({ type: cc.Node, displayName: "显示区域" })
  11. node_display: cc.Node = null;
  12. @property({ type: cc.Node, displayName: "点击区域" })
  13. node_click: cc.Node = null;
  14. @property({ type: cc.Node, displayName: "背景" })
  15. node_bg: cc.Node = null;
  16. @property({ type: cc.Node, displayName: "对话框" })
  17. node_dialog: cc.Node = null;
  18. @property({ type: cc.Node, displayName: "手指" })
  19. node_finger: cc.Node = null;
  20. @property({ type: cc.Sprite, displayName: "头像" })
  21. node_head: cc.Sprite = null;
  22. @property({ type: sp.Skeleton, displayName: "形象" })
  23. node_person: sp.Skeleton = null;
  24. @property({ type: cc.RichText, displayName: "对话内容" })
  25. rich_dialog: cc.RichText = null;
  26. private guides: GuideVO[];
  27. private crtStep: number;
  28. private crtGuide: GuideVO;
  29. private targetNode: cc.Node;
  30. private event_data: string;
  31. /** 对话框对齐方式 上下左右中 */
  32. private widgets: string[] = ['top', 'bottom', 'left', 'right', 'verticalCenter', 'horizontalCenter'];
  33. onLoad() {
  34. // this.initTouch();
  35. }
  36. start() {
  37. if (gData.guideData.crtID == null) {
  38. this.node.destroy();
  39. return;
  40. }
  41. this.reset();
  42. this.guides = gData.guideData.getGuidesByID(gData.guideData.crtID);
  43. this.crtStep = -1;
  44. this.nextStep();
  45. }
  46. private nextStep() {
  47. this.crtStep++;
  48. this.crtGuide = this.guides[this.crtStep];
  49. mk.data.sendDataEvent(DataEventId.guide, `新手引导第${gData.guideData.crtID}_${this.crtStep}步完成`);
  50. this.updateGuide();
  51. }
  52. private updateGuide() {
  53. if (this.crtGuide == null) {
  54. this.close();
  55. }
  56. else {
  57. this.node_display.getComponent(cc.Mask).type = this.crtGuide.display_type == 0 ? cc.Mask.Type.RECT : cc.Mask.Type.ELLIPSE;
  58. this.node_bg.opacity = 180;
  59. this.node_finger.opacity = 255;
  60. this.node_dialog.opacity = 255;
  61. this.targetNode = null;
  62. this.event_data = null;
  63. this.ifThrough = true;
  64. let [x, y, w, h] = [null, null, null, null];
  65. //点击全屏下一步,显示区域不显示 || 点击区域下一步,并发送事件,显示区域读配置
  66. if (this.crtGuide.click_rect == "all" || this.crtGuide.click_rect.indexOf("event") != -1) {
  67. //显示区域: display_rect为""表示全屏
  68. if (this.crtGuide.display_rect.length == 0) {
  69. this.node_display.width = this.node_display.height = 0;
  70. }
  71. else {
  72. let arr = this.crtGuide.display_rect.split(":");
  73. x = parseInt(arr[0]);
  74. y = parseInt(arr[1]);
  75. w = parseInt(arr[2]);
  76. h = parseInt(arr[3]);
  77. }
  78. if (this.crtGuide.click_rect == "all") {
  79. this.node_click.x = 0;
  80. this.node_click.y = 0;
  81. this.node_click.width = this.node.width;
  82. this.node_click.height = this.node.height;
  83. this.ifThrough = false;
  84. }
  85. else {
  86. this.event_data = this.crtGuide.id + "_" + (this.crtStep + 1);
  87. this.node_click.x = x;
  88. this.node_click.y = y;
  89. this.node_click.width = w;
  90. this.node_click.height = h;
  91. }
  92. }
  93. else {//点击按钮下一步,显示区域为配置的node,点击区域=显示区域
  94. this.targetNode = cc.find("Canvas/" + this.crtGuide.click_rect);
  95. w = this.targetNode.width;
  96. h = this.targetNode.height;
  97. let pos = mk.game.getWorldPos(this.targetNode);
  98. x = pos.x;
  99. y = pos.y;
  100. //lastStruggle
  101. if (this.crtGuide && this.crtGuide.id == 2 && this.crtStep == 1) {
  102. w = this.targetNode.parent.width;
  103. h = this.targetNode.parent.height;
  104. let pos1 = mk.game.getWorldPos(this.targetNode.parent);
  105. x = pos1.x;
  106. y = pos1.y - h / 2;
  107. }
  108. //穿透点击目标节点时增加一个事件触发下一步,触发后移除
  109. this.node_click.width = this.node_click.height = 0;
  110. let eventHandler = new cc.Component.EventHandler();
  111. eventHandler.target = this.node;
  112. eventHandler.component = "Guide";
  113. eventHandler.handler = "clickNodeClick";
  114. this.targetNode.getComponent(cc.Button).clickEvents.push(eventHandler);
  115. }
  116. //显示区域由大到小动画
  117. if (x != null && y != null && w != null && h != null) {
  118. if (this.crtGuide.display_type == 0) {//矩形选中框
  119. //先重置
  120. this.node_bg.x = this.node_bg.y = this.node_display.x = this.node_display.y = 0;
  121. this.node_display.width = this.node.width;
  122. this.node_display.height = this.node.height;
  123. //动画
  124. cc.tween(this.node_display)
  125. .to(0.2, { x: x, y: y, width: w, height: h }, {
  126. onUpdate: () => {
  127. this.node_bg.x = -this.node_display.x;
  128. this.node_bg.y = -this.node_display.y;
  129. }
  130. })
  131. .start();
  132. }
  133. else {
  134. //先重置
  135. this.node_bg.x = this.node_display.x = x;
  136. this.node_bg.y = this.node_display.y = y;
  137. this.node_display.width = this.node_display.height = this.node_bg.width = this.node_bg.height = this.node.height * 2;
  138. //动画
  139. let radius = this.crtGuide.display_type;
  140. cc.tween(this.node_display)
  141. .to(0.2, { width: radius, height: radius })
  142. .start();
  143. }
  144. }
  145. //对话框位置
  146. //全屏对齐
  147. if (this.crtGuide.dialog_pos != null && this.crtGuide.dialog_pos.length > 0) {
  148. let arr = this.crtGuide.dialog_pos.split(":");
  149. let wedgit = this.node_dialog.getComponent(cc.Widget);
  150. wedgit.enabled = true;
  151. for (let i = 0; i < this.widgets.length; i++) {
  152. wedgit[this.widgets[i]] = arr[i] == "" ? null : parseInt(arr[i]);
  153. }
  154. }
  155. else {//显示区域偏移
  156. let arr = this.crtGuide.dialog_pos1.split(",");
  157. let wedgit = this.node_dialog.getComponent(cc.Widget);
  158. wedgit.enabled = false;
  159. this.node_dialog.x = x + parseInt(arr[0]);
  160. this.node_dialog.y = y + parseInt(arr[1]);
  161. }
  162. //对话文字对齐方式
  163. let a = this.crtGuide.dialog_alignment;
  164. this.rich_dialog.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  165. if (this.crtGuide.finger == "") {
  166. this.node_finger.active = false;
  167. }
  168. else {
  169. this.node_finger.active = true;
  170. let arr = this.crtGuide.finger.split(":");
  171. this.node_finger.x = x + parseInt(arr[0]);
  172. this.node_finger.y = y + parseInt(arr[1]);
  173. }
  174. if (this.crtGuide.tr_form == 1) {
  175. this.node_head.node.active = true;
  176. this.node_person.node.active = false;
  177. }
  178. else {
  179. this.node_head.node.active = false;
  180. this.node_person.node.active = true;
  181. }
  182. this.rich_dialog.string = this.crtGuide.dialog;
  183. }
  184. }
  185. async clickNodeClick() {
  186. mk.audio.playEffect("button");
  187. cc.Tween.stopAllByTarget(this.node_display);
  188. console.log("clickNodeClick");
  189. if (this.targetNode) {
  190. this.targetNode.getComponent(cc.Button).clickEvents.pop();
  191. }
  192. if (this.event_data) {
  193. //event_guide data:1_2 (1_2为新手引导的id)
  194. mk.event.emit("event_guide", this.event_data);
  195. }
  196. this.reset();
  197. if (this.crtGuide.lag_next > 0) {
  198. await mk.time.WaitForSeconds(this.crtGuide.lag_next);
  199. }
  200. this.nextStep();
  201. }
  202. /** 等待状态 透明不可点击 */
  203. private reset() {
  204. this.node_display.width = 0;
  205. this.node_display.height = 0;
  206. this.node_bg.width = this.node.width;
  207. this.node_bg.height = this.node.height;
  208. this.node_bg.x = -this.node_display.x;
  209. this.node_bg.y = -this.node_display.y;
  210. this.node_bg.opacity = 0;
  211. this.node_click.width = 0
  212. this.node_click.height = 0;
  213. this.node_finger.opacity = 0;
  214. this.node_dialog.opacity = 0;
  215. }
  216. private close() {
  217. mk.guide.close();
  218. mk.ui.closePanel(this.node.name);
  219. }
  220. ///////////////// 自定义触摸监听 /////////////////
  221. private _eventManager = cc["internal"]["eventManager"];
  222. private _touchListener: any;
  223. private ifThrough: boolean;
  224. private initTouch() {
  225. const EventListener = cc["EventListener"];
  226. this._touchListener = EventListener.create({
  227. event: EventListener.TOUCH_ONE_BY_ONE,
  228. swallowTouches: false,//是否吞噬touch事件
  229. owner: this.node_click,
  230. mask: null,
  231. onTouchBegan: this.onTouchStart.bind(this),
  232. onTouchMoved: null,
  233. onTouchEnded: this.onTouchEnded.bind(this),
  234. onTouchCancelled: null,
  235. });
  236. this._eventManager.addListener(this._touchListener, this.node_click);
  237. }
  238. private onTouchStart(touch: cc.Touch, event: cc.Event.EventTouch): boolean {
  239. // cc.log("touch start");
  240. //此处必须返回true(表示接触到了节点),否则TOUCH_MOVE,TOUCH_END,TOUCH_CANCEL不触发。
  241. if (this.ifThrough) {
  242. const point_world_pos = touch.getLocation();
  243. let localPoint = this.node.parent.convertToNodeSpaceAR(new cc.Vec2(point_world_pos.x, point_world_pos.y));
  244. // let localPoint = mk.game.localConvertWorldPointARCenter()
  245. let result = this.pointInPoly(localPoint, this.node_click);
  246. return result;
  247. }
  248. else {
  249. return false;
  250. }
  251. }
  252. /**
  253. * 点是否在节点宽高范围内
  254. * @param point 点击的点
  255. * @param node
  256. * @returns
  257. */
  258. private pointInPoly(point: cc.Vec2 | cc.Vec3, node: cc.Node) {
  259. const self_node_pos = node.getPosition();
  260. if (point.x >= (self_node_pos.x - (this.node_click.width / 2)) &&
  261. point.x <= (self_node_pos.x + (this.node_click.width / 2)) &&
  262. point.y >= (self_node_pos.y - (this.node_click.height / 2)) &&
  263. point.y <= (self_node_pos.y + (this.node_click.height / 2))) {
  264. return true;
  265. }
  266. return false;
  267. }
  268. private onTouchEnded(touch: cc.Touch, event: cc.Event.EventTouch): void {
  269. // cc.log("touch end");
  270. this.clickNodeClick();
  271. }
  272. protected onDestroy(): void {
  273. // super.onDestroy();
  274. this._eventManager.removeListener(this._touchListener, this.node_click);
  275. }
  276. /** 点击跳过 */
  277. clickSkip() {
  278. mk.audio.playEffect('button');
  279. mk.data.sendXYEvent("guide", `skip_${mk.guide.crtGuideID}`);
  280. mk.data.sendDataEvent(DataEventId.guideSkip, `新手引导第${mk.guide.crtGuideID}步跳过`);
  281. this.close();
  282. }
  283. }