import GameConst from "../GameConst";
export class CmPig extends egret.Sprite {
    public land;
    public main;

    public isStand = false;
    public aniComplete = true;
    // private threeActionBtnShow
    constructor(fun?) {
        super()
        this.init();
        // this.threeActionBtnShow = fun;
    }
    //组装     
    init() {
        //pig分为主体main和land
        this.x = 375;
        this.y = GameConst.stage.stageHeight - 355;
        //底座
        var land = new eui.Image();
        land.source = "cmLand_png";
        land.anchorOffsetX = 271 / 2;
        land.anchorOffsetY = 43 / 2;
        this["land"] = land;
        //  pig.addChild(land);
        //主体部分，还是以底座中心为原点
        var main = new egret.Sprite();
        this["main"] = main;
        this.addChild(main);

        //左腿，无装扮，用image，
        var leftLeg = new eui.Image();
        leftLeg.source = "cmLegLeft_png";
        leftLeg.x = -61;
        leftLeg.y = -146;
        main["leftLeg"] = leftLeg;
        main.addChild(leftLeg);

        //右腿，无装扮，用image
        var rightLeg = new eui.Image();
        rightLeg.source = "cmLegRight_png";
        rightLeg.x = 15;
        rightLeg.y = -146;
        main["rightLeg"] = rightLeg;
        main.addChild(rightLeg);

        //左脚，不动用image；和vip等级有关
        var leftFoot = new eui.Image();
        leftFoot.source = "cmFootLeft0_png";
        leftFoot.x = -73;
        leftFoot.y = -37;    //9
        main["leftFoot"] = leftFoot;
        main.addChild(leftFoot);
        //右脚，不动，无装扮，用image
        var rightFoot = new eui.Image();
        rightFoot.source = "cmFootRight0_png";
        rightFoot.x = -5;
        rightFoot.y = -37;
        main["rightFoot"] = rightFoot;
        main.addChild(rightFoot);

        //左手，有装扮，用commp
        var leftHand = new eui.Component();
        leftHand.x = -106;
        leftHand.y = -190;
        var leftHandImage = new eui.Image();
        leftHandImage.source = "cmHandLeft_png";
        leftHand.addChild(leftHandImage)
        main["leftHand"] = leftHand;
        main.addChild(leftHand);
        //右手，有装扮，用com，设置原点
        var rightHand = new eui.Component();
        rightHand.x = 53;
        rightHand.y = -190;
        var rightHandImage = new eui.Image();
        rightHandImage.source = "cmHandRight_png";
        rightHand.addChild(rightHandImage)
        main["rightHand"] = rightHand;
        main.addChild(rightHand);
        //左手下
        var leftHandDown = new eui.Component();
        leftHandDown.alpha = 0;
        leftHandDown.x = -126;
        leftHandDown.y = -217;
        var leftHandDownImage = new eui.Image();
        leftHandDownImage.source = "cmHandLeftDown_png";
        leftHandDown.addChild(leftHandDownImage)
        main["leftHandDown"] = leftHandDown;
        main.addChild(leftHandDown);
        //右手下
        var rightHandDown = new eui.Component();
        rightHandDown.alpha = 0;
        rightHandDown.x = 51;
        rightHandDown.y = -219;
        var rightHandDownImage = new eui.Image();
        rightHandDownImage.source = "cmHandRightDown_png";
        rightHandDown.addChild(rightHandDownImage)
        main["rightHandDown"] = rightHandDown;
        main.addChild(rightHandDown);

        //肚子，有装扮，用comp，设置原点
        var body = new eui.Component();
        body.x = -76;
        body.y = -235;
        var bodyImage = new eui.Image();
        bodyImage.source = "ydBody_png";
        body.addChild(bodyImage)
        main["body"] = body;
        main.addChild(body);

        //猪头，层级，用comp，设置原点,所有头部东西都加入，所以其他部件都用image   
        var head = new egret.Sprite();
        head.x = 0;
        head.y = -177;
        main["head"] = head;
        main.addChild(head);
        //左耳
        var leftEar = new eui.Image();
        leftEar.source = "cmEarLeft_png";
        leftEar.anchorOffsetX = 97;
        leftEar.anchorOffsetY = 102;
        leftEar.x = -56;
        leftEar.y = -160;
        head["leftEar"] = leftEar;
        head.addChild(leftEar);
        //右耳
        var rightEar = new eui.Image();
        rightEar.source = "cmEarRight_png";
        // rightEar.anchorOffsetX = 114;
        rightEar.anchorOffsetY = 106;
        rightEar.x = 64;
        rightEar.y = -157;
        head["rightEar"] = rightEar;
        head.addChild(rightEar);
        //脸
        var face = new eui.Image();
        face.source = "cmFace_png";
        face.x = -243 / 2;
        face.y = -265;
        head.addChild(face);

        //眉毛左   有动效,设置锚点在中心
        var leftMeimao = new eui.Image();
        leftMeimao.source = "cmMeimaoLeft_png"
        leftMeimao.anchorOffsetX = 33 / 2;
        leftMeimao.anchorOffsetY = 9 / 2;
        leftMeimao.x = -72 + 33 / 2;
        leftMeimao.y = -188 + 9 / 2;
        head["leftMeimao"] = leftMeimao;
        head.addChild(leftMeimao);

        //眉毛右   有动效,设置锚点在中心
        var rightMeimao = new eui.Image();
        rightMeimao.source = "cmMeimaoRight_png"
        rightMeimao.anchorOffsetX = 33 / 2;
        rightMeimao.anchorOffsetY = 9 / 2;
        rightMeimao.x = 38 + 33 / 2;
        rightMeimao.y = -189 + 9 / 2;
        head["rightMeimao"] = rightMeimao;
        head.addChild(rightMeimao);

        //眼睛2
        var eye2 = new eui.Image();
        eye2.source = "cmEye2_png";
        eye2.x = -133;
        eye2.y = -171;
        head["eye2"] = eye2;
        head.addChild(eye2);
        //眼睛1
        var eye1 = new eui.Image();
        eye1.source = "cmEye1_png";
        eye1.x = -133;
        eye1.y = -171;
        eye1.alpha = 0;
        head["eye1"] = eye1;
        head.addChild(eye1);
        //眼睛0
        var eye0 = new eui.Image();
        eye0.source = "cmEye0_png";
        eye0.x = -133;
        eye0.y = -171;
        eye0.alpha = 0;
        head["eye0"] = eye0;
        head.addChild(eye0);
        //嘴巴0   有口红，用com
        var mouth0 = new eui.Component();
        mouth0.x = -58;
        mouth0.y = -86;
        var mouth0Image = new eui.Image();
        mouth0Image.source = "cmMouth0_png";
        mouth0.addChild(mouth0Image)
        head["mouth0"] = mouth0;
        head.addChild(mouth0);
        //嘴巴1
        var mouth1 = new eui.Image();
        mouth1.alpha = 0;
        mouth1.source = "cmMouth1_png";
        mouth1.x = -10;
        mouth1.y = -68;
        head["mouth1"] = mouth1;
        head.addChild(mouth1);

        //鼻子
        var nose = new eui.Image();
        nose.source = "cmNose_png";
        nose.x = -59;
        nose.y = -179;
        head["nose"] = nose;
        head.addChild(nose);


        //左手上
        var leftHandUp = new eui.Component();
        leftHandUp.alpha = 0;
        leftHandUp.x = -126;
        leftHandUp.y = -244;
        var leftHandUpImage = new eui.Image();
        leftHandUpImage.source = "cmHandLeftUp_png";
        leftHandUp.addChild(leftHandUpImage)
        main["leftHandUp"] = leftHandUp;
        main.addChild(leftHandUp);
        //右手上
        var rightHandUp = new eui.Component();
        rightHandUp.alpha = 0;
        rightHandUp.x = 121 - 45;
        rightHandUp.y = -244;
        var rightHandUpImage = new eui.Image();
        rightHandUpImage.source = "cmHandRightUp_png";
        rightHandUp.addChild(rightHandUpImage)
        main["rightHandUp"] = rightHandUp;
        main.addChild(rightHandUp);

        //嘴巴2
        var mouth2 = new eui.Image();
        mouth2.alpha = 0;
        mouth2.source = "cmMouth2_png";
        mouth2.anchorOffsetX = 58 / 2;
        mouth2.anchorOffsetY = 52 / 2;
        mouth2.x = 1;
        mouth2.y = -56;
        head["mouth2"] = mouth2;
        head.addChild(mouth2);

        //全程固定动效写这
        this.aniAlways();

        this.aniUsual();
    }

    //根据vip等级更新鞋子
    public updateFeet(vipLevel) {
        this.main.leftFoot.source = "cmFootLeft" + vipLevel + "_png";
        this.main.rightFoot.source = "cmFootRight" + vipLevel + "_png";
    }

    //臭美猪全程动效
    private aniAlways() {
        var pig = this["main"];
        // 开眼全  初始alph1
        pig.head.eye2.alpha = 1;
        var eye2 = egret.Tween.get(pig.head.eye2, { loop: true });
        eye2.to({ alpha: 1 }, 100)
            .wait(29)
            .to({ alpha: 0 }, 1)
            .wait(269)
            .to({ alpha: 1 }, 1)
            .wait(1600);
        // 开眼半  初始alph0
        pig.head.eye1.alpha = 0;
        var eye1 = egret.Tween.get(pig.head.eye1, { loop: true });
        eye1.wait(129)
            .to({ alpha: 1 }, 1)
            .to({ alpha: 1 }, 100)
            .wait(29)
            .to({ alpha: 0 }, 1)
            .wait(1740);
        // 闭眼  初始alph0
        pig.head.eye0.alpha = 0;
        var eye0 = egret.Tween.get(pig.head.eye0, { loop: true });
        eye0.wait(259)
            .to({ alpha: 1 }, 1)
            .to({ alpha: 1 }, 100)
            .wait(39)
            .to({ alpha: 0 }, 1)
            .wait(1600);

        //整体
        var pigAllTween = egret.Tween.get(pig, { loop: true });
        pigAllTween.to({ scaleY: 0.99 }, 1000)
            .to({ scaleY: 1 }, 1000)

        //左耳
        var leftEar = egret.Tween.get(pig.head.leftEar, { loop: true });
        leftEar.wait(330)
            .to({ rotation: -6 }, 170)
            .to({ rotation: 0 }, 160)
            .wait(1340)
            .to({ rotation: -6 }, 160)
            .to({ rotation: 0 }, 170)
            .to({ rotation: -6 }, 170)
            .to({ rotation: 0 }, 160)
            .wait(2000)
        //右耳
        var rightEar = egret.Tween.get(pig.head.rightEar, { loop: true });
        rightEar.wait(330)
            .to({ rotation: 6 }, 170)
            .to({ rotation: 0 }, 160)
            .wait(1340)
            .to({ rotation: 6 }, 160)
            .to({ rotation: 0 }, 170)
            .to({ rotation: 6 }, 170)
            .to({ rotation: 0 }, 160)
            .wait(2000)
    }
    //臭美普通状态动画
    private aniUsual() {
        //建一个空的tween用于监听 land未添加进pig 
        // egret.Tween.get(this["land"], { loop: true })
        //     .wait(200)
        //     .call(() => {
        //         if (!this.status) {
        //             this.cmPigUsualToStand();
        //         }
        //     }, this);
    }
    //从普通状态到站立状态
    private usualToStand() {
        //显示交互
        // this.threeActionBtnShow();

        // egret.Tween.removeTweens(this.land);

        var pig = this["main"];
        //左手
        egret.Tween.get(pig.leftHand)
            .to({ alpha: 0 }, 30)
            .wait(670)
            .to({ alpha: 1 }, 30)
        //右手
        egret.Tween.get(pig.rightHand)
            .to({ alpha: 0 }, 30)
            .wait(670)
            .to({ alpha: 1 }, 30)
        //左手下
        egret.Tween.get(pig.leftHandDown)
            .to({ alpha: 1 }, 30)
            .wait(670)
            .to({ alpha: 0 }, 30)
        //右手下
        egret.Tween.get(pig.rightHandDown)
            .to({ alpha: 1 }, 30)
            .wait(670)
            .to({ alpha: 0 }, 30)
        //左手下
        egret.Tween.get(pig.leftHandUp)
            .to({ alpha: 1 }, 30)
            .wait(670)
            .to({ alpha: 0 }, 30)
        //右手下
        egret.Tween.get(pig.rightHandUp)
            .to({ alpha: 1 }, 30)
            .wait(670)
            .to({ alpha: 0 }, 30)
        //头
        let headY = pig.head.y;
        egret.Tween.get(pig.head)
            .to({ y: headY + 4 }, 160)
            .wait(500)
            .to({ y: headY }, 70)
            .call(() => {
                this.aniComplete = true;
                this.isStand = true;
            }, this);

        //嘴巴0
        egret.Tween.get(pig.head.mouth0)
            .to({ alpha: 0 }, 30)
            .wait(630)
            .to({ alpha: 1 }, 40)
        //嘴巴1
        egret.Tween.get(pig.head.mouth1)
            .to({ alpha: 1 }, 30)
            .wait(630)
            .to({ alpha: 0 }, 40)
        //嘴巴2
        egret.Tween.get(pig.head.mouth2)
            .to({ alpha: 1 }, 30)
            .wait(970)
            .to({ alpha: 0 }, 1000)
           
        pig.head.mouth2.scaleX = 1;
        pig.head.mouth2.scaleY = 1;
        egret.Tween.get(pig.head.mouth2)
            .to({ scaleX: 0.2, scaleY: 0.2 }, 30)
            .to({ scaleX: 5, scaleY: 5 }, 1770)

        //眉毛左
        let meimaoLeftX = pig.head.leftMeimao.x;
        egret.Tween.get(pig.head.leftMeimao)
            .wait(30)
            .to({ x: meimaoLeftX + 7, rotation: 24 }, 170)
            .wait(330)
            .to({ x: meimaoLeftX, rotation: 0 }, 170)
        //眉毛右
        let meimaoRightX = pig.head.rightMeimao.x;
        egret.Tween.get(pig.head.rightMeimao)
            .wait(30)
            .to({ x: meimaoRightX - 7, rotation: -24 }, 170)
            .wait(330)
            .to({ x: meimaoRightX, rotation: 0 }, 170)

        //建一个空的tween用于监听 land未添加进pig 
        // egret.Tween.get(this["land"], { loop: true })
        //     .wait(200)
        //     .call(() => {
        //         if (this.status) {
        //             this.cmPigStandToUsual();
        //         }
        //     }, this);
    }

    //从站立到普通状态  空的
    private standToUsual() {
        //建一个空的tween用于监听 land未添加进pig 
        // egret.Tween.removeTweens(this["land"])
        egret.Tween.get(this["land"])
            .wait(200)
            .call(() => {
                this.aniComplete = true;
                this.isStand = false;
                this.aniUsual();
            }, this);
    }

     //喂食动效
     private feedMouth;  //8次
     private feedAni() {
         if (!this.feedMouth) {
             this.feedMouth = new eui.Image();
             this.feedMouth.source = "feedMouth_png";
             this.feedMouth.anchorOffsetX = 27 / 2;
             this.feedMouth.x = 0;
             this.feedMouth.y = -65;
             console.log(this.feedMouth)
         }
         var head = this.main.head;
         head.addChild(this.feedMouth);
         head.mouth0.visible=false;
         head.mouth1.visible=false;
         head.mouth2.visible=false;
         //动效 8次
         var deta = 250
         egret.Tween.get(this.feedMouth)
             .to({ scaleX: 0.7 }, deta)
             .to({ scaleX: 1 }, deta)
             .to({ scaleX: 0.7 }, deta)
             .to({ scaleX: 1 }, deta)
             .to({ scaleX: 0.7 }, deta)
             .to({ scaleX: 1 }, deta)
             .to({ scaleX: 0.7 }, deta)
             .to({ scaleX: 1 }, deta)
             .to({ scaleX: 0.7 }, deta)
             .to({ scaleX: 1 }, deta)
             .to({ scaleX: 0.7 }, deta)
             .to({ scaleX: 1 }, deta)
             .to({ scaleX: 0.7 }, deta)
             .to({ scaleX: 1 }, deta)
             .to({ scaleX: 0.7 }, deta)
             .to({ scaleX: 1 }, deta)
             .call(() => {
                 head.removeChild(this.feedMouth);
                 head.mouth0.visible=true;
                 head.mouth1.visible=true;
                 head.mouth2.visible=true;
                 this.standToUsual();
             }, this)
         egret.Tween.get(head)
             .to({ y: -177 + 3 }, deta)
             .to({ y: -177 }, deta)
             .to({ y: -177 + 3 }, deta)
             .to({ y: -177 }, deta)
             .to({ y: -177 + 3 }, deta)
             .to({ y: -177 }, deta)
             .to({ y: -177 + 3 }, deta)
             .to({ y: -177 }, deta)
             .to({ y: -177 + 3 }, deta)
             .to({ y: -177 }, deta)
             .to({ y: -177 + 3 }, deta)
             .to({ y: -177 }, deta)
             .to({ y: -177 + 3 }, deta)
             .to({ y: -177 }, deta)
             .to({ y: -177 + 3 }, deta)
             .to({ y: -177 }, deta)
     }

}


//  // 开眼全  初始alph1
//  pig.head.eye2.alpha = 1;
//  var eye2 = egret.Tween.get(pig.head.eye2, { loop: true });
//  eye2.to({ alpha: 1 }, 100)
//      .to({ alpha: 0 }, 30)
//      .to({ alpha: 1 }, 270)
//      .wait(1600);
//  // 开眼半  初始alph0
//  pig.head.eye1.alpha = 0;
//  var eye1 = egret.Tween.get(pig.head.eye1, { loop: true });
//  eye1.wait(100)
//      .to({ alpha: 1 }, 30)
//      .to({ alpha: 1 }, 100)
//      .to({ alpha: 0 }, 30)
//      .wait(1740);
//  // 闭眼  初始alph0
//  pig.head.eye0.alpha = 0;
//  var eye0 = egret.Tween.get(pig.head.eye0, { loop: true });
//  eye0.wait(230)
//      .to({ alpha: 1 }, 30)
//      .to({ alpha: 1 }, 100)
//      .to({ alpha: 0 }, 40)
//      .wait(1600);