Global.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { UITransform, v3 } from "cc";
  2. import CountDownSchedule from "./CountDownSchedule";
  3. export class Global {
  4. public static IsDevelop = false;
  5. //事件类型
  6. public static EventType_AddScore = "AddScore";
  7. public static EventType_MinusScore = "MinusScore";
  8. public static EventType_ShowTips = "ShowTips";
  9. public static EventType_GameOver = "GameOver";
  10. //音效
  11. public static Audio_Bg = "/audio/bg"
  12. public static Audio_AddScore = "/audio/AddScore"
  13. public static Audio_MinusScore = "/audio/minusScore"
  14. public static Audio_gameOver = "/audio/gameOver"
  15. public static Audio_GameFail = "/audio/GameFail"
  16. public static Audio_GameWin = "/audio/GameWin"
  17. public static Audio_Success = "/audio/success"
  18. public static Audio_SelectFail = "/audio/SelectFail"
  19. public static Audio_SelectWin = "/audio/SelectWin"
  20. public static Audio_Selected = "/audio/Selected"
  21. public static Audio_Drop = "/audio/Drop"
  22. public static Audio_timeOutFail = "/audio/timeOutFail"
  23. public static Audio_BlockRAndS = "/audio/BlockRAndS"
  24. //游戏
  25. public static StartLv = 1;
  26. public static MaxLv = 5;
  27. public static GameSetTime = 120;
  28. public static DifficultyRate = 1; //游戏难度系数
  29. //全局定时器
  30. public static cdSchedule: CountDownSchedule = new CountDownSchedule();
  31. public static getLevelData(datas, count) {
  32. let data = [];
  33. for (let i = 0; i < count; i++) {
  34. let index = 0;
  35. do {
  36. index = Global.getRandom(0, datas.length - 1);
  37. } while (data.indexOf(index) != -1);
  38. }
  39. }
  40. /**
  41. * 获取xxx-xxx之间的随机数
  42. * @param min 最小数
  43. * @param max 最大数
  44. * @returns 随机数字
  45. */
  46. public static getRandom(min, max): number {
  47. return Math.floor(Math.random() * (max - min + 1)) + min;
  48. }
  49. /**
  50. * 获取两个数字之差
  51. * @param num1
  52. * @param num2
  53. * @returns
  54. */
  55. public static getDifference(num1, num2): number {
  56. return Math.abs(num1 - num2);
  57. }
  58. public static randomSort(a) {
  59. return a.sort(() => Math.random() - 0.5);
  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. }