Guide.ts 14 KB

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