Guide.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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: cc.Node, displayName: "形象" })
  23. node_person: cc.Node = null;
  24. @property({ type: cc.RichText, displayName: "对话内容" })
  25. rich_dialog: cc.RichText = null;
  26. @property({ type: cc.RichText, displayName: "对话内容1" })
  27. rich_dialog1: cc.RichText = null;
  28. private guides: GuideVO[];
  29. private crtStep: number;
  30. private crtGuide: GuideVO;
  31. private targetNode: cc.Node;
  32. private event_data: string;
  33. /** 对话框对齐方式 上下左右中 */
  34. private widgets: string[] = ['top', 'bottom', 'left', 'right', 'verticalCenter', 'horizontalCenter'];
  35. onLoad() {
  36. this.node_click.on(cc.Node.EventType.TOUCH_START, this.clickNodeClick, this);
  37. }
  38. start() {
  39. if (gData.guideData.crtID == null) {
  40. this.node.destroy();
  41. return;
  42. }
  43. this.reset();
  44. this.guides = gData.guideData.getGuidesByID(gData.guideData.crtID);
  45. this.crtStep = -1;
  46. this.nextStep();
  47. }
  48. private nextStep() {
  49. this.crtStep++;
  50. this.crtGuide = this.guides[this.crtStep];
  51. mk.data.sendDataEvent(DataEventId.guide, `新手引导第${gData.guideData.crtID}_${this.crtStep}步完成`);
  52. this.updateGuide();
  53. }
  54. private updateGuide() {
  55. if (this.crtGuide == null) {
  56. this.close();
  57. }
  58. else {
  59. this.node_display.getComponent(cc.Mask).type = this.crtGuide.display_type == 0 ? cc.Mask.Type.RECT : cc.Mask.Type.ELLIPSE;
  60. this.node_bg.opacity = 180;
  61. this.node_finger.opacity = 255;
  62. this.node_dialog.opacity = 255;
  63. this.targetNode = null;
  64. this.event_data = null;
  65. let [x, y, w, h] = [null, null, null, null];
  66. //点击全屏下一步,显示区域不显示 || 点击区域下一步,并发送事件,显示区域读配置
  67. if (this.crtGuide.click_rect == "all" || this.crtGuide.click_rect.indexOf("event") != -1) {
  68. //显示区域: display_rect为""表示全屏
  69. if (this.crtGuide.display_rect.length == 0) {
  70. this.node_display.width = this.node_display.height = 0;
  71. }
  72. else {
  73. let arr = this.crtGuide.display_rect.split(":");
  74. x = parseInt(arr[0]);
  75. y = parseInt(arr[1]);
  76. w = parseInt(arr[2]);
  77. h = parseInt(arr[3]);
  78. }
  79. if (this.crtGuide.click_rect == "all") {
  80. this.node_click.x = 0;
  81. this.node_click.y = 0;
  82. this.node_click.width = this.node.width;
  83. this.node_click.height = this.node.height;
  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. if (this.crtGuide.btnEnable == '1') {
  109. //穿透点击目标节点时增加一个事件触发下一步,触发后移除
  110. this.node_click.width = this.node_click.height = 0;
  111. let eventHandler = new cc.Component.EventHandler();
  112. eventHandler.target = this.node;
  113. eventHandler.component = "Guide";
  114. eventHandler.handler = "clickNodeClick";
  115. this.targetNode.getComponent(cc.Button).clickEvents.push(eventHandler);
  116. }
  117. else {
  118. this.node_click.x = 0;
  119. this.node_click.y = 0;
  120. this.node_click.width = this.node.width;
  121. this.node_click.height = this.node.height;
  122. }
  123. }
  124. //显示区域由大到小动画
  125. if (x != null && y != null && w != null && h != null) {
  126. if (this.crtGuide.display_type == 0) {//矩形选中框
  127. //先重置
  128. this.node_bg.x = this.node_bg.y = this.node_display.x = this.node_display.y = 0;
  129. this.node_display.width = this.node.width;
  130. this.node_display.height = this.node.height;
  131. //动画
  132. cc.tween(this.node_display)
  133. .to(0.2, { x: x, y: y, width: w, height: h }, {
  134. onUpdate: () => {
  135. this.node_bg.x = -this.node_display.x;
  136. this.node_bg.y = -this.node_display.y;
  137. }
  138. })
  139. .start();
  140. }
  141. else {
  142. //先重置
  143. this.node_bg.x = this.node_display.x = x;
  144. this.node_bg.y = this.node_display.y = y;
  145. this.node_display.width = this.node_display.height = this.node_bg.width = this.node_bg.height = this.node.height * 2;
  146. //动画
  147. let radius = this.crtGuide.display_type;
  148. cc.tween(this.node_display)
  149. .to(0.2, { width: radius, height: radius })
  150. .start();
  151. }
  152. }
  153. //对话框位置
  154. //全屏对齐
  155. if (this.crtGuide.dialog_pos != null && this.crtGuide.dialog_pos.length > 0) {
  156. let arr = this.crtGuide.dialog_pos.split(":");
  157. let wedgit = this.node_dialog.getComponent(cc.Widget);
  158. for (let i = 0; i < this.widgets.length; i++) {
  159. switch (i) {
  160. case 0:
  161. wedgit.isAlignTop = arr[i] != "";
  162. break;
  163. case 1:
  164. wedgit.isAlignBottom = arr[i] != "";
  165. break;
  166. case 2:
  167. wedgit.isAlignLeft = arr[i] != "";
  168. break;
  169. case 3:
  170. wedgit.isAlignRight = arr[i] != "";
  171. break;
  172. case 4:
  173. wedgit.isAlignVerticalCenter = arr[i] != "";
  174. break;
  175. case 5:
  176. wedgit.isAlignHorizontalCenter = arr[i] != "";
  177. break;
  178. }
  179. if (arr[i] != "") {
  180. wedgit[this.widgets[i]] = parseInt(arr[i]);
  181. }
  182. }
  183. wedgit.enabled = true;
  184. wedgit.updateAlignment();
  185. }
  186. else {//显示区域偏移
  187. let arr = this.crtGuide.dialog_pos1.split(",");
  188. let wedgit = this.node_dialog.getComponent(cc.Widget);
  189. wedgit.enabled = false;
  190. this.node_dialog.x = x + parseInt(arr[0]);
  191. this.node_dialog.y = y + parseInt(arr[1]);
  192. }
  193. //对话文字对齐方式
  194. let a = this.crtGuide.dialog_alignment;
  195. this.rich_dialog.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  196. this.rich_dialog1.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  197. if (this.crtGuide.finger == "") {
  198. this.node_finger.active = false;
  199. }
  200. else {
  201. this.node_finger.active = true;
  202. let arr = this.crtGuide.finger.split(":");
  203. this.node_finger.x = x + parseInt(arr[0]);
  204. this.node_finger.y = y + parseInt(arr[1]);
  205. }
  206. if (this.crtGuide.tr_form == 3 || this.crtGuide.tr_form == 4) {
  207. this.node_bg.active = false;
  208. if (this.node_click['_touchListener']) {
  209. this.node_click['_touchListener'].swallowTouches = false;
  210. }
  211. }
  212. else {
  213. this.node_bg.active = true;
  214. if (this.node_click['_touchListener']) {
  215. this.node_click['_touchListener'].swallowTouches = true;
  216. }
  217. }
  218. if (this.crtGuide.tr_form == 1 || this.crtGuide.tr_form == 3) {
  219. this.node_head.node.active = true;
  220. this.node_person.active = false;
  221. }
  222. else if (this.crtGuide.tr_form == 2 || this.crtGuide.tr_form == 4) {
  223. this.node_head.node.active = false;
  224. this.node_person.active = true;
  225. }
  226. if (mk.guide.curDes != '') {
  227. this.rich_dialog.string = mk.guide.curDes;
  228. this.rich_dialog1.string = mk.guide.curDes;
  229. mk.guide.curDes = '';
  230. }
  231. else {
  232. this.rich_dialog.string = this.crtGuide.dialog;
  233. this.rich_dialog1.string = this.crtGuide.dialog;
  234. }
  235. }
  236. }
  237. async clickNodeClick() {
  238. mk.audio.playEffect("button");
  239. cc.Tween.stopAllByTarget(this.node_display);
  240. console.log("clickNodeClick");
  241. if (this.crtGuide.btnEnable == '1') {
  242. if (this.targetNode) {
  243. this.targetNode.getComponent(cc.Button).clickEvents.pop();
  244. }
  245. }
  246. if (this.event_data) {
  247. //event_guide data:1_2 (1_2为新手引导的id)
  248. mk.event.emit("event_guide", this.event_data);
  249. }
  250. this.reset();
  251. if (this.crtGuide.lag_next > 0) {
  252. await mk.time.WaitForSeconds(this.crtGuide.lag_next);
  253. }
  254. this.nextStep();
  255. }
  256. /** 等待状态 透明不可点击 */
  257. private reset() {
  258. this.node_display.width = 0;
  259. this.node_display.height = 0;
  260. this.node_bg.width = this.node.width;
  261. this.node_bg.height = this.node.height;
  262. this.node_bg.x = -this.node_display.x;
  263. this.node_bg.y = -this.node_display.y;
  264. this.node_bg.opacity = 0;
  265. this.node_click.width = 0
  266. this.node_click.height = 0;
  267. this.node_finger.opacity = 0;
  268. this.node_dialog.opacity = 0;
  269. }
  270. private close() {
  271. mk.guide.close();
  272. // this.node.destroy();
  273. mk.ui.closePanel(this.node.name);
  274. }
  275. private clickSkip() {
  276. mk.audio.playEffect("button");
  277. mk.data.sendXYEvent("guide", `skip_${mk.guide.crtGuideID}`);
  278. this.close();
  279. }
  280. protected onDestroy(): void {
  281. this.node_click.off(cc.Node.EventType.TOUCH_START, this.clickNodeClick, this);
  282. }
  283. }