lq_game_util.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import SpriteFrame = cc.SpriteFrame;
  2. import director = cc.director;
  3. import tween = cc.tween;
  4. import Texture2D = cc.Texture2D;
  5. import Canvas = cc.Canvas;
  6. import visibleRect = cc.visibleRect;
  7. import find = cc.find;
  8. import Node = cc.Node;
  9. export class LQGameUtil {
  10. private static image_cache: { [key: string]: SpriteFrame } = {};
  11. public static get_image(url: string, callback: (success: boolean, sf: SpriteFrame | undefined) => void, cache = true) {
  12. if (!url || url === '') {
  13. callback(false, undefined);
  14. return;
  15. }
  16. if (this.image_cache[url]) {
  17. callback(true, this.image_cache[url]);
  18. return;
  19. }
  20. cc.loader.load(
  21. {url: url, type: 'png'},
  22. (err: string, texture: Texture2D | undefined) => {
  23. if (err) {
  24. console.error('err:' + err);
  25. callback(false, undefined);
  26. return;
  27. }
  28. const frame = new SpriteFrame(texture);
  29. callback(true, frame);
  30. if (cache) {
  31. this.image_cache[url] = frame;
  32. }
  33. });
  34. }
  35. public static canvas_policy(c: Canvas, width: number, height: number): boolean {
  36. // @ts-ignore
  37. const ratio = visibleRect.height / visibleRect.width;
  38. if (ratio > height / width) {
  39. c.fitHeight = false;
  40. c.fitWidth = true;
  41. } else {
  42. c.fitHeight = true;
  43. c.fitWidth = false;
  44. }
  45. return c.fitHeight;
  46. }
  47. public static recursion_node_property(node: Node, p: { key: string, value: number }) {
  48. if (node.parent) {
  49. // @ts-ignore
  50. p.value *= node.parent[p.key];
  51. this.recursion_node_property(node.parent, p);
  52. }
  53. }
  54. /**
  55. *
  56. * @param path
  57. * eg.'Canvas>node_main>btn_start'
  58. */
  59. public static find_node(path: string): Node | undefined {
  60. if (!path || path.length <= 0) {
  61. console.warn('路径不正确');
  62. return undefined;
  63. }
  64. const arr = path.split('/');
  65. const root = find(arr[0]);
  66. if (!root) {
  67. console.warn('没找到节点:' + arr[0]);
  68. return undefined;
  69. }
  70. let node = root;
  71. for (let i = 1; i < arr.length; i++) {
  72. const temp = node.getChildByName(arr[i]);
  73. if (!temp) {
  74. console.warn('没找到节点:' + arr[i]);
  75. return undefined;
  76. }
  77. node = temp;
  78. }
  79. return node;
  80. }
  81. public static wait(time: number) {
  82. return new Promise((resolve) => {
  83. tween(director.getScene()).delay(time).call(() => {
  84. resolve();
  85. }).start();
  86. });
  87. }
  88. public static set_clip(clip: cc.AnimationClip, off: cc.Vec2, flip_x: boolean, flip_y: boolean) {
  89. let s = (arr: number[]) => {
  90. for (let i = 0; i < arr.length; i++) {
  91. if (i % 2 === 0) {
  92. if (flip_x) {
  93. arr[i] = -arr[i];
  94. }
  95. arr[i] += off.x;
  96. } else {
  97. if (flip_y) {
  98. arr[i] = -arr[i];
  99. }
  100. arr[i] += off.y;
  101. }
  102. }
  103. };
  104. const pos_arr = clip.curveData.props.position;
  105. for (let i = 0; i < pos_arr.length; i++) {
  106. const motionPath = pos_arr[i].motionPath;
  107. const value = pos_arr[i].value;
  108. if (motionPath) {
  109. for (let i = 0; i < motionPath.length; i++) {
  110. s(motionPath[i]);
  111. }
  112. }
  113. s(value);
  114. }
  115. }
  116. public static scroll_layout(layout: cc.Layout, speed: number = 50) {
  117. layout.updateLayout();
  118. const len = layout.type === cc.Layout.Type.HORIZONTAL ? layout.node.width : layout.node.height;
  119. const time = len / speed;
  120. if (layout.type === cc.Layout.Type.HORIZONTAL) {
  121. let offset = layout.node.anchorX === 1 ? layout.node.width * 0.5 : -layout.node.width * 0.5;
  122. layout.node.runAction(cc.repeatForever(cc.sequence(
  123. cc.moveBy(time, cc.v2(offset, 0)),
  124. cc.callFunc(() => {
  125. layout.node.x -= offset;
  126. })
  127. )));
  128. } else if (layout.type === cc.Layout.Type.VERTICAL || layout.type === cc.Layout.Type.GRID) {
  129. let offset = layout.node.anchorY === 1 ? layout.node.height * 0.5 : -layout.node.height * 0.5;
  130. layout.node.runAction(cc.repeatForever(cc.sequence(
  131. cc.moveBy(time, cc.v2(0, offset)),
  132. cc.callFunc(() => {
  133. layout.node.y -= offset;
  134. })
  135. )));
  136. }
  137. }
  138. }