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. 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_dialog1.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  190. if (this.crtGuide.finger == "") {
  191. this.node_finger.active = false;
  192. }
  193. else {
  194. this.node_finger.active = true;
  195. let arr = this.crtGuide.finger.split(":");
  196. this.node_finger.x = x + parseInt(arr[0]);
  197. this.node_finger.y = y + parseInt(arr[1]);
  198. }
  199. if (this.crtGuide.tr_form == 1) {
  200. this.node_head.node.active = true;
  201. this.node_person.active = false;
  202. }
  203. else {
  204. this.node_head.node.active = false;
  205. this.node_person.active = true;
  206. }
  207. this.rich_dialog.string = this.crtGuide.dialog;
  208. this.rich_dialog1.string = this.crtGuide.dialog;
  209. }
  210. }
  211. async clickNodeClick() {
  212. mk.audio.playEffect("button");
  213. cc.Tween.stopAllByTarget(this.node_display);
  214. console.log("clickNodeClick");
  215. if (this.targetNode) {
  216. this.targetNode.getComponent(cc.Button).clickEvents.pop();
  217. }
  218. if (this.event_data) {
  219. //event_guide data:1_2 (1_2为新手引导的id)
  220. mk.event.emit("event_guide", this.event_data);
  221. }
  222. this.reset();
  223. if (this.crtGuide.lag_next > 0) {
  224. await mk.time.WaitForSeconds(this.crtGuide.lag_next);
  225. }
  226. this.nextStep();
  227. }
  228. /** 等待状态 透明不可点击 */
  229. private reset() {
  230. this.node_display.width = 0;
  231. this.node_display.height = 0;
  232. this.node_bg.width = this.node.width;
  233. this.node_bg.height = this.node.height;
  234. this.node_bg.x = -this.node_display.x;
  235. this.node_bg.y = -this.node_display.y;
  236. this.node_bg.opacity = 0;
  237. this.node_click.width = 0
  238. this.node_click.height = 0;
  239. this.node_finger.opacity = 0;
  240. this.node_dialog.opacity = 0;
  241. }
  242. private close() {
  243. mk.guide.close();
  244. // this.node.destroy();
  245. mk.ui.closePanel(this.node.name);
  246. }
  247. private clickSkip() {
  248. mk.audio.playEffect('closeButton');
  249. mk.data.sendXYEvent("guide", `skip_${mk.guide.crtGuideID}`);
  250. this.close();
  251. }
  252. ///////////////// 自定义触摸监听 /////////////////
  253. private _eventManager = cc["internal"]["eventManager"];
  254. private _touchListener: any;
  255. private ifThrough: boolean;
  256. private initTouch() {
  257. const EventListener = cc["EventListener"];
  258. this._touchListener = EventListener.create({
  259. event: EventListener.TOUCH_ONE_BY_ONE,
  260. swallowTouches: false,//是否吞噬touch事件
  261. owner: this.node_click,
  262. mask: null,
  263. onTouchBegan: this.onTouchStart.bind(this),
  264. onTouchMoved: null,
  265. onTouchEnded: this.onTouchEnded.bind(this),
  266. onTouchCancelled: null,
  267. });
  268. this._eventManager.addListener(this._touchListener, this.node_click);
  269. }
  270. private onTouchStart(touch: cc.Touch, event: cc.Event.EventTouch): boolean {
  271. // cc.log("touch start");
  272. //此处必须返回true(表示接触到了节点),否则TOUCH_MOVE,TOUCH_END,TOUCH_CANCEL不触发。
  273. if (this.ifThrough) {
  274. const point_world_pos = touch.getLocation();
  275. let localPoint = this.node.parent.convertToNodeSpaceAR(new cc.Vec2(point_world_pos.x, point_world_pos.y));
  276. // let localPoint = mk.game.localConvertWorldPointARCenter()
  277. let result = this.pointInPoly(localPoint, this.node_click);
  278. return result;
  279. }
  280. else {
  281. return false;
  282. }
  283. }
  284. /**
  285. * 点是否在节点宽高范围内
  286. * @param point 点击的点
  287. * @param node
  288. * @returns
  289. */
  290. private pointInPoly(point: cc.Vec2 | cc.Vec3, node: cc.Node) {
  291. const self_node_pos = node.getPosition();
  292. if (point.x >= (self_node_pos.x - (this.node_click.width / 2)) &&
  293. point.x <= (self_node_pos.x + (this.node_click.width / 2)) &&
  294. point.y >= (self_node_pos.y - (this.node_click.height / 2)) &&
  295. point.y <= (self_node_pos.y + (this.node_click.height / 2))) {
  296. return true;
  297. }
  298. return false;
  299. }
  300. private onTouchEnded(touch: cc.Touch, event: cc.Event.EventTouch): void {
  301. // cc.log("touch end");
  302. this.clickNodeClick();
  303. }
  304. protected onDestroy(): void {
  305. // super.onDestroy();
  306. this._eventManager.removeListener(this._touchListener, this.node_click);
  307. }
  308. }