Global.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { Prefab, UITransform, resources, v3 } from "cc";
  2. import CountDownSchedule from "./CountDownSchedule";
  3. export class Global {
  4. //是否开发环境
  5. public static IsDevelop = false;
  6. //事件类型
  7. public static EventType_AddScore = "AddScore";
  8. public static EventType_MinusScore = "MinusScore";
  9. public static EventType_ShowTips = "ShowTips";
  10. public static EventType_GameOver = "GameOver";
  11. //音效
  12. public static Audio_Bg = "/audio/bg"
  13. public static Audio_Success = "/audio/success"
  14. public static Audio_GameFail = "/audio/gameFail"
  15. public static Audio_TimeOutFail = "/audio/timeOutFail"
  16. public static Audio_right = "/audio/right"
  17. public static Audio_wrong = "/audio/wrong"
  18. public static Audio_MinusScore = "/audio/wrong"
  19. public static Audio_AddScore = "/audio/addScore"
  20. public static Audio_achieve = "/audio/achieve"
  21. public static Audio_nextLev = "audio/achieve";
  22. //游戏
  23. public static StartLv = 1;
  24. public static MaxLv = 5;
  25. public static GameSetTime = 60;
  26. public static DifficultyRate = 1; //游戏难度系数
  27. //全局定时器
  28. public static cdSchedule: CountDownSchedule = new CountDownSchedule();
  29. /**
  30. * 获取xxx-xxx之间的随机数
  31. * @param min 最小数
  32. * @param max 最大数
  33. * @returns 随机数字
  34. */
  35. public static getRandom(min, max): number {
  36. return Math.floor(Math.random() * (max - min + 1)) + min;
  37. }
  38. /**
  39. * 获取两个数字之差
  40. * @param num1
  41. * @param num2
  42. * @returns
  43. */
  44. public static getDifference(num1, num2): number {
  45. return Math.abs(num1 - num2);
  46. }
  47. /**
  48. * 数组乱序
  49. * @param a 数组
  50. * @returns
  51. */
  52. public static randomSort(a) {
  53. return a.sort(() => Math.random() - 0.5);
  54. }
  55. /**
  56. * 节点间坐标转换
  57. * @param s
  58. * @param o
  59. * @returns
  60. */
  61. public static nodeToOtherPos(s, o) {
  62. return Global.worldConvertLocalPointAR(s, Global.localConvertWorldPointAR(o))
  63. }
  64. /**
  65. * 得到一个节点的世界坐标
  66. * node的原点在中心
  67. * @param {*} node
  68. */
  69. public static localConvertWorldPointAR(node) {
  70. if (node) {
  71. return node.getComponent(UITransform).convertToWorldSpaceAR(v3());
  72. }
  73. return null;
  74. }
  75. /**
  76. * 把一个世界坐标的点,转换到某个节点下的坐标
  77. * 原点在node中心
  78. * @param {*} node
  79. * @param {*} worldPoint
  80. */
  81. public static worldConvertLocalPointAR(node, worldPoint) {
  82. if (node) {
  83. return node.getComponent(UITransform).convertToNodeSpaceAR(worldPoint);
  84. }
  85. return null;
  86. }
  87. /**
  88. * resources读取Prefab
  89. * @param {*} prename prefab地址
  90. */
  91. public static async LoadPrefab(prename: string): Promise<Prefab> {
  92. let res = (r) => {
  93. return r;
  94. }
  95. return new Promise((res) => {
  96. resources.load(prename, Prefab, (err, node) => {
  97. res(node);
  98. });
  99. });
  100. }
  101. }