Guide.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.guide.curGuideIdStr = gData.guideData.crtID + "_" + this.crtStep;
  52. this.updateGuide();
  53. mk.data.sendDataEvent(DataEventId.guide, `新手引导第${gData.guideData.crtID}_${this.crtStep}步完成`);
  54. }
  55. private updateGuide() {
  56. if (this.crtGuide == null) {
  57. this.close();
  58. }
  59. else {
  60. this.node_display.getComponent(cc.Mask).type = this.crtGuide.display_type == 0 ? cc.Mask.Type.RECT : cc.Mask.Type.ELLIPSE;
  61. this.node_bg.opacity = 180;
  62. this.node_finger.opacity = 255;
  63. this.node_dialog.opacity = 255;
  64. this.targetNode = null;
  65. this.event_data = null;
  66. let [x, y, w, h] = [null, null, null, null];
  67. //点击全屏下一步,显示区域不显示 || 点击区域下一步,并发送事件,显示区域读配置
  68. if (this.crtGuide.click_rect == "all" || this.crtGuide.click_rect.indexOf("event") != -1) {
  69. //显示区域: display_rect为""表示全屏
  70. if (this.crtGuide.display_rect.length == 0) {
  71. this.node_display.width = this.node_display.height = 0;
  72. }
  73. else {
  74. let arr = this.crtGuide.display_rect.split(":");
  75. x = parseInt(arr[0]);
  76. y = parseInt(arr[1]);
  77. w = parseInt(arr[2]);
  78. h = parseInt(arr[3]);
  79. }
  80. if (this.crtGuide.click_rect == "all") {
  81. this.node_click.x = 0;
  82. this.node_click.y = 0;
  83. this.node_click.width = this.node.width;
  84. this.node_click.height = this.node.height;
  85. }
  86. else {
  87. this.event_data = this.crtGuide.id + "_" + (this.crtStep + 1);
  88. this.node_click.x = x;
  89. this.node_click.y = y;
  90. this.node_click.width = w;
  91. this.node_click.height = h;
  92. }
  93. }
  94. else {//点击按钮下一步,显示区域为配置的node,点击区域=显示区域
  95. this.targetNode = cc.find("Canvas/" + this.crtGuide.click_rect);
  96. w = this.targetNode.width;
  97. h = this.targetNode.height;
  98. let pos = mk.game.getWorldPos(this.targetNode);
  99. x = pos.x;
  100. y = pos.y;
  101. // //lastStruggle
  102. // if (this.crtGuide && this.crtGuide.id == 2 && this.crtStep == 1) {
  103. // w = this.targetNode.parent.width;
  104. // h = this.targetNode.parent.height;
  105. // let pos1 = mk.game.getWorldPos(this.targetNode.parent);
  106. // x = pos1.x;
  107. // y = pos1.y - h / 2;
  108. // }
  109. if (this.crtGuide.btnEnable == '1') {
  110. //穿透点击目标节点时增加一个事件触发下一步,触发后移除
  111. this.node_click.width = this.node_click.height = 0;
  112. let eventHandler = new cc.Component.EventHandler();
  113. eventHandler.target = this.node;
  114. eventHandler.component = "Guide";
  115. eventHandler.handler = "clickNodeClick";
  116. this.targetNode.getComponent(cc.Button).clickEvents.push(eventHandler);
  117. }
  118. else {
  119. this.node_click.x = 0;
  120. this.node_click.y = 0;
  121. this.node_click.width = this.node.width;
  122. this.node_click.height = this.node.height;
  123. }
  124. }
  125. //显示区域由大到小动画
  126. if (x != null && y != null && w != null && h != null) {
  127. if (this.crtGuide.display_type == 0) {//矩形选中框
  128. //先重置
  129. this.node_bg.x = this.node_bg.y = this.node_display.x = this.node_display.y = 0;
  130. this.node_display.width = this.node.width;
  131. this.node_display.height = this.node.height;
  132. //动画
  133. cc.tween(this.node_display)
  134. .to(0.2, { x: x, y: y, width: w, height: h }, {
  135. onUpdate: () => {
  136. this.node_bg.x = -this.node_display.x;
  137. this.node_bg.y = -this.node_display.y;
  138. }
  139. })
  140. .start();
  141. }
  142. else {
  143. //先重置
  144. this.node_bg.x = this.node_display.x = x;
  145. this.node_bg.y = this.node_display.y = y;
  146. this.node_display.width = this.node_display.height = this.node_bg.width = this.node_bg.height = this.node.height * 2;
  147. //动画
  148. let radius = this.crtGuide.display_type;
  149. cc.tween(this.node_display)
  150. .to(0.2, { width: radius, height: radius })
  151. .start();
  152. }
  153. }
  154. //对话框位置
  155. //全屏对齐
  156. if (this.crtGuide.dialog_pos != null && this.crtGuide.dialog_pos.length > 0) {
  157. let arr = this.crtGuide.dialog_pos.split(":");
  158. let wedgit = this.node_dialog.getComponent(cc.Widget);
  159. for (let i = 0; i < this.widgets.length; i++) {
  160. switch (i) {
  161. case 0:
  162. wedgit.isAlignTop = arr[i] != "";
  163. break;
  164. case 1:
  165. wedgit.isAlignBottom = arr[i] != "";
  166. break;
  167. case 2:
  168. wedgit.isAlignLeft = arr[i] != "";
  169. break;
  170. case 3:
  171. wedgit.isAlignRight = arr[i] != "";
  172. break;
  173. case 4:
  174. wedgit.isAlignVerticalCenter = arr[i] != "";
  175. break;
  176. case 5:
  177. wedgit.isAlignHorizontalCenter = arr[i] != "";
  178. break;
  179. }
  180. if (arr[i] != "") {
  181. wedgit[this.widgets[i]] = parseInt(arr[i]);
  182. }
  183. }
  184. wedgit.enabled = true;
  185. wedgit.updateAlignment();
  186. }
  187. else {//显示区域偏移
  188. let arr = this.crtGuide.dialog_pos1.split(",");
  189. let wedgit = this.node_dialog.getComponent(cc.Widget);
  190. wedgit.enabled = false;
  191. this.node_dialog.x = x + parseInt(arr[0]);
  192. this.node_dialog.y = y + parseInt(arr[1]);
  193. }
  194. //对话文字对齐方式
  195. let a = this.crtGuide.dialog_alignment;
  196. this.rich_dialog.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  197. this.rich_dialog1.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  198. if (this.crtGuide.finger == "") {
  199. this.node_finger.active = false;
  200. }
  201. else {
  202. this.node_finger.active = true;
  203. let arr = this.crtGuide.finger.split(":");
  204. this.node_finger.x = x + parseInt(arr[0]);
  205. this.node_finger.y = y + parseInt(arr[1]);
  206. }
  207. if (this.crtGuide.tr_form == 3 || this.crtGuide.tr_form == 4) {
  208. this.node_bg.active = false;
  209. if (this.node_click['_touchListener']) {
  210. this.node_click['_touchListener'].swallowTouches = false;
  211. }
  212. }
  213. else {
  214. this.node_bg.active = true;
  215. if (this.node_click['_touchListener']) {
  216. this.node_click['_touchListener'].swallowTouches = true;
  217. }
  218. }
  219. if (this.crtGuide.tr_form == 1 || this.crtGuide.tr_form == 3) {
  220. this.node_head.node.active = true;
  221. this.node_person.active = false;
  222. }
  223. else if (this.crtGuide.tr_form == 2 || this.crtGuide.tr_form == 4) {
  224. this.node_head.node.active = false;
  225. this.node_person.active = true;
  226. }
  227. if (mk.guide.curDes != '') {
  228. this.rich_dialog.string = mk.guide.curDes;
  229. this.rich_dialog1.string = mk.guide.curDes;
  230. mk.guide.curDes = '';
  231. }
  232. else {
  233. this.rich_dialog.string = this.crtGuide.dialog;
  234. this.rich_dialog1.string = this.crtGuide.dialog;
  235. }
  236. }
  237. }
  238. async clickNodeClick() {
  239. mk.audio.playEffect("button");
  240. cc.Tween.stopAllByTarget(this.node_display);
  241. console.log("clickNodeClick");
  242. if (this.crtGuide.btnEnable == '1') {
  243. if (this.targetNode) {
  244. this.targetNode.getComponent(cc.Button).clickEvents.pop();
  245. }
  246. }
  247. if (this.event_data) {
  248. //event_guide data:1_2 (1_2为新手引导的id)
  249. mk.event.emit("event_guide", this.event_data);
  250. }
  251. this.reset();
  252. if (this.crtGuide.lag_next > 0) {
  253. await mk.time.WaitForSeconds(this.crtGuide.lag_next);
  254. }
  255. if (this.crtGuide.continue == "true") {
  256. if(mk.guide.continueCallBack)
  257. {
  258. let isContinue = mk.guide.continueCallBack();
  259. if(isContinue)
  260. {
  261. --this.crtStep;
  262. }
  263. }
  264. }
  265. this.nextStep();
  266. }
  267. /** 等待状态 透明不可点击 */
  268. private reset() {
  269. this.node_display.width = 0;
  270. this.node_display.height = 0;
  271. this.node_bg.width = this.node.width;
  272. this.node_bg.height = this.node.height;
  273. this.node_bg.x = -this.node_display.x;
  274. this.node_bg.y = -this.node_display.y;
  275. this.node_bg.opacity = 0;
  276. this.node_click.width = 0
  277. this.node_click.height = 0;
  278. this.node_finger.opacity = 0;
  279. this.node_dialog.opacity = 0
  280. }
  281. private close() {
  282. mk.guide.close();
  283. // this.node.destroy();
  284. mk.guide.curGuideIdStr = null;
  285. if(mk.guide.continueCallBack)
  286. {
  287. mk.guide.continueCallBack = null;
  288. }
  289. console.log("closeGuide ======");
  290. mk.ui.closePanel(this.node.name);
  291. }
  292. private clickSkip() {
  293. mk.audio.playEffect("button");
  294. this.close();
  295. mk.data.sendXYEvent("guide", `skip_${gData.guideData.crtID}_${this.crtStep}`);
  296. mk.data.sendDataEvent(DataEventId.guideSkip, `新手引导第${gData.guideData.crtID}_${this.crtStep}步跳过`);
  297. }
  298. protected onDestroy(): void {
  299. this.node_click.off(cc.Node.EventType.TOUCH_START, this.clickNodeClick, this);
  300. }
  301. }