"use strict";
cc._RF.push(module, '3cf09wwU29CroO7y7PFzZPr', 'utils');
// Lwd/script/common/utils.js

"use strict";

exports.__esModule = true;
exports.getPosByAngle = getPosByAngle;
exports.getAngleByPos = getAngleByPos;
exports.exchangeNodePos = exchangeNodePos;
exports.RandomInt = RandomInt;
exports.Between = Between;
exports.randomSortByArr = randomSortByArr;
exports.asyncTweenTo = asyncTweenTo;
exports.asyncTweenBy = asyncTweenBy;
exports.asyncPlayDragonBoneAnimation = asyncPlayDragonBoneAnimation;
exports.asyncPlayEffectByUrl = asyncPlayEffectByUrl;
exports.jelly = jelly;
exports.asyncDelay = asyncDelay;
exports.showFireworks = showFireworks;

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

lwd_g.utils = {
  // 范围随机
  randFromTo: function randFromTo(_min, _max) {
    var min = parseFloat(_min);
    var max = parseFloat(_max);
    return min + Math.random() * (max - min);
  },
  // 范围随机一个整数
  // 例：1~3，返回的可能值为：1、2、3
  randFromTo_Int: function randFromTo_Int(min, max) {
    var val = this.randFromTo(min, max + 1 - 0.0001);
    return Math.floor(val);
  },
  // 深拷贝
  deepCopy: function deepCopy(src) {
    var cpy = src instanceof Array ? [] : {};

    for (var i in src) {
      cpy[i] = typeof src[i] === 'object' ? this.deepCopy(src[i]) : src[i];
    }

    return cpy;
  },
  deletList: function deletList(list, val) {
    var index = list(val);

    if (index > -1) {
      this.splice(index, 1);
    }
  }
};

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 Between(a, b, c) {
  return [a, b, c].sort(function (a, b) {
    return a - b;
  })[1];
}

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 asyncTweenTo(_x, _x2, _x3, _x4) {
  return _asyncTweenTo.apply(this, arguments);
}

function _asyncTweenTo() {
  _asyncTweenTo = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(node, duration, obj, ease) {
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (ease === void 0) {
              ease = undefined;
            }

            return _context.abrupt("return", new Promise(function (resolve, reject) {
              cc.tween(node).to(duration, obj, ease).call(function () {
                resolve();
              }).start();
            }));

          case 2:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _asyncTweenTo.apply(this, arguments);
}

function asyncTweenBy(_x5, _x6, _x7, _x8) {
  return _asyncTweenBy.apply(this, arguments);
}

function _asyncTweenBy() {
  _asyncTweenBy = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(node, duration, obj, ease) {
    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            if (ease === void 0) {
              ease = undefined;
            }

            return _context2.abrupt("return", new Promise(function (resolve, reject) {
              cc.tween(node).by(duration, obj, ease).call(function () {
                resolve();
              }).start();
            }));

          case 2:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _asyncTweenBy.apply(this, arguments);
}

function asyncPlayDragonBoneAnimation(_x9, _x10, _x11, _x12) {
  return _asyncPlayDragonBoneAnimation.apply(this, arguments);
}

function _asyncPlayDragonBoneAnimation() {
  _asyncPlayDragonBoneAnimation = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(node, animationName, time, onFrameEvent) {
    return regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            if (time === void 0) {
              time = 1;
            }

            return _context3.abrupt("return", new Promise(function (resolve, reject) {
              node.getComponent(dragonBones.ArmatureDisplay).once(dragonBones.EventObject.COMPLETE, function () {
                resolve();
              });
              node.getComponent(dragonBones.ArmatureDisplay).on(dragonBones.EventObject.FRAME_EVENT, function (_ref) {
                var name = _ref.name;

                if (onFrameEvent && typeof onFrameEvent == 'function') {
                  onFrameEvent(name);
                }
              });
              node.getComponent(dragonBones.ArmatureDisplay).playAnimation(animationName, time);
            }));

          case 2:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _asyncPlayDragonBoneAnimation.apply(this, arguments);
}

function asyncPlayEffectByUrl(_x13, _x14) {
  return _asyncPlayEffectByUrl.apply(this, arguments);
}

function _asyncPlayEffectByUrl() {
  _asyncPlayEffectByUrl = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(url, loop) {
    return regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            if (loop === void 0) {
              loop = false;
            }

            return _context4.abrupt("return", new Promise(function (resolve, reject) {
              cc.assetManager.loadRemote(url, function (err, clip) {
                console.log(clip);
                cc.audioEngine.playEffect(clip, loop);
                resolve();
              });
            }));

          case 2:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _asyncPlayEffectByUrl.apply(this, arguments);
}

function jelly(_x15) {
  return _jelly.apply(this, arguments);
}

function _jelly() {
  _jelly = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(node) {
    return regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            return _context5.abrupt("return", new Promise(function (resolve, reject) {
              cc.tween(node).to(0.1, {
                scaleX: 0.9,
                scaleY: 1.1
              }).to(0.1, {
                scaleX: 1.1,
                scaleY: 0.9
              }).to(0.1, {
                scaleX: 1,
                scaleY: 1
              }).call(resolve).start();
            }));

          case 1:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _jelly.apply(this, arguments);
}

function asyncDelay(_x16) {
  return _asyncDelay.apply(this, arguments);
}

function _asyncDelay() {
  _asyncDelay = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6(time) {
    return regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            return _context6.abrupt("return", new Promise(function (resolve, reject) {
              setTimeout(function () {
                resolve();
              }, time * 1000);
            }));

          case 1:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _asyncDelay.apply(this, arguments);
}

function showFireworks(_x17, _x18, _x19, _x20, _x21, _x22) {
  return _showFireworks.apply(this, arguments);
}

function _showFireworks() {
  _showFireworks = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8(baseNode, nodeList, pos, side, range, number) {
    return regeneratorRuntime.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            if (pos === void 0) {
              pos = cc.v2(0, 0);
            }

            if (side === void 0) {
              side = cc.v2(0, 100);
            }

            if (range === void 0) {
              range = 50;
            }

            if (number === void 0) {
              number = 100;
            }

            new Array(number).fill(' ').forEach( /*#__PURE__*/function () {
              var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7(_, i) {
                var rabbonNode, node, rate, angle;
                return regeneratorRuntime.wrap(function _callee7$(_context7) {
                  while (1) {
                    switch (_context7.prev = _context7.next) {
                      case 0:
                        rabbonNode = new cc.Node();
                        rabbonNode.parent = baseNode;
                        rabbonNode.x = pos.x;
                        rabbonNode.y = pos.y;
                        rabbonNode.angle = 60 * Math.random() - 30;
                        node = cc.instantiate(nodeList[RandomInt(nodeList.length)]);
                        node.parent = rabbonNode;
                        node.active = true;
                        node.x = 0;
                        node.y = 0;
                        node.angle = 0;
                        rate = Math.random();
                        angle = Math.PI * (Math.random() * 2 - 1);
                        _context7.next = 15;
                        return asyncTweenBy(rabbonNode, 0.3, {
                          x: side.x * rate + Math.cos(angle) * range * rate,
                          y: side.y * rate + Math.sin(angle) * range * rate
                        }, {
                          easing: 'quadIn'
                        });

                      case 15:
                        cc.tween(rabbonNode).by(8, {
                          y: -2000
                        }).start();
                        rabbonFall(rabbonNode);
                        _context7.next = 19;
                        return asyncDelay(Math.random());

                      case 19:
                        cc.tween(node).by(0.15, {
                          x: -10,
                          angle: -10
                        }).by(0.3, {
                          x: 20,
                          angle: 20
                        }).by(0.15, {
                          x: -10,
                          angle: -10
                        }).union().repeatForever().start();
                        cc.tween(rabbonNode).delay(5).to(0.3, {
                          opacity: 0
                        }).call(function () {
                          node.stopAllActions();
                          node.active = false;
                          node.parent = null;
                          node = null;
                        }).start();

                      case 21:
                      case "end":
                        return _context7.stop();
                    }
                  }
                }, _callee7);
              }));

              return function (_x24, _x25) {
                return _ref2.apply(this, arguments);
              };
            }());

          case 5:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  }));
  return _showFireworks.apply(this, arguments);
}

function rabbonFall(_x23) {
  return _rabbonFall.apply(this, arguments);
}

function _rabbonFall() {
  _rabbonFall = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9(node) {
    var time, offsetX;
    return regeneratorRuntime.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            time = 1 + Math.random();
            offsetX = RandomInt(-200, 200) * time;
            _context9.next = 4;
            return asyncTweenBy(node, time, {
              x: offsetX,
              angle: offsetX * 60 / 200
            });

          case 4:
            rabbonFall(node);

          case 5:
          case "end":
            return _context9.stop();
        }
      }
    }, _callee9);
  }));
  return _rabbonFall.apply(this, arguments);
}

cc._RF.pop();