Guide.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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.initTouch();
  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. this.ifThrough = true;
  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. this.ifThrough = false;
  86. }
  87. else {
  88. this.event_data = this.crtGuide.id + "_" + (this.crtStep + 1);
  89. this.node_click.x = x;
  90. this.node_click.y = y;
  91. this.node_click.width = w;
  92. this.node_click.height = h;
  93. }
  94. }
  95. else {//点击按钮下一步,显示区域为配置的node,点击区域=显示区域
  96. this.targetNode = cc.find("Canvas/" + this.crtGuide.click_rect);
  97. w = this.targetNode.width;
  98. h = this.targetNode.height;
  99. let pos = mk.game.getWorldPos(this.targetNode);
  100. x = pos.x;
  101. y = pos.y;
  102. //lastStruggle
  103. if (this.crtGuide && this.crtGuide.id == 2 && this.crtStep == 1) {
  104. w = this.targetNode.parent.width;
  105. h = this.targetNode.parent.height;
  106. let pos1 = mk.game.getWorldPos(this.targetNode.parent);
  107. x = pos1.x;
  108. y = pos1.y - h / 2;
  109. }
  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. //显示区域由大到小动画
  119. if (x != null && y != null && w != null && h != null) {
  120. if (this.crtGuide.display_type == 0) {//矩形选中框
  121. //先重置
  122. this.node_bg.x = this.node_bg.y = this.node_display.x = this.node_display.y = 0;
  123. this.node_display.width = this.node.width;
  124. this.node_display.height = this.node.height;
  125. //动画
  126. cc.tween(this.node_display)
  127. .to(0.2, { x: x, y: y, width: w, height: h }, {
  128. onUpdate: () => {
  129. this.node_bg.x = -this.node_display.x;
  130. this.node_bg.y = -this.node_display.y;
  131. }
  132. })
  133. .start();
  134. }
  135. else {
  136. //先重置
  137. this.node_bg.x = this.node_display.x = x;
  138. this.node_bg.y = this.node_display.y = y;
  139. this.node_display.width = this.node_display.height = this.node_bg.width = this.node_bg.height = this.node.height * 2;
  140. //动画
  141. let radius = this.crtGuide.display_type;
  142. cc.tween(this.node_display)
  143. .to(0.2, { width: radius, height: radius })
  144. .start();
  145. }
  146. }
  147. //对话框位置
  148. //全屏对齐
  149. if (this.crtGuide.dialog_pos != null && this.crtGuide.dialog_pos.length > 0) {
  150. let arr = this.crtGuide.dialog_pos.split(":");
  151. let wedgit = this.node_dialog.getComponent(cc.Widget);
  152. for (let i = 0; i < this.widgets.length; i++) {
  153. switch (i) {
  154. case 0:
  155. wedgit.isAlignTop = arr[i] != "";
  156. break;
  157. case 1:
  158. wedgit.isAlignBottom = arr[i] != "";
  159. break;
  160. case 2:
  161. wedgit.isAlignLeft = arr[i] != "";
  162. break;
  163. case 3:
  164. wedgit.isAlignRight = arr[i] != "";
  165. break;
  166. case 4:
  167. wedgit.isAlignVerticalCenter = arr[i] != "";
  168. break;
  169. case 5:
  170. wedgit.isAlignHorizontalCenter = arr[i] != "";
  171. break;
  172. }
  173. if (arr[i] != "") {
  174. wedgit[this.widgets[i]] = parseInt(arr[i]);
  175. }
  176. }
  177. wedgit.enabled = true;
  178. wedgit.updateAlignment();
  179. }
  180. else {//显示区域偏移
  181. let arr = this.crtGuide.dialog_pos1.split(",");
  182. let wedgit = this.node_dialog.getComponent(cc.Widget);
  183. wedgit.enabled = false;
  184. this.node_dialog.x = x + parseInt(arr[0]);
  185. this.node_dialog.y = y + parseInt(arr[1]);
  186. }
  187. //对话文字对齐方式
  188. let a = this.crtGuide.dialog_alignment;
  189. this.rich_dialog.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  190. this.rich_dialog1.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  191. if (this.crtGuide.finger == "") {
  192. this.node_finger.active = false;
  193. }
  194. else {
  195. this.node_finger.active = true;
  196. let arr = this.crtGuide.finger.split(":");
  197. this.node_finger.x = x + parseInt(arr[0]);
  198. this.node_finger.y = y + parseInt(arr[1]);
  199. }
  200. if (this.crtGuide.tr_form == 3 || this.crtGuide.tr_form == 4) {
  201. this.node_bg.opacity = 0;
  202. }
  203. if (this.crtGuide.tr_form == 1 || this.crtGuide.tr_form == 3) {
  204. this.node_head.node.active = true;
  205. this.node_person.active = false;
  206. }
  207. else if (this.crtGuide.tr_form == 2 || this.crtGuide.tr_form == 4) {
  208. this.node_head.node.active = false;
  209. this.node_person.active = true;
  210. }
  211. this.rich_dialog.string = this.crtGuide.dialog;
  212. this.rich_dialog1.string = this.crtGuide.dialog;
  213. }
  214. }
  215. async clickNodeClick() {
  216. mk.audio.playEffect("button");
  217. cc.Tween.stopAllByTarget(this.node_display);
  218. console.log("clickNodeClick");
  219. if (this.targetNode) {
  220. this.targetNode.getComponent(cc.Button).clickEvents.pop();
  221. }
  222. if (this.event_data) {
  223. //event_guide data:1_2 (1_2为新手引导的id)
  224. mk.event.emit("event_guide", this.event_data);
  225. }
  226. this.reset();
  227. if (this.crtGuide.lag_next > 0) {
  228. await mk.time.WaitForSeconds(this.crtGuide.lag_next);
  229. }
  230. this.nextStep();
  231. }
  232. /** 等待状态 透明不可点击 */
  233. private reset() {
  234. this.node_display.width = 0;
  235. this.node_display.height = 0;
  236. this.node_bg.width = this.node.width;
  237. this.node_bg.height = this.node.height;
  238. this.node_bg.x = -this.node_display.x;
  239. this.node_bg.y = -this.node_display.y;
  240. this.node_bg.opacity = 0;
  241. this.node_click.width = 0
  242. this.node_click.height = 0;
  243. this.node_finger.opacity = 0;
  244. this.node_dialog.opacity = 0;
  245. }
  246. private close() {
  247. mk.guide.close();
  248. // this.node.destroy();
  249. mk.ui.closePanel(this.node.name);
  250. }
  251. private clickSkip() {
  252. mk.audio.playEffect('closeButton');
  253. mk.data.sendXYEvent("guide", `skip_${mk.guide.crtGuideID}`);
  254. this.close();
  255. }
  256. ///////////////// 自定义触摸监听 /////////////////
  257. private _eventManager = cc["internal"]["eventManager"];
  258. private _touchListener: any;
  259. private ifThrough: boolean;
  260. private initTouch() {
  261. const EventListener = cc["EventListener"];
  262. this._touchListener = EventListener.create({
  263. event: EventListener.TOUCH_ONE_BY_ONE,
  264. swallowTouches: false,//是否吞噬touch事件
  265. owner: this.node_click,
  266. mask: null,
  267. onTouchBegan: this.onTouchStart.bind(this),
  268. onTouchMoved: null,
  269. onTouchEnded: this.onTouchEnded.bind(this),
  270. onTouchCancelled: null,
  271. });
  272. this._eventManager.addListener(this._touchListener, this.node_click);
  273. }
  274. private onTouchStart(touch: cc.Touch, event: cc.Event.EventTouch): boolean {
  275. // cc.log("touch start");
  276. //此处必须返回true(表示接触到了节点),否则TOUCH_MOVE,TOUCH_END,TOUCH_CANCEL不触发。
  277. if (this.ifThrough) {
  278. const point_world_pos = touch.getLocation();
  279. let localPoint = this.node.parent.convertToNodeSpaceAR(new cc.Vec2(point_world_pos.x, point_world_pos.y));
  280. // let localPoint = mk.game.localConvertWorldPointARCenter()
  281. let result = this.pointInPoly(localPoint, this.node_click);
  282. return result;
  283. }
  284. else {
  285. return false;
  286. }
  287. }
  288. /**
  289. * 点是否在节点宽高范围内
  290. * @param point 点击的点
  291. * @param node
  292. * @returns
  293. */
  294. private pointInPoly(point: cc.Vec2 | cc.Vec3, node: cc.Node) {
  295. const self_node_pos = node.getPosition();
  296. if (point.x >= (self_node_pos.x - (this.node_click.width / 2)) &&
  297. point.x <= (self_node_pos.x + (this.node_click.width / 2)) &&
  298. point.y >= (self_node_pos.y - (this.node_click.height / 2)) &&
  299. point.y <= (self_node_pos.y + (this.node_click.height / 2))) {
  300. return true;
  301. }
  302. return false;
  303. }
  304. private onTouchEnded(touch: cc.Touch, event: cc.Event.EventTouch): void {
  305. // cc.log("touch end");
  306. this.clickNodeClick();
  307. }
  308. protected onDestroy(): void {
  309. // super.onDestroy();
  310. this._eventManager.removeListener(this._touchListener, this.node_click);
  311. }
  312. }