import GameConst from "../GameConst";
export class ShPig extends egret.Sprite {
    public land;
    public main;
    public isStand = false;
    public aniComplete = true;
    // private threeActionBtnShow
    constructor(fun?) {
        super()
        this.init();
        // this.threeActionBtnShow = fun;
    }
    private init() {
        //pig分为主体main和land
        this.x = 375;
        this.y = GameConst.stage.stageHeight - 355;
        //底座
        var land = new eui.Image();
        land.source = "ydLand_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 = "shLegLeft_png";
        leftLeg.anchorOffsetX = 54 / 2;
        leftLeg.anchorOffsetY = 128;
        leftLeg.x = -70 + 27;
        leftLeg.y = -157 + 128;
        main["leftLeg"] = leftLeg;
        main.addChild(leftLeg);

        //右腿，无装扮，用image
        var rightLeg = new eui.Image();
        rightLeg.source = "shLegRight_png";
        rightLeg.anchorOffsetX = 69 / 2;
        rightLeg.anchorOffsetY = 147;
        rightLeg.x = 23 + 69 / 2;
        rightLeg.y = -167 + 147;
        main["rightLeg"] = rightLeg;
        main.addChild(rightLeg);

        //左脚，不动用image；和vip等级有关
        var leftFoot = new eui.Image();
        leftFoot.source = "shFootLeft0_png";
        leftFoot.x = -77;
        leftFoot.y = -44;    //9
        main["leftFoot"] = leftFoot;
        main.addChild(leftFoot);
        //右脚，不动，无装扮，用image
        var rightFoot = new eui.Image();
        rightFoot.source = "shFootRight0_png";
        rightFoot.x = 41;
        rightFoot.y = -50;
        main["rightFoot"] = rightFoot;
        main.addChild(rightFoot);

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

        //手，有装扮，用commp 加入进body 但指向不变
        var leftHand = new eui.Component();
        leftHand.x = 75;
        leftHand.y = 45;
        var leftHandImage = new eui.Image();
        leftHandImage.source = "shHand_png";
        leftHandImage.x = -170 / 2;
        leftHand.addChild(leftHandImage)
        main["leftHand"] = leftHand;
        body.addChild(leftHand);

        //猪头，层级，用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 = "shEarLeft_png";
        leftEar.anchorOffsetX = 114;
        leftEar.anchorOffsetY = 102;
        leftEar.x = -156 + 114;
        leftEar.y = -261 + 102;
        head["leftEar"] = leftEar;
        head.addChild(leftEar);
        //右耳
        var rightEar = new eui.Image();
        rightEar.source = "shEarRight_png";
        rightEar.anchorOffsetY = 106;
        rightEar.x = 155 - 103;
        rightEar.y = -262 + 106;
        head["rightEar"] = rightEar;
        head.addChild(rightEar);

        //脸
        var face = new eui.Image();
        face.source = "shFace_png";
        face.x = -123;
        face.y = -266;
        head.addChild(face);

        //眉毛左   有动效,设置锚点在中心
        var leftMeimao = new eui.Image();
        leftMeimao.source = "shMeimaoLeft_png"
        leftMeimao.anchorOffsetX = 40 / 2;
        leftMeimao.anchorOffsetY = 18 / 2;
        leftMeimao.x = -91 + 40 / 2;
        leftMeimao.y = -184 + 18 / 2;
        head["leftMeimao"] = leftMeimao;
        head.addChild(leftMeimao);

        //眉毛右   有动效,设置锚点在中心
        var rightMeimao = new eui.Image();
        rightMeimao.rotation = 10;
        rightMeimao.scaleX = rightMeimao.scaleY = 0.86;
        rightMeimao.source = "shMeimaoRight_png"
        rightMeimao.anchorOffsetX = 48 / 2;
        rightMeimao.anchorOffsetY = 22 / 2;
        rightMeimao.x = 58;
        rightMeimao.y = -175;
        head["rightMeimao"] = rightMeimao;
        head.addChild(rightMeimao);

        //眼睛0
        var eye0 = new eui.Image();
        eye0.source = "shEye0_png";
        eye0.x = -129;
        eye0.y = -166;
        eye0.alpha = 0;
        head["eye0"] = eye0;
        head.addChild(eye0);
        //眼睛1
        var eye1 = new eui.Image();
        eye1.source = "shEye1_png";
        eye1.x = -129;
        eye1.y = -166;
        eye1.alpha = 0;
        head["eye1"] = eye1;
        head.addChild(eye1);
        //眼睛2
        var eye2 = new eui.Image();
        eye2.source = "shEye2_png";
        eye2.x = -129;
        eye2.y = -166;
        head["eye2"] = eye2;
        head.addChild(eye2);
        //眼睛3
        var eye3 = new eui.Image();
        eye3.source = "shEye3_png";
        eye3.alpha = 0;
        eye3.x = -129;
        eye3.y = -166;
        head["eye3"] = eye3;
        head.addChild(eye3);

        //眼镜
        var glasses = new eui.Component();
        glasses.x = -106;
        glasses.y = -126;
        var glassesImage = new eui.Image();
        glassesImage.source = "shGlasses_png";
        glasses.addChild(glassesImage)
        head["glasses"] = glasses;
        head.addChild(glasses);
        //光
        var light = new eui.Image();
        light.source = "shGlassLight_png";
        light.x = -21;
        light.y = -21;
        glasses["light"] = light
        glasses.addChild(light);
        //遮罩
        var mask = new egret.Shape();
        mask.graphics.beginFill(0xff0000, 1);
        mask.graphics.drawCircle(11 + 22, 4 + 22, 22)
        mask.graphics.drawCircle(199 - 22, 4 + 22, 22)
        mask.graphics.endFill();
        glasses.addChild(mask);
        light.mask = mask;

        //鼻子
        var nose = new eui.Image();
        nose.source = "shNose_png";
        nose.x = -60;
        nose.y = -174;
        head["nose"] = nose;
        head.addChild(nose);

        //嘴巴
        var mouth = new eui.Image();
        mouth.source = "shBubble_png";
        mouth.anchorOffsetX = 73 / 2;
        mouth.anchorOffsetY = 84 / 2;
        // mouth.x = 0;
        mouth.y = -60;
        head["mouth"] = mouth;
        head.addChild(mouth);

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

        this.aniUsual();

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

    //社会猪全程动效
    private aniAlways() {
        var pig = this["main"];

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

        //眼镜光
        pig.head.glasses.light.x = -30;
        pig.head.glasses.light.y = -21;
        egret.Tween.get(pig.head.glasses.light, { loop: true })
            .to({ x: -30 + 61, y: -21 + 53 }, 670)
            .wait(1000)
            .call(() => {
                pig.head.glasses.light.x = -30;
                pig.head.glasses.light.y = -21;
            })

    }
    //社会猪普通状态动画
    private aniUsual() {
        var pig = this["main"];
        //建一个空的tween用于监听 land未添加进pig 
        // egret.Tween.get(this["land"], { loop: true })
        //     .wait(200)
        //     .call(() => {
        //         if (!this.status) {
        //             this.shPigUsualToStand();
        //         }
        //     }, this);
        //猪头
        var headY = pig.head.y;
        var head = egret.Tween.get(pig.head, { loop: true });
        head.to({ y: headY + 3 }, 340)
            .to({ y: headY }, 330)
            .to({ y: headY + 3 }, 330)
            .to({ y: headY }, 340)
            .to({ y: headY + 3 }, 330)
            .to({ y: headY }, 330)
            // .call(() => {
            //     if (!this.status) {
            //         this.shPigUsualToStand();
            //     }
            //     //这里统一回调
            //     // console.log(111)
            // }, this)
            .wait(1000)
        //身体
        var bodyY = pig.body.y;
        var body = egret.Tween.get(pig.body, { loop: true });
        body.to({ y: bodyY + 3 }, 340)
            .to({ y: bodyY }, 330)
            .to({ y: bodyY + 3 }, 330)
            .to({ y: bodyY }, 340)
            .to({ y: bodyY + 3 }, 330)
            .to({ y: bodyY }, 330)
            .wait(1000)

        //左耳
        var leftEar = egret.Tween.get(pig.head.leftEar, { loop: true });
        leftEar.to({ rotation: -4 }, 340)
            .to({ rotation: 0 }, 330)
            .to({ rotation: -4 }, 330)
            .to({ rotation: 0 }, 340)
            .to({ rotation: -4 }, 330)
            .to({ rotation: 0 }, 330)
            .wait(1000)
        //右耳
        var rightEar = egret.Tween.get(pig.head.rightEar, { loop: true });
        rightEar.to({ rotation: 4 }, 340)
            .to({ rotation: 0 }, 330)
            .to({ rotation: 4 }, 330)
            .to({ rotation: 0 }, 340)
            .to({ rotation: 4 }, 330)
            .to({ rotation: 0 }, 330)
            .wait(1000)

        // 开眼全  初始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 hand = egret.Tween.get(pig.leftHand, { loop: true });
        hand.to({ scaleY: 1.06 }, 340)
            .to({ scaleY: 1 }, 330)
            .to({ scaleY: 1.06 }, 330)
            .to({ scaleY: 1 }, 340)
            .to({ scaleY: 1.06 }, 330)
            .to({ scaleY: 1 }, 330)
            .wait(1000)
        //左腿
        var leftLeg = egret.Tween.get(pig.leftLeg, { loop: true });
        leftLeg.to({ rotation: -2 }, 340)
            .to({ rotation: 0 }, 330)
            .to({ rotation: -2 }, 330)
            .to({ rotation: 0 }, 340)
            .to({ rotation: -2 }, 330)
            .to({ rotation: 0 }, 330)
            .wait(1000)
        //左腿
        var rightLeg = egret.Tween.get(pig.rightLeg, { loop: true });
        rightLeg.to({ rotation: -2 }, 340)
            .to({ rotation: 0 }, 330)
            .to({ rotation: -2 }, 330)
            .to({ rotation: 0 }, 340)
            .to({ rotation: -2 }, 330)
            .to({ rotation: 0 }, 330)
            .wait(1000)

        //泡泡
        pig.head.mouth.alpha = 1;
        pig.head.mouth.scaleX = pig.head.mouth.scaleY = 1;
        var mouth = egret.Tween.get(pig.head.mouth, { loop: true });
        mouth.wait(1670)
            .to({ scaleX: 1.1, scaleY: 1.1 }, 160)
            .to({ scaleX: 1, scaleY: 1 }, 170)
            .to({ scaleX: 1.6, scaleY: 1.6 }, 670)
            .wait(160)
            .to({ scaleX: 0.95, scaleY: 0.95 }, 130)
            .to({ scaleX: 1, scaleY: 1 }, 40)
            .wait(2200)
    }

    //从普通状态到站立状态
    private usualToStand() {
        //显示交互
        // this.threeActionBtnShow();

        // egret.Tween.removeTweens(this.land);
        var pig = this["main"];
        egret.Tween.removeTweens(pig.head);
        egret.Tween.removeTweens(pig.head.leftEar);
        egret.Tween.removeTweens(pig.head.rightEar);
        egret.Tween.removeTweens(pig.head.mouth);
        egret.Tween.removeTweens(pig.head.eye0);
        egret.Tween.removeTweens(pig.head.eye1);
        egret.Tween.removeTweens(pig.head.eye2);
        egret.Tween.removeTweens(pig.body);
        egret.Tween.removeTweens(pig.leftHand);
        egret.Tween.removeTweens(pig.leftLeg);
        egret.Tween.removeTweens(pig.rightLeg);

        //执行切换
        //躯体  初始位置x:-79 y:-236
        egret.Tween.get(pig.body)
            .to({ x: -79 + 20 }, 140)
        //头   初始位置x:0,y:-177
        egret.Tween.get(pig.head)
            .to({ x: 20 }, 140)
        //左腿 
        egret.Tween.get(pig.leftLeg)
            .to({ rotation: 14 }, 140)
        //右腿 
        egret.Tween.get(pig.rightLeg)
            .to({ rotation: 12 }, 140)
        //嘴
        pig.head.mouth.scaleX = pig.head.mouth.scaleY = 1;
        egret.Tween.get(pig.head.mouth)
            .to({ scaleX: 2, scaleY: 2 }, 200)
            .to({ scaleX: 0, scaleY: 0, alpha: 0 }, 60)
            .call(() => {
                this.aniComplete = true;
                this.isStand = true;
            }, this);

        //眼睛3
        egret.Tween.get(pig.head.eye3)
            .to({ alpha: 1 }, 140)
            .wait(1000)
            .to({ alpha: 0 }, 140)
        // 开眼全  初始alph1
        pig.head.eye2.alpha = 1;
        var eye2 = egret.Tween.get(pig.head.eye2, { loop: true });
        eye2.wait(1280)
            .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(1280)
            .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(1280)
            .wait(259)
            .to({ alpha: 1 }, 1)
            .to({ alpha: 1 }, 100)
            .wait(39)
            .to({ alpha: 0 }, 1)
            .wait(1600);

        //眼镜y:-126
        egret.Tween.get(pig.head.glasses)
            .wait(110)
            .to({ y: -126 - 10 }, 30)

        //左耳
        egret.Tween.get(pig.head.leftEar)
            .to({ rotation: 13 }, 140)
        //右耳
        egret.Tween.get(pig.head.rightEar)
            .to({ rotation: -13 }, 140)

        //右眉毛  y:175
        egret.Tween.get(pig.head.rightMeimao)
            .to({ y: -175 - 18, rotation: 0, scaleX: 1, scaleY: 1 }, 140)

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

    //从站立到普通状态  空的
    private standToUsual() {
        var pig = this["main"];

        // egret.Tween.removeTweens(this["land"])
        //执行切换
        //躯体  初始位置x:-79 y:-236
        egret.Tween.get(pig.body)
            .to({ x: -79 }, 140)
            .call(() => {
                this.aniComplete = true;
                this.isStand = false;
                this.aniUsual();
            }, this)
        //头   初始位置x:0,y:-177
        egret.Tween.get(pig.head)
            .to({ x: 0 }, 140)
        //左腿 
        egret.Tween.get(pig.leftLeg)
            .to({ rotation: 0 }, 140)
        //右腿 
        egret.Tween.get(pig.rightLeg)
            .to({ rotation: 0 }, 140)

        //嘴
        pig.head.mouth.scaleX = pig.head.mouth.scaleY = 0;
        pig.head.mouth.alpha = 0
        egret.Tween.get(pig.head.mouth)
            .to({ scaleX: 1, scaleY: 1, alpha: 1 }, 140)

        //眼睛3
        // egret.Tween.get(pig.head.eye3)
        //     .to({ alpha: 0 }, 140)

        //眼镜y:-126
        egret.Tween.get(pig.head.glasses)
            .to({ y: -126 }, 30)

        //左耳
        egret.Tween.get(pig.head.leftEar)
            .to({ rotation: 0 }, 140)
        //右耳
        egret.Tween.get(pig.head.rightEar)
            .to({ rotation: 0 }, 140)

        //右眉毛  y:-175
        egret.Tween.get(pig.head.rightMeimao)
            .to({ y: -175, rotation: 10, scaleX: 0.86, scaleY: 0.86 }, 140)
    }


    //喂食动效
    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 = -60;
            console.log(this.feedMouth)
        }
        var head = this.main.head;
        head.addChild(this.feedMouth);
        //动效 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)
                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)
    }

}