"use strict";
cc._RF.push(module, '21f5ev7Z1lHd6RDR8CEQmn/', 'Z_04_syys');
// Z_04_syys/scene/Z_04_syys.js

"use strict";

var _Z_04_syys_utils = require("../script/Z_04_syys_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/Z_04_syys_defaultData');

cc.Class({
  "extends": cc.Component,
  properties: {
    bubblePrefab: {
      type: cc.Prefab,
      "default": null
    },
    fishPrefab: {
      type: cc.Prefab,
      "default": null
    },
    shellPrefab: {
      type: cc.Prefab,
      "default": null
    }
  },
  ctor: function ctor() {
    this._imageResList = [];
    this._audioResList = [];
    this._status = {
      treeHeight: -1
    };
  },
  start: function start() {
    var _this = this;

    cc.debug.setDisplayStats(false);
    this.data = defaultData;
    this.getData(function (data) {
      if (data) {
        _this.data = data;
      }

      _this.preloadItem();
    });
    this.initListeners();
    this.createBubbles();
    this.showKnockKnock();
    this.showFish();
    setInterval(function () {
      _this.showFish();
    }, 9000);
  },
  getData: function getData(func) {
    if (window && window.courseware) {
      window.courseware.getData(func, 'Z_04_syys');
    } 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 = this._audioResList).push.apply(_this$_audioResList, this.data.wordList.map(function (data) {
      return {
        url: data.audio
      };
    }));

    (_this$_audioResList2 = this._audioResList).push.apply(_this$_audioResList2, this.data.wordList.map(function (data) {
      return {
        url: data.backWordAudio
      };
    }));
  },
  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 btnWheel = cc.find('Canvas/NodeBg/BtnWheel');
    btnWheel.on('click', function () {
      _this3.onClickWheel();
    });
  },
  onClickWheel: function onClickWheel() {
    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:
              _this4.hideKnockKnock();

              _context.next = 3;
              return _this4.openTheDoor();

            case 3:
              _this4.characterWalk();

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

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
      var nodeWheel, nodeDoor;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              nodeWheel = cc.find('Canvas/NodeBg/NodeWheel');

              _this5.playEffect('门铃');

              _context2.next = 4;
              return (0, _Z_04_syys_utils.asyncDelay)(0.3);

            case 4:
              _this5.playEffect('旋转');

              _context2.next = 7;
              return (0, _Z_04_syys_utils.asyncTweenBy)(nodeWheel, 0.5, {
                angle: -360
              });

            case 7:
              nodeWheel.active = false;

              _this5.playEffect('开门');

              nodeDoor = cc.find('Canvas/NodeBg/NodeDoor');
              nodeDoor.active = true;
              nodeDoor.opacity = 0;
              _context2.next = 14;
              return (0, _Z_04_syys_utils.asyncTweenBy)(nodeDoor, 0.1, {
                opacity: 255
              });

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

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
      var nodeCharacterWalk, nodeCharacterBase, nodeCharacterWater, nodeCharacterNormal;
      return regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              nodeCharacterWalk = cc.find('Canvas/NodeBg/NodeCharacterBase/NodeCharacterWalk');
              nodeCharacterWalk.active = true;
              (0, _Z_04_syys_utils.asyncPlayDragonBoneAnimation)(nodeCharacterWalk, 'walk', -1);

              _this6.playEffect('走路');

              nodeCharacterBase = cc.find('Canvas/NodeBg/NodeCharacterBase');
              _context3.next = 7;
              return (0, _Z_04_syys_utils.asyncTweenTo)(nodeCharacterBase, 3, {
                x: 100
              });

            case 7:
              nodeCharacterWalk.active = false;
              nodeCharacterWater = cc.find('Canvas/NodeBg/NodeCharacterBase/NodeCharacterWater');
              nodeCharacterWater.active = true;
              cc.tween(nodeCharacterBase).delay(1.5).call(function () {
                _this6.showTree(0);
              }).delay(1).to(2.5, {
                x: 0
              }).start();

              _this6.playEffect('浇水');

              _context3.next = 14;
              return (0, _Z_04_syys_utils.asyncPlayDragonBoneAnimation)(nodeCharacterWater, 'water', 1);

            case 14:
              nodeCharacterWater.active = false;
              nodeCharacterNormal = cc.find('Canvas/NodeBg/NodeCharacterBase/NodeCharacterNormal');
              nodeCharacterNormal.active = true;
              (0, _Z_04_syys_utils.asyncPlayDragonBoneAnimation)(nodeCharacterNormal, 'normal', -1);

            case 18:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    }))();
  },
  showTree: function showTree(idx) {
    var _this7 = this;

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
      var nodeTree;
      return regeneratorRuntime.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              if (!(idx <= _this7._status.treeHeight || _this7.data.wordList.length <= idx)) {
                _context4.next = 2;
                break;
              }

              return _context4.abrupt("return");

            case 2:
              nodeTree = cc.find('Canvas/NodeBg/TreeMaskNode/NodeTree');

              _this7.playEffect('植物');

              _this7._status.treeHeight = idx;

              _this7.growTree(idx);

              _this7.growSky(idx);

              _this7.moveCamera(idx);

              _context4.next = 10;
              return (0, _Z_04_syys_utils.asyncTweenTo)(nodeTree, 3, {
                y: 500 + 200 * idx
              });

            case 10:
              _this7.playEffect('路牌');

              _this7.showShell(idx);

            case 12:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    }))();
  },
  growTree: function growTree(idx) {
    if (idx < 5) {
      return;
    }

    var nodeTree = cc.find('Canvas/NodeBg/TreeMaskNode/NodeTree');
    (0, _Z_04_syys_utils.asyncTweenTo)(nodeTree, 3, {
      scaleY: (nodeTree.height + 200 * (idx - 4)) / nodeTree.height
    });
  },
  growSky: function growSky(idx) {
    if (idx < 5) {
      return;
    }

    var nodeBg = cc.find('Canvas/NodeBg');
    (0, _Z_04_syys_utils.asyncTweenTo)(nodeBg, 3, {
      height: nodeBg.height + 200 * (idx - 4)
    });
  },
  moveCamera: function moveCamera(idx) {
    var cameraYList = [0, 0];
    this.data.wordList.forEach(function (word, idx) {
      if (idx < 2) {
        return;
      }

      cameraYList.push((idx - 1) * 200 + 50);
    });
    var camera = cc.find('Canvas/Main Camera');
    (0, _Z_04_syys_utils.asyncTweenTo)(camera, 3, {
      y: cameraYList[idx]
    });
  },
  showShell: function showShell(idx) {
    var _this8 = this;

    var nodeShell = cc.instantiate(this.shellPrefab);
    nodeShell.parent = cc.find('Canvas/NodeBg/TreeMaskNode');
    nodeShell.y = idx * 200 + 300;
    var btnChange = nodeShell.getChildByName('BtnChange');
    var labelNode = nodeShell.getChildByName('LabelNode');
    var labelNodeBack = nodeShell.getChildByName('LabelNodeBack');
    btnChange.on('click', function () {
      cc.tween(nodeShell).to(0.3, {
        scaleX: 0
      }, {
        easing: 'cubicIn'
      }).call(function () {
        labelNode.active = !labelNode.active;
        labelNode.scaleX = labelNode.scaleX == 0 ? 1 : 0;
        labelNodeBack.active = !labelNodeBack.active;
        labelNodeBack.scaleX = labelNodeBack.scaleX == 0 ? 1 : 0;
      }).to(0.3, {
        scaleX: 1
      }, {
        easing: 'cubicOut'
      }).start();
    });

    if (this.data.wordList[idx].backWord == '') {
      btnChange.active = false;
    }

    labelNode.on('click', function () {
      (0, _Z_04_syys_utils.asyncPlayEffectByUrl)(_this8.data.wordList[idx].audio);

      if (_this8.data.wordList[idx].backWord == '') {
        _this8.showTree(idx + 1);

        return;
      }
    });
    labelNodeBack.on('click', function () {
      (0, _Z_04_syys_utils.asyncPlayEffectByUrl)(_this8.data.wordList[idx].backWordAudio);

      _this8.showTree(idx + 1);
    });
    labelNode.getComponent(cc.Label).string = this.data.wordList[idx].word;
    labelNodeBack.active = true;
    labelNodeBack.scaleX = 1;
    labelNodeBack.getComponent(cc.Label).string = this.data.wordList[idx].backWord;
    labelNodeBack.active = false;
    labelNodeBack.scaleX = 0;
    cc.tween(nodeShell).to(0.3, {
      scaleX: 1
    }, {
      easing: 'cubicIn'
    }).start();
  },
  createBubbles: function createBubbles() {
    var _this9 = this;

    setInterval(function () {
      if (Math.random() < 0.6) {
        return;
      }

      var speedX = (0, _Z_04_syys_utils.RandomInt)(30, 70);
      var speedY = (0, _Z_04_syys_utils.RandomInt)(20, 40);
      var nodeBubblePos = cc.find('Canvas/NodeBg/NodeBubblePos');
      var bubble = cc.instantiate(_this9.bubblePrefab);
      bubble.parent = nodeBubblePos;
      bubble.scale = 0;
      var bubbleScale = Math.random() * 0.7 + 0.3;
      cc.tween(bubble).to(0.7, {
        scale: bubbleScale,
        x: speedX,
        y: speedY
      }).by(1, {
        x: speedX,
        y: speedY
      }).repeatForever().start();
      cc.tween(bubble).to(0.3 + Math.random() * 0.1, {
        scaleX: 0.95 * bubbleScale,
        scaleY: 1.05 * bubbleScale
      }).to(0.3 + Math.random() * 0.1, {
        scaleX: 1.05 * bubbleScale,
        scaleY: 0.95 * bubbleScale
      }).union().repeatForever().start();
    }, 900);
  },
  showKnockKnock: function showKnockKnock() {
    var nodeKnockMask = cc.find('Canvas/NodeBg/NodeKnockMask');
    var heightList = [40, 80, 200, 200, 0];
    var idx = 0;
    setInterval(function () {
      if (idx >= heightList.length) {
        idx = 0;
      }

      nodeKnockMask.height = heightList[idx];
      idx++;
    }, 500);
  },
  hideKnockKnock: function hideKnockKnock() {
    var nodeKnockMask = cc.find('Canvas/NodeBg/NodeKnockMask');
    nodeKnockMask.active = false;
  },
  showFish: function showFish() {
    var _this10 = this;

    var targetPosList = [];

    for (var i = 1; i < 5; i++) {
      var node = cc.find("Canvas/NodeBg/NodePosBase/NodeFishPos_" + i);
      targetPosList.push({
        x: node.x,
        y: node.y + 720
      });
    }

    var nodeBg = cc.find('Canvas/NodeBg');

    var _loop = function _loop(_i) {
      var fishScle = 0.7 + Math.random() * 0.2;
      var offsetX = (0, _Z_04_syys_utils.RandomInt)(-3, 3) * 120;
      var offsetY = (0, _Z_04_syys_utils.RandomInt)(-2, 1) * 50 + offsetX / 100 % 2 * 25;
      offsetX += Math.random() * 50;
      offsetY += Math.random() * 25;
      var fish = cc.instantiate(_this10.fishPrefab);
      fish.parent = nodeBg;
      fish.x = -1000 + offsetX;
      fish.y = 1020 + offsetY;
      fish.scaleX = fishScle + Math.random() * 0.3;
      fish.scaleY = fishScle + Math.random() * 0.3;
      cc.tween(fish).call(function () {
        var _fish$getComponent$ta;

        fish.getComponent('Z_04_syys_NodeFish').offsetX = offsetX;
        fish.getComponent('Z_04_syys_NodeFish').offsetY = offsetY;

        (_fish$getComponent$ta = fish.getComponent('Z_04_syys_NodeFish').targetPosList).push.apply(_fish$getComponent$ta, targetPosList);
      }).start();
    };

    for (var _i = 0; _i < (0, _Z_04_syys_utils.RandomInt)(15, 25); _i++) {
      _loop(_i);
    }
  },
  playEffect: function playEffect(name, loop, volume) {
    var musicComponent = cc.find('Canvas/MusicNode').getComponent('Z_04_syys_MusicNode');
    musicComponent.playEffect(name, loop, volume);
  } // update (dt) {},

});

cc._RF.pop();