Guide.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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: sp.Skeleton, displayName: "形象" })
  23. node_person: sp.Skeleton = null;
  24. @property({ type: cc.RichText, displayName: "对话内容" })
  25. rich_dialog: cc.RichText = null;
  26. private guides: GuideVO[];
  27. private crtStep: number;
  28. private crtGuide: GuideVO;
  29. private targetNode: cc.Node;
  30. private event_data: string;
  31. /** 对话框对齐方式 上下左右中 */
  32. private widgets: string[] = ['top', 'bottom', 'left', 'right', 'verticalCenter', 'horizontalCenter'];
  33. onLoad() {
  34. // this.initTouch();
  35. }
  36. start() {
  37. if (gData.guideData.crtID == null) {
  38. this.node.destroy();
  39. return;
  40. }
  41. this.guides = gData.guideData.getGuidesByID(gData.guideData.crtID);
  42. this.crtStep = -1;
  43. this.nextStep();
  44. }
  45. private nextStep() {
  46. this.crtStep++;
  47. this.crtGuide = this.guides[this.crtStep];
  48. mk.data.sendDataEvent(DataEventId.guide,`新手引导第${gData.guideData.crtID}_${this.crtStep}步完成`);
  49. this.updateGuide();
  50. }
  51. private updateGuide() {
  52. if (this.crtGuide == null) {
  53. this.close();
  54. }
  55. else {
  56. this.node_display.getComponent(cc.Mask).type = this.crtGuide.display_type == 0 ? cc.Mask.Type.RECT : cc.Mask.Type.ELLIPSE;
  57. this.node_bg.opacity = 180;
  58. this.node_finger.opacity = 255;
  59. this.node_dialog.opacity = 255;
  60. this.targetNode = null;
  61. this.event_data = null;
  62. this.ifThrough = true;
  63. let [x, y, w, h] = [null, null, null, null];
  64. //点击全屏下一步,显示区域不显示 || 点击区域下一步,并发送事件,显示区域读配置
  65. if (this.crtGuide.click_rect == "all" || this.crtGuide.click_rect.indexOf("event") != -1) {
  66. //显示区域: display_rect为""表示全屏
  67. if (this.crtGuide.display_rect.length == 0) {
  68. this.node_display.width = this.node_display.height = 0;
  69. }
  70. else {
  71. let arr = this.crtGuide.display_rect.split(":");
  72. x = parseInt(arr[0]);
  73. y = parseInt(arr[1]);
  74. w = parseInt(arr[2]);
  75. h = parseInt(arr[3]);
  76. }
  77. if (this.crtGuide.click_rect == "all") {
  78. this.node_click.x = 0;
  79. this.node_click.y = 0;
  80. this.node_click.width = this.node.width;
  81. this.node_click.height = this.node.height;
  82. this.ifThrough = false;
  83. }
  84. else {
  85. this.event_data = this.crtGuide.id + "_" + (this.crtStep + 1);
  86. this.node_click.x = x;
  87. this.node_click.y = y;
  88. this.node_click.width = w;
  89. this.node_click.height = h;
  90. }
  91. }
  92. else {//点击按钮下一步,显示区域为配置的node,点击区域=显示区域
  93. this.targetNode = cc.find("Canvas/" + this.crtGuide.click_rect);
  94. w = this.targetNode.width;
  95. h = this.targetNode.height;
  96. let pos = mk.game.getWorldPos(this.targetNode);
  97. x = pos.x;
  98. y = pos.y;
  99. //穿透点击目标节点时增加一个事件触发下一步,触发后移除
  100. this.node_click.width = this.node_click.height = 0;
  101. let eventHandler = new cc.Component.EventHandler();
  102. eventHandler.target = this.node;
  103. eventHandler.component = "Guide";
  104. eventHandler.handler = "clickNodeClick";
  105. this.targetNode.getComponent(cc.Button).clickEvents.push(eventHandler);
  106. }
  107. //显示区域由大到小动画
  108. if (x != null && y != null && w != null && h != null) {
  109. if (this.crtGuide.display_type == 0) {//矩形选中框
  110. //先重置
  111. this.node_bg.x = this.node_bg.y = this.node_display.x = this.node_display.y = 0;
  112. this.node_display.width = this.node.width;
  113. this.node_display.height = this.node.height;
  114. //动画
  115. cc.tween(this.node_display)
  116. .to(0.1, { x: x, y: y, width: w, height: h }, {
  117. onUpdate: () => {
  118. this.node_bg.x = -this.node_display.x;
  119. this.node_bg.y = -this.node_display.y;
  120. }
  121. })
  122. .start();
  123. }
  124. else {
  125. //先重置
  126. this.node_bg.x = this.node_display.x = x;
  127. this.node_bg.y = this.node_display.y = y;
  128. this.node_display.width = this.node_display.height = this.node_bg.width = this.node_bg.height = this.node.height * 2;
  129. //动画
  130. let radius = this.crtGuide.display_type;
  131. cc.tween(this.node_display)
  132. .to(0.1, { width: radius, height: radius })
  133. .start();
  134. }
  135. }
  136. //对话框位置
  137. ////全屏对齐
  138. if (this.crtGuide.dialog_pos != null && this.crtGuide.dialog_pos.length > 0) {
  139. let arr = this.crtGuide.dialog_pos.split(":");
  140. let wedgit = this.node_dialog.getComponent(cc.Widget);
  141. for (let i = 0; i < this.widgets.length; i++) {
  142. wedgit[this.widgets[i]] = arr[i] == "" ? null : parseInt(arr[i]);
  143. }
  144. }
  145. else {//显示区域偏移
  146. let arr = this.crtGuide.dialog_pos1.split(",");
  147. this.node_dialog.x = x + parseInt(arr[0]);
  148. this.node_dialog.y = y + parseInt(arr[1]);
  149. }
  150. //对话文字对齐方式
  151. let a = this.crtGuide.dialog_alignment;
  152. this.rich_dialog.horizontalAlign = a == '0' ? cc.macro.TextAlignment.LEFT : (a == '1' ? cc.macro.TextAlignment.CENTER : cc.macro.TextAlignment.RIGHT);
  153. if (this.crtGuide.finger == "") {
  154. this.node_finger.active = false;
  155. }
  156. else {
  157. this.node_finger.active = true;
  158. let arr = this.crtGuide.finger.split(":");
  159. this.node_finger.x = x + parseInt(arr[0]);
  160. this.node_finger.y = y + parseInt(arr[1]);
  161. }
  162. if (this.crtGuide.tr_form == 1) {
  163. this.node_head.node.active = true;
  164. this.node_person.node.active = false;
  165. }
  166. else {
  167. this.node_head.node.active = false;
  168. this.node_person.node.active = true;
  169. }
  170. this.rich_dialog.string = this.crtGuide.dialog;
  171. }
  172. }
  173. async clickNodeClick() {
  174. mk.audio.playEffect("button");
  175. cc.Tween.stopAllByTarget(this.node_display);
  176. console.log("clickNodeClick");
  177. if (this.targetNode) {
  178. this.targetNode.getComponent(cc.Button).clickEvents.pop();
  179. }
  180. if (this.event_data) {
  181. //event_guide data:1_2 (1_2为新手引导的id)
  182. mk.event.emit("event_guide", this.event_data);
  183. }
  184. this.reset();
  185. if (this.crtGuide.lag_next > 0) {
  186. await mk.time.WaitForSeconds(this.crtGuide.lag_next);
  187. }
  188. this.nextStep();
  189. }
  190. /** 等待状态 透明不可点击 */
  191. private reset() {
  192. this.node_display.width = 0;
  193. this.node_display.height = 0;
  194. this.node_bg.width = this.node.width;
  195. this.node_bg.height = this.node.height;
  196. this.node_bg.x = -this.node_display.x;
  197. this.node_bg.y = -this.node_display.y;
  198. this.node_bg.opacity = 0;
  199. this.node_click.width = 0
  200. this.node_click.height = 0;
  201. this.node_finger.opacity = 0;
  202. this.node_dialog.opacity = 0;
  203. }
  204. private close() {
  205. mk.guide.close();
  206. this.node.destroy();
  207. }
  208. ///////////////// 自定义触摸监听 /////////////////
  209. private _eventManager = cc["internal"]["eventManager"];
  210. private _touchListener: any;
  211. private ifThrough: boolean;
  212. private initTouch() {
  213. const EventListener = cc["EventListener"];
  214. this._touchListener = EventListener.create({
  215. event: EventListener.TOUCH_ONE_BY_ONE,
  216. swallowTouches: false,//是否吞噬touch事件
  217. owner: this.node_click,
  218. mask: null,
  219. onTouchBegan: this.onTouchStart.bind(this),
  220. onTouchMoved: null,
  221. onTouchEnded: this.onTouchEnded.bind(this),
  222. onTouchCancelled: null,
  223. });
  224. this._eventManager.addListener(this._touchListener, this.node_click);
  225. }
  226. private onTouchStart(touch: cc.Touch, event: cc.Event.EventTouch): boolean {
  227. // cc.log("touch start");
  228. //此处必须返回true(表示接触到了节点),否则TOUCH_MOVE,TOUCH_END,TOUCH_CANCEL不触发。
  229. if (this.ifThrough) {
  230. const point_world_pos = touch.getLocation();
  231. let localPoint = this.node.parent.convertToNodeSpaceAR(new cc.Vec2(point_world_pos.x, point_world_pos.y));
  232. // let localPoint = mk.game.localConvertWorldPointARCenter()
  233. let result = this.pointInPoly(localPoint, this.node_click);
  234. return result;
  235. }
  236. else {
  237. return false;
  238. }
  239. }
  240. /**
  241. * 点是否在节点宽高范围内
  242. * @param point 点击的点
  243. * @param node
  244. * @returns
  245. */
  246. private pointInPoly(point: cc.Vec2 | cc.Vec3, node: cc.Node) {
  247. const self_node_pos = node.getPosition();
  248. if (point.x >= (self_node_pos.x - (this.node_click.width / 2)) &&
  249. point.x <= (self_node_pos.x + (this.node_click.width / 2)) &&
  250. point.y >= (self_node_pos.y - (this.node_click.height / 2)) &&
  251. point.y <= (self_node_pos.y + (this.node_click.height / 2))) {
  252. return true;
  253. }
  254. return false;
  255. }
  256. private onTouchEnded(touch: cc.Touch, event: cc.Event.EventTouch): void {
  257. // cc.log("touch end");
  258. this.clickNodeClick();
  259. }
  260. protected onDestroy(): void {
  261. // super.onDestroy();
  262. this._eventManager.removeListener(this._touchListener, this.node_click);
  263. }
  264. }