"use strict";
cc._RF.push(module, '2692a9zO7NEw4WXBNkq3GZj', 'choose_cake');
// choose_cake/scene/choose_cake.js

"use strict";

var _choose_cake_utils = require("../script/choose_cake_utils");

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); }); }; }

// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
var defaultData = require('../script/choose_cake_defaultData');

cc.Class({
  "extends": cc.Component,
  properties: {
    cakePrefab: {
      type: cc.Prefab,
      "default": null
    },
    starPrefab: {
      type: cc.Prefab,
      "default": null
    }
  },
  ctor: function ctor() {
    this._imageResList = [];
    this._audioResList = [];
    this._status = {
      selectedRightWordList: [],
      canCharacterWalk: false
    };
  },
  start: function start() {
    var _this = this;

    this.data = defaultData;
    this.getData(function (data) {
      if (data) {
        _this.data = data;
      }

      _this.preloadItem();
    });
    this.initListeners();
  },
  getData: function getData(func) {
    if (window && window.courseware) {
      window.courseware.getData(func, 'choose_cake');
    } else {
      func(defaultData);
    }
  },
  _imageResList: null,
  _audioResList: null,
  preloadItem: function preloadItem() {
    this.addPreloadImage();
    this.addPreloadAudio();
    this.preload();
  },
  addPreloadImage: function addPreloadImage() {},
  addPreloadAudio: function addPreloadAudio() {
    var _this$_audioResList, _this$_audioResList2;

    this._audioResList.push({
      url: this.data.audio
    });

    (_this$_audioResList = this._audioResList).push.apply(_this$_audioResList, this.data.rightWordList.map(function (data) {
      return {
        url: data.audio
      };
    }));

    (_this$_audioResList2 = this._audioResList).push.apply(_this$_audioResList2, this.data.wrongWordList.map(function (data) {
      return {
        url: data.audio
      };
    }));
  },
  preload: function preload() {
    var _this2 = this;

    var preloadArr = this._imageResList.concat(this._audioResList);

    cc.assetManager.loadAny(preloadArr, null, null, function (err, data) {
      _this2.hideAirClassLoading();
    });

    if (preloadArr.length == 0) {
      this.hideAirClassLoading();
    }
  },
  hideAirClassLoading: function hideAirClassLoading() {
    if (window && window["air"]) {
      window["air"].hideAirClassLoading();
    }

    console.log('hideAirClassLoading');
  },
  initListeners: function initListeners() {
    var _this3 = this;

    var btnStart = cc.find('Canvas/BtnStart');
    btnStart.on('click', function () {
      cc.tween(btnStart).to(0.3, {
        scale: 3,
        opacity: 0
      }).call(function () {
        btnStart.active = false;

        _this3.gameStart();
      }).start();
    });
    var btnRestart = cc.find('Canvas/BtnRestart');
    btnRestart.on('click', function () {
      cc.tween(btnRestart).to(0.3, {
        scale: 3,
        opacity: 0
      }).call(function () {
        btnRestart.active = false;
        cc.audioEngine.stopAll();
        cc.director.loadScene('choose_cake');
      }).start();
    });
    var nodeDesk = cc.find('Canvas/NodeDesk');
    nodeDesk.on('touchstart', function (event) {
      _this3.onTouchDeskStart({
        x: event.getLocationX() - nodeDesk.width * nodeDesk.anchorX,
        y: event.getLocationY() - nodeDesk.height * nodeDesk.anchorY
      });
    });
  },
  onTouchDeskStart: function onTouchDeskStart(pos) {
    if (this._status.canCharacterWalk) {
      this.characterWalk({
        x: pos.x,
        y: pos.y + 50
      });
    }
  },
  characterWalk: function characterWalk(pos, speed, changeActive) {
    if (speed === void 0) {
      speed = 1;
    }

    if (changeActive === void 0) {
      changeActive = false;
    }

    return new Promise(function (resolve, reject) {
      var nodeCharacter = cc.find('Canvas/NodeCharacter');
      nodeCharacter.stopAllActions();
      var walkVec = cc.v2(nodeCharacter.x - pos.x, nodeCharacter.y - pos.y);
      var time = walkVec.mag() / 300;
      var nodeCharacterUp = cc.find('Canvas/NodeCharacter/NodeCharacterUp');
      var nodeCharacterDown = cc.find('Canvas/NodeCharacter/NodeCharacterDown');
      var nodeCharacterLeft = cc.find('Canvas/NodeCharacter/NodeCharacterLeft');
      var nodeCharacterRight = cc.find('Canvas/NodeCharacter/NodeCharacterRight');
      var nodeCharacterFront = cc.find('Canvas/NodeCharacter/NodeCharacterFront');
      nodeCharacterUp.active = false;
      nodeCharacterDown.active = false;
      nodeCharacterLeft.active = false;
      nodeCharacterRight.active = false;
      nodeCharacterFront.active = false;

      if (walkVec.x >= walkVec.y && walkVec.x >= -walkVec.y) {
        nodeCharacterLeft.active = true;
        (0, _choose_cake_utils.asyncPlayDragonBoneAnimation)(nodeCharacterLeft, 'left', -1);
      }

      if (walkVec.x < walkVec.y && walkVec.x < -walkVec.y) {
        nodeCharacterRight.active = true;
        (0, _choose_cake_utils.asyncPlayDragonBoneAnimation)(nodeCharacterRight, 'right', -1);
      }

      if (walkVec.x >= walkVec.y && walkVec.x < -walkVec.y) {
        nodeCharacterUp.active = true;
        (0, _choose_cake_utils.asyncPlayDragonBoneAnimation)(nodeCharacterUp, 'back', -1);
      }

      if (walkVec.x < walkVec.y && walkVec.x >= -walkVec.y) {
        nodeCharacterDown.active = true;
        (0, _choose_cake_utils.asyncPlayDragonBoneAnimation)(nodeCharacterDown, 'heads', -1);
      }

      cc.tween(nodeCharacter).to(time * speed, {
        x: pos.x,
        y: pos.y
      }).call(function () {
        if (!changeActive) {
          nodeCharacterUp.active = false;
          nodeCharacterLeft.active = false;
          nodeCharacterRight.active = false;
          nodeCharacterDown.active = false;
          nodeCharacterFront.active = true;
        }

        resolve();
      }).start();
    });
  },
  gameStart: function gameStart() {
    this.playEffect('bgm', true, 0.5);
    this.showCakes();
    this.showCharacter();
  },
  showCharacter: function showCharacter() {
    var _this4 = this;

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return _this4.openDeskDoor();

            case 2:
              _context.next = 4;
              return _this4.riseCharacter();

            case 4:
              _context.next = 6;
              return _this4.closeDeskDoor();

            case 6:
              _this4._status.canCharacterWalk = true;
              (0, _choose_cake_utils.asyncPlayEffectByUrl)(_this4.data.audio);

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }))();
  },
  showCakes: function showCakes() {
    var _this5 = this;

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
      var _this5$data, rightWordList, wrongWordList, wordList;

      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _this5$data = _this5.data, rightWordList = _this5$data.rightWordList, wrongWordList = _this5$data.wrongWordList;
              wordList = (0, _choose_cake_utils.randomSortByArr)([].concat(rightWordList.map(function (data) {
                return data.word;
              }), wrongWordList.map(function (data) {
                return data.word;
              })));
              wordList.forEach(function (data, idx) {
                var cakeNode = cc.instantiate(_this5.cakePrefab);
                cakeNode.getComponent('choose_cake_Cake').setWord(data);
                cakeNode.zIndex = 12;
                cakeNode.opacity = 0;
                cakeNode.y = 500;
                cakeNode.parent = _this5.node;
                cakeNode.name = "CakeNode_" + idx;
                var posNode = cc.find("Canvas/PosNodeBase/CakePosNode_" + idx);
                var targetPos = (0, _choose_cake_utils.exchangeNodePos)(cakeNode.parent, posNode);
                cakeNode.on('click', function () {
                  _this5.onClickCake(idx);
                });
                cc.tween(cakeNode).to(0.1 * idx, {
                  opacity: 255
                }).to(0.5, {
                  x: targetPos.x,
                  y: targetPos.y
                }, {
                  easing: 'cubicIn'
                }).call(function () {
                  _this5.playEffect('落地');
                }).start();
              });

            case 3:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }))();
  },
  onClickCake: function onClickCake(idx) {
    var _this6 = this;

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
      var cakeNode;
      return regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              if (_this6._status.canCharacterWalk) {
                _context3.next = 2;
                break;
              }

              return _context3.abrupt("return");

            case 2:
              cakeNode = cc.find("Canvas/CakeNode_" + idx);
              _this6._status.canCharacterWalk = false;
              _context3.next = 6;
              return _this6.characterWalk(cakeNode.getPosition());

            case 6:
              _context3.next = 8;
              return (0, _choose_cake_utils.asyncTweenBy)(cakeNode, 0.7, {
                y: 100
              });

            case 8:
              _this6.playCakeEffect(cakeNode);

              _this6.characterGetCake(cakeNode);

              _context3.next = 12;
              return _this6.characterWalk({
                x: 0,
                y: 50
              });

            case 12:
              _this6.characterReleaseCake(cakeNode);

              if (!_this6.checkCake(cakeNode)) {
                _context3.next = 18;
                break;
              }

              _context3.next = 16;
              return _this6.npcPlayAnimRight(cakeNode);

            case 16:
              _context3.next = 20;
              break;

            case 18:
              _context3.next = 20;
              return _this6.npcPlayAnimWrong(cakeNode, idx);

            case 20:
              if (!_this6.checkOver()) {
                _context3.next = 23;
                break;
              }

              _this6.showOver();

              return _context3.abrupt("return");

            case 23:
              (0, _choose_cake_utils.asyncPlayEffectByUrl)(_this6.data.audio);
              _this6._status.canCharacterWalk = true;

            case 25:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    }))();
  },
  playCakeEffect: function playCakeEffect(cakeNode) {
    var word = cakeNode.getComponent('choose_cake_Cake').word;
    var wordData = [].concat(this.data.rightWordList, this.data.wrongWordList).find(function (data) {
      return data.word == word;
    });
    (0, _choose_cake_utils.asyncPlayEffectByUrl)(wordData.audio);
  },
  characterGetCake: function characterGetCake(cakeNode) {
    cakeNode.parent = cc.find('Canvas/NodeCharacter');
    cakeNode.x = 0;
    cakeNode.y = 100;
  },
  characterReleaseCake: function characterReleaseCake(cakeNode) {
    var pos = (0, _choose_cake_utils.exchangeNodePos)(cakeNode.parent, this.node);
    cakeNode.parent = this.node;
    cakeNode.x = pos.x;
    cakeNode.y = pos.y + 200;
  },
  checkCake: function checkCake(cakeNode) {
    var word = cakeNode.getComponent('choose_cake_Cake').word;

    if (this.data.rightWordList.findIndex(function (data) {
      return data.word == word;
    }) == -1) {
      return false;
    }

    this._status.selectedRightWordList.push(word);

    return true;
  },
  checkOver: function checkOver() {
    return this._status.selectedRightWordList.length == this.data.rightWordList.length;
  },
  showOver: function showOver() {
    var _this7 = this;

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
      return regeneratorRuntime.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              _this7.hideNpc();

              _context4.next = 3;
              return _this7.hideCakes();

            case 3:
              _context4.next = 5;
              return _this7.showStars();

            case 5:
              _context4.next = 7;
              return _this7.moveCamera();

            case 7:
              _context4.next = 9;
              return _this7.openDoor();

            case 9:
              _context4.next = 11;
              return _this7.characterMoveOut();

            case 11:
              _context4.next = 13;
              return _this7.showRestartBtn();

            case 13:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    }))();
  },
  hideNpc: function hideNpc() {
    var nodeNpc = cc.find('Canvas/NodeNpc');
    cc.tween(nodeNpc).by(1, {
      y: 350
    }).repeatForever().start();
  },
  hideCakes: function hideCakes() {
    for (var i = 0; i < 5; i++) {
      var cakeNode = cc.find("Canvas/CakeNode_" + i);

      if (!cakeNode) {
        continue;
      } // cakeNode.zIndex = 100;


      cc.tween(cakeNode).by(1, {
        y: 350
      }).repeatForever().start();
    }
  },
  showStars: function showStars() {
    this.playEffect('星星');

    for (var index = 0; index < 70; index++) {
      var starNode = cc.instantiate(this.starPrefab);
      starNode.x = (0, _choose_cake_utils.RandomInt)(-640, 640);
      starNode.y = (0, _choose_cake_utils.RandomInt)(360, 1800);
      starNode.parent = this.node;
      starNode.zIndex = 20;
      cc.tween(starNode).by(1, {
        y: -300,
        angle: 360
      }).repeatForever().start();
      cc.tween(starNode).delay(1 + Math.random() * 3).to(1, {
        opacity: 0
      }).start();
    }
  },
  moveCamera: function moveCamera() {
    var camera = cc.find('Canvas/Main Camera');
    cc.tween(camera).delay(2).by(1, {
      y: 1036 - 720
    }).start();
  },
  openDoor: function openDoor() {
    var _this8 = this;

    var delayTime = 2;
    var nodeWheel1 = cc.find('Canvas/NodeDoorMask/NodeWheel1');
    var nodeWheel2 = cc.find('Canvas/NodeDoorMask/NodeWheel2');
    var nodeWheel3 = cc.find('Canvas/NodeDoorMask/NodeWheel3');
    cc.tween(nodeWheel1).delay(delayTime).call(function () {
      _this8.playEffect('门');
    }).by(1, {
      x: 200
    }).start();
    cc.tween(nodeWheel2).delay(delayTime).by(1, {
      x: -200
    }).start();
    cc.tween(nodeWheel3).delay(delayTime).by(1, {
      y: -200
    }).start();
  },
  characterMoveOut: function characterMoveOut() {
    var _this9 = this;

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() {
      var nodeCharacterUp, nodeCharacterFront, nodeDoorFront;
      return regeneratorRuntime.wrap(function _callee5$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              _context5.next = 2;
              return _this9.characterWalk({
                x: 0,
                y: 380
              }, 4, true);

            case 2:
              nodeCharacterUp = cc.find('Canvas/NodeCharacter/NodeCharacterUp');
              nodeCharacterFront = cc.find('Canvas/NodeCharacter/NodeCharacterFront');
              nodeCharacterUp.active = true;
              nodeCharacterFront.active = false;
              nodeDoorFront = cc.find('Canvas/NodeDoorFront');
              nodeDoorFront.zIndex = 100;
              cc.tween(nodeDoorFront).to(1, {
                opacity: 255
              }).start();

            case 9:
            case "end":
              return _context5.stop();
          }
        }
      }, _callee5);
    }))();
  },
  showRestartBtn: function showRestartBtn() {
    var btnRestart = cc.find('Canvas/BtnRestart');
    btnRestart.active = true;
  },
  npcPlayAnimRight: function npcPlayAnimRight(cakeNode) {
    var _this10 = this;

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() {
      var nodeNpcNormal, nodeNpcRight;
      return regeneratorRuntime.wrap(function _callee6$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              _this10.playEffect('吃');

              nodeNpcNormal = cc.find('Canvas/NodeNpc/NodeNpcNormal');
              nodeNpcRight = cc.find('Canvas/NodeNpc/NodeNpcRight');
              nodeNpcNormal.active = false;
              nodeNpcRight.active = true;
              cakeNode.zIndex = 0;
              cc.tween(cakeNode).to(0.1, {
                scale: 0.7
              }).to(1, {
                scale: 0
              }).call(function () {
                _this10.playEffect('正确');
              }).start();
              _context6.next = 9;
              return (0, _choose_cake_utils.asyncPlayDragonBoneAnimation)(nodeNpcRight, 'npcright', 1);

            case 9:
              nodeNpcNormal.active = true;
              nodeNpcRight.active = false; // cakeNode

            case 11:
            case "end":
              return _context6.stop();
          }
        }
      }, _callee6);
    }))();
  },
  npcPlayAnimWrong: function npcPlayAnimWrong(cakeNode, idx) {
    var _this11 = this;

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7() {
      var nodeNpcNormal, nodeNpcWrong, posNode;
      return regeneratorRuntime.wrap(function _callee7$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              _this11.playEffect('吃');

              nodeNpcNormal = cc.find('Canvas/NodeNpc/NodeNpcNormal');
              nodeNpcWrong = cc.find('Canvas/NodeNpc/NodeNpcWrong');
              nodeNpcNormal.active = false;
              nodeNpcWrong.active = true;
              cakeNode.zIndex = 0;
              posNode = cc.find("Canvas/PosNodeBase/CakePosNode_" + idx);
              cc.tween(cakeNode).to(0.1, {
                scale: 0.7
              }).delay(1.5).call(function () {
                _this11.playEffect('错误');
              }).to(0.5, {
                x: posNode.x,
                y: posNode.y,
                scale: 1
              }, {
                easing: 'cubicOut'
              }).call(function () {
                _this11.playEffect('落地');
              }).start();
              _context7.next = 10;
              return (0, _choose_cake_utils.asyncPlayDragonBoneAnimation)(nodeNpcWrong, 'npcwrong', 1);

            case 10:
              cakeNode.zIndex = 12;
              nodeNpcNormal.active = true;
              nodeNpcWrong.active = false;

            case 13:
            case "end":
              return _context7.stop();
          }
        }
      }, _callee7);
    }))();
  },
  openDeskDoor: function openDeskDoor() {
    return new Promise(function (resolve, reject) {
      var nodeDoorLeft = cc.find('Canvas/NodeDoorLeft');
      cc.tween(nodeDoorLeft).to(0.5, {
        x: -150
      }).start();
      var nodeDoorRight = cc.find('Canvas/NodeDoorRight');
      cc.tween(nodeDoorRight).to(0.5, {
        x: 150
      }).call(resolve).start();
    });
  },
  closeDeskDoor: function closeDeskDoor() {
    return new Promise(function (resolve, reject) {
      var nodeDoorLeft = cc.find('Canvas/NodeDoorLeft');
      cc.tween(nodeDoorLeft).to(0.5, {
        x: 0
      }).start();
      var nodeDoorRight = cc.find('Canvas/NodeDoorRight');
      cc.tween(nodeDoorRight).to(0.5, {
        x: 0
      }).call(resolve).start();
    });
  },
  riseCharacter: function riseCharacter() {
    return new Promise(function (resolve, reject) {
      var nodeDeskMask = cc.find('Canvas/NodeDeskMask');
      nodeDeskMask.zIndex = 11;
      var nodeCharacter = cc.find('Canvas/NodeCharacter');
      nodeCharacter.zIndex = 10;
      cc.tween(nodeCharacter).to(1, {
        y: 75
      }).call(function () {
        nodeDeskMask.zIndex = 0;
        resolve();
      }).start();
    });
  },
  playEffect: function playEffect(name, loop, volume) {
    var musicComponent = cc.find('Canvas/MusicNode').getComponent('choose_cake_MusicNode');
    musicComponent.playEffect(name, loop, volume);
  } // update (dt) {},

});

cc._RF.pop();