import * as PIXI from "pixi.js"; const { resources } = PIXI.loader; export function isDev() { return ( /^test|\.webdev2\./.test(window.location.host) || window.location.port !== "" ); } /** * 获取Url上指定参数 * @param {String} name 参数名 */ export function getUrlParam(name) { var reg = new RegExp("[?&]" + name + "=([^&#?]*)(&|#|$)"); var r = window.location.href.match(reg); return r ? r[1] : null; } /** * @desc PIXI资源加载(顺序加载,避免出错) * @param {array} res 资源列表 */ let loadingState = false; let loadQueen = []; export function loaderRes(_res, fn, context, version = false) { let res = [...new Set(_res)]; // 去重 if (loadingState) { loadQueen.push([res, fn, context, version]); return; } loadingState = true; let loadRes = res.filter(item => resources[item] == null); if (loadRes.length > 0) { try { PIXI.loader.baseUrl = isDev() ? "" : `https://pub.dwstatic.com/dwgame/decibel/`; // PIXI.loader.defaultQueryString = version ? resVersion : ""; PIXI.loader.add(loadRes).load(() => { fn.call(context); loadingState = false; loadQueen.length && loaderRes(...loadQueen.pop()); }); } catch (e) { loadingState = false; loadQueen.length && loaderRes(...loadQueen.pop()); } } else { fn.call(context); loadingState = false; } } export function hitTestRectangle(r1, r2) { //Define the variables we'll need to calculate let hit, combinedHalfWidths, combinedHalfHeights, vx, vy; //hit will determine whether there's a collision hit = false; //Find the center points of each sprite r1.centerX = r1.x + r1.width / 2; r1.centerY = r1.y + r1.height / 2; r2.centerX = r2.x + r2.width / 2; r2.centerY = r2.y + r2.height / 2; //Find the half-widths and half-heights of each sprite r1.halfWidth = r1.width / 2; r1.halfHeight = r1.height / 2; r2.halfWidth = r2.width / 2; r2.halfHeight = r2.height / 2; //Calculate the distance vector between the sprites vx = r1.centerX - r2.centerX; vy = r1.centerY - r2.centerY; //Figure out the combined half-widths and half-heights combinedHalfWidths = r1.halfWidth + r2.halfWidth; combinedHalfHeights = r1.halfHeight + r2.halfHeight; //Check for a collision on the x axis if (Math.abs(vx) < combinedHalfWidths) { //A collision might be occuring. Check for a collision on the y axis if (Math.abs(vy) < combinedHalfHeights) { //There's definitely a collision happening hit = true; } else { //There's no collision on the y axis hit = false; } } else { //There's no collision on the x axis hit = false; } //`hit` will be either `true` or `false` return hit; }