"use strict";
cc._RF.push(module, 'ade7a9A1W1Ah7vGKIj+9VNT', 'util');
// tmpGame/script/util.js

"use strict";

exports.__esModule = true;
exports.getPosByAngle = getPosByAngle;
exports.getAngleByPos = getAngleByPos;
exports.exchangeNodePos = exchangeNodePos;
exports.RandomInt = RandomInt;
exports.randomSortByArr = randomSortByArr;
exports.setSprNodeMaxLen = setSprNodeMaxLen;
exports.localPosTolocalPos = localPosTolocalPos;
exports.worldPosToLocalPos = worldPosToLocalPos;
exports.getScaleRateBy2Node = getScaleRateBy2Node;
exports.getDistance = getDistance;
exports.playAudioByUrl = playAudioByUrl;
exports.btnClickAnima = btnClickAnima;

function getPosByAngle(angle, len) {
  var radian = angle * Math.PI / 180;
  var x = Math.sin(radian) * len;
  var y = Math.cos(radian) * len;
  return {
    x: x,
    y: y
  };
}

function getAngleByPos(px, py, mx, my) {
  var x = Math.abs(px - mx);
  var y = Math.abs(py - my);
  var z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  var cos = y / z;
  var radina = Math.acos(cos); // 用反三角函数求弧度

  var angle = Math.floor(180 / (Math.PI / radina) * 100) / 100; // 将弧度转换成角度

  if (mx > px && my > py) {
    // 鼠标在第四象限
    angle = 180 - angle;
  }

  if (mx === px && my > py) {
    // 鼠标在y轴负方向上
    angle = 180;
  }

  if (mx > px && my === py) {
    // 鼠标在x轴正方向上
    angle = 90;
  }

  if (mx < px && my > py) {
    // 鼠标在第三象限
    angle = 180 + angle;
  }

  if (mx < px && my === py) {
    // 鼠标在x轴负方向
    angle = 270;
  }

  if (mx < px && my < py) {
    // 鼠标在第二象限
    angle = 360 - angle;
  } // console.log('angle: ', angle);


  return angle;
}

function exchangeNodePos(baseNode, targetNode) {
  return baseNode.convertToNodeSpaceAR(targetNode._parent.convertToWorldSpaceAR(cc.v2(targetNode.x, targetNode.y)));
}

function RandomInt(a, b) {
  if (b === void 0) {
    b = 0;
  }

  var max = Math.max(a, b);
  var min = Math.min(a, b);
  return Math.floor(Math.random() * (max - min) + min);
}

function randomSortByArr(arr) {
  var newArr = [];
  var tmpArr = arr.concat();

  while (tmpArr.length > 0) {
    var randomIndex = Math.floor(tmpArr.length * Math.random());
    newArr.push(tmpArr[randomIndex]);
    tmpArr.splice(randomIndex, 1);
  }

  return newArr;
}

function setSprNodeMaxLen(sprNode, maxW, maxH) {
  var sx = maxW / sprNode.width;
  var sy = maxH / sprNode.height;
  var s = Math.min(sx, sy);
  sprNode.scale = Math.round(s * 1000) / 1000;
}

function localPosTolocalPos(baseNode, targetNode) {
  var worldPos = targetNode.parent.convertToWorldSpaceAR(cc.v2(targetNode.x, targetNode.y));
  var localPos = baseNode.parent.convertToNodeSpaceAR(cc.v2(worldPos.x, worldPos.y));
  return localPos;
}

function worldPosToLocalPos(worldPos, baseNode) {
  var localPos = baseNode.parent.convertToNodeSpaceAR(cc.v2(worldPos.x, worldPos.y));
  return localPos;
}

function getScaleRateBy2Node(baseNode, targetNode, maxFlag) {
  if (maxFlag === void 0) {
    maxFlag = true;
  }

  var worldRect1 = targetNode.getBoundingBoxToWorld();
  var worldRect2 = baseNode.getBoundingBoxToWorld();
  var sx = worldRect1.width / worldRect2.width;
  var sy = worldRect1.height / worldRect2.height;

  if (maxFlag) {
    return Math.max(sx, sy);
  } else {
    return Math.min(sx, sy);
  }
}

function getDistance(start, end) {
  var pos = cc.v2(start.x - end.x, start.y - end.y);
  var dis = Math.sqrt(pos.x * pos.x + pos.y * pos.y);
  return dis;
}

function playAudioByUrl(audio_url, cb) {
  if (cb === void 0) {
    cb = null;
  }

  if (audio_url) {
    cc.assetManager.loadRemote(audio_url, function (err, audioClip) {
      var audioId = cc.audioEngine.play(audioClip, false, 0.8);

      if (cb) {
        cc.audioEngine.setFinishCallback(audioId, function () {
          cb();
        });
      }
    });
  }
}

function btnClickAnima(btn, time, rate) {
  if (time === void 0) {
    time = 0.15;
  }

  if (rate === void 0) {
    rate = 1.05;
  }

  btn.tmpScale = btn.scale;
  btn.on(cc.Node.EventType.TOUCH_START, function () {
    cc.tween(btn).to(time / 2, {
      scale: btn.scale * rate
    }).start();
  });
  btn.on(cc.Node.EventType.TOUCH_CANCEL, function () {
    cc.tween(btn).to(time / 2, {
      scale: btn.tmpScale
    }).start();
  });
  btn.on(cc.Node.EventType.TOUCH_END, function () {
    cc.tween(btn).to(time / 2, {
      scale: btn.tmpScale
    }).start();
  });
}

cc._RF.pop();