"use strict";
cc._RF.push(module, 'e729bOHnRpHpJ09B2/R3cNg', 'item');
// Lwd/script/game/item.js

"use strict";

/**
 * item
 */
cc.Class({
  "extends": cc.Component,
  properties: {
    Picture: {
      "default": null,
      type: cc.Sprite,
      displayName: "图片"
    },
    nodeState: {
      "default": [],
      type: cc.Node,
      displayName: "节点状态"
    },
    spt_error: {
      "default": null,
      type: cc.Node,
      displayName: "失败"
    },
    spt_chick: {
      "default": null,
      type: cc.Node,
      displayName: "小鸡"
    }
  },
  //更新界面ui
  updateUI: function updateUI(Info) {
    //当前数据
    this.itemInfo = Info; //初始化数据

    this.InitData(); //设置图片

    this.setPicture(Info.url);
  },
  //初始化信息
  InitData: function InitData() {
    //设置按钮状态
    this.setBtnState(0);

    if (this.itemInfo.positionId == 2) {
      this.setBtnState(2);
    }
  },
  //设置图片
  setPicture: function setPicture(url) {
    var _this = this;

    //图片资源
    if (!url) {
      return;
    }

    lwd_g.res_mgr.getSpriteFrimeByUrl(url, function (list) {
      _this.Picture.spriteFrame = list;

      _this.photoScare(_this.Picture.node);

      _this.Picture.node.active = true;
    });
  },
  //图片适配
  photoScare: function photoScare(node) {
    var maxSize = Math.min(120 / node.height, 120 / node.width);

    if (node.perScale == undefined) {
      node.perScale = node.scaleX;
    } else {
      node.scaleX = node.perScale;
      node.scaleY = node.perScale;
    }

    node.scaleX *= maxSize;
    node.scaleY *= maxSize;
  },
  //设置按钮状态
  setBtnOpen: function setBtnOpen(b) {
    this.node.getComponent(cc.Button).enabled = b;
  },
  //设置按钮状态0可点击  1已答对 2答错 3连线完成
  setBtnState: function setBtnState(num) {
    //this.nodeState[num].active = true;
    switch (~~num) {
      case 0:
        this.nodeState[0].active = true;
        this.nodeState[1].active = false;
        this.nodeState[2].active = false;
        this.node.state = 0;
        this.setBtnOpen(true);
        break;

      case 1:
        this.nodeState[1].active = true;
        this.nodeState[2].active = false;
        this.node.state = 1;
        this.setBtnOpen(false);
        break;

      case 2:
        this.nodeState[1].active = false;
        this.nodeState[2].active = true;
        this.node.state = 2;
        this.setBtnOpen(false);
        break;

      case 3:
        this.nodeState[0].active = true;
        this.nodeState[1].active = false;
        this.nodeState[2].active = false;
        this.node.state = 3;
        this.setBtnOpen(false);
        break;
    }
  },
  //
  onBtnGet: function onBtnGet() {
    var _this2 = this;

    //如果节点的Id等于当前数据的id的话
    if (this.itemInfo.typeId == lwd_g.data_mgr.playId && this.itemInfo.positionId == lwd_g.data_mgr.playType) {
      //播放答对音效
      this.setBtnState(1);
      lwd_g.speaker.inst.play_good(function () {
        //如果是上面答对的话
        if (_this2.itemInfo.positionId == 1) {
          //执行播放下排的音乐
          lwd_g.data_mgr.playType = 2;
          lwd_g.game.inst.PlayAudio(); //设置状态

          lwd_g.game.inst.allLightDark(1, 2);
          lwd_g.game.inst.allLightDark(2, 0); //把这个答对的节点保存起来

          lwd_g.data_mgr.itemInfo = _this2;
        } //如果答对的是下排的话


        if (_this2.itemInfo.positionId == 2) {
          //连线
          var posStart = lwd_g.data_mgr.itemInfo.spt_chick.parent.convertToWorldSpaceAR(lwd_g.data_mgr.itemInfo.spt_chick.position);

          var posEnd = _this2.spt_chick.parent.convertToWorldSpaceAR(_this2.spt_chick.position);

          lwd_g.data_mgr.nowNode = _this2.itemInfo;
          lwd_g.game.inst.ShowLine(posStart.x - 640, posStart.y - 360, posEnd.x - 640, posEnd.y - 360, function () {
            lwd_g.data_mgr.itemInfo.setBtnState(3);

            _this2.setBtnState(3);
          });
        }
      });
    } else {
      this.spt_error.active = true; //播放失败音效

      lwd_g.speaker.inst.play_error();
      this.setBtnState(2);
      cc.tween(this.spt_error).to(0.2, {
        scale: 1.1
      }).to(0.5, {
        scale: 0
      }).start();
    }
  }
});

cc._RF.pop();