util.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import * as PIXI from "pixi.js";
  2. const { resources } = PIXI.loader;
  3. export function isDev() {
  4. return (
  5. /^test|\.webdev2\./.test(window.location.host) ||
  6. window.location.port !== ""
  7. );
  8. }
  9. /**
  10. * 获取Url上指定参数
  11. * @param {String} name 参数名
  12. */
  13. export function getUrlParam(name) {
  14. var reg = new RegExp("[?&]" + name + "=([^&#?]*)(&|#|$)");
  15. var r = window.location.href.match(reg);
  16. return r ? r[1] : null;
  17. }
  18. /**
  19. * @desc PIXI资源加载(顺序加载,避免出错)
  20. * @param {array} res 资源列表
  21. */
  22. let loadingState = false;
  23. let loadQueen = [];
  24. export function loaderRes(_res, fn, context, version = false) {
  25. let res = [...new Set(_res)]; // 去重
  26. if (loadingState) {
  27. loadQueen.push([res, fn, context, version]);
  28. return;
  29. }
  30. loadingState = true;
  31. let loadRes = res.filter(item => resources[item] == null);
  32. if (loadRes.length > 0) {
  33. try {
  34. PIXI.loader.baseUrl = isDev()
  35. ? ""
  36. : `https://pub.dwstatic.com/dwgame/decibel/`;
  37. // PIXI.loader.defaultQueryString = version ? resVersion : "";
  38. PIXI.loader.add(loadRes).load(() => {
  39. fn.call(context);
  40. loadingState = false;
  41. loadQueen.length && loaderRes(...loadQueen.pop());
  42. });
  43. } catch (e) {
  44. loadingState = false;
  45. loadQueen.length && loaderRes(...loadQueen.pop());
  46. }
  47. } else {
  48. fn.call(context);
  49. loadingState = false;
  50. }
  51. }
  52. export function hitTestRectangle(r1, r2) {
  53. //Define the variables we'll need to calculate
  54. let hit, combinedHalfWidths, combinedHalfHeights, vx, vy;
  55. //hit will determine whether there's a collision
  56. hit = false;
  57. //Find the center points of each sprite
  58. r1.centerX = r1.x + r1.width / 2;
  59. r1.centerY = r1.y + r1.height / 2;
  60. r2.centerX = r2.x + r2.width / 2;
  61. r2.centerY = r2.y + r2.height / 2;
  62. //Find the half-widths and half-heights of each sprite
  63. r1.halfWidth = r1.width / 2;
  64. r1.halfHeight = r1.height / 2;
  65. r2.halfWidth = r2.width / 2;
  66. r2.halfHeight = r2.height / 2;
  67. //Calculate the distance vector between the sprites
  68. vx = r1.centerX - r2.centerX;
  69. vy = r1.centerY - r2.centerY;
  70. //Figure out the combined half-widths and half-heights
  71. combinedHalfWidths = r1.halfWidth + r2.halfWidth;
  72. combinedHalfHeights = r1.halfHeight + r2.halfHeight;
  73. //Check for a collision on the x axis
  74. if (Math.abs(vx) < combinedHalfWidths) {
  75. //A collision might be occuring. Check for a collision on the y axis
  76. if (Math.abs(vy) < combinedHalfHeights) {
  77. //There's definitely a collision happening
  78. hit = true;
  79. } else {
  80. //There's no collision on the y axis
  81. hit = false;
  82. }
  83. } else {
  84. //There's no collision on the x axis
  85. hit = false;
  86. }
  87. //`hit` will be either `true` or `false`
  88. return hit;
  89. }