Guide.ts 12 KB

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