Guide.ts 13 KB

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