
import GameConst from "../GameConst";
export class YdPig 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 = "ydLand_png";
        land.anchorOffsetX = 271 / 2;
        land.anchorOffsetY = 43 / 2;
        this["land"] = land;
        this.addChild(land);
        //主体部分，还是以底座中心为原点
        var main = new egret.Sprite();
        this["main"] = main;
        this.addChild(main);

        //左腿，无装扮，用image，设置锚点
        var leftLeg = new eui.Image();
        leftLeg.source = "ydLegLeft_png";
        leftLeg.anchorOffsetX = 47;
        leftLeg.anchorOffsetY = 126;
        leftLeg.x = -30;
        leftLeg.y = -29;
        main["leftLeg"] = leftLeg;
        main.addChild(leftLeg);

        //右腿，无装扮，用image，设置锚点
        var rightLeg = new eui.Image();
        rightLeg.source = "ydLegRight_png";
        rightLeg.anchorOffsetX = 25;
        rightLeg.anchorOffsetY = 126;
        rightLeg.x = 58;
        rightLeg.y = -45;
        main["rightLeg"] = rightLeg;
        main.addChild(rightLeg);

        //左脚，不动用image；和vip等级有关
        var leftFoot = new eui.Image();
        leftFoot.source = "ydFootLeft0_png";
        leftFoot.x = -83;
        leftFoot.y = -56;
        main["leftFoot"] = leftFoot;
        main.addChild(leftFoot);
        //右脚，无装扮，用image，设置锚点
        var rightFoot = new eui.Image();
        rightFoot.source = "ydFootRight0_png";
        rightFoot.anchorOffsetX = 32;
        rightFoot.anchorOffsetY = 35;
        rightFoot.x = 62;
        rightFoot.y = -29;
        main["rightFoot"] = rightFoot;
        main.addChild(rightFoot);

        //左手，有装扮，用commp，设置原点
        var leftHand = new eui.Component();
        leftHand.x = -42;
        leftHand.y = -190;
        var leftHandImage = new eui.Image();
        leftHandImage.source = "ydHandLeft_png";
        leftHandImage.x = -68;
        leftHand.addChild(leftHandImage)
        main["leftHand"] = leftHand;
        main.addChild(leftHand);
        //右手，有装扮，用com，设置原点
        var rightHand = new eui.Component();
        rightHand.x = 22;
        rightHand.y = -198;
        var rightHandImage = new eui.Image();
        rightHandImage.source = "ydHandRight_png";
        rightHand.addChild(rightHandImage)
        main["rightHand"] = rightHand;
        main.addChild(rightHand);

        //肚子，右装扮，用comp，设置原点
        var body = new eui.Component();
        body.x = -3;
        body.y = -189;
        var bodyImage = new eui.Image();
        bodyImage.x = -153 / 2;
        bodyImage.y = -45;
        bodyImage.source = "ydBody_png";
        body.addChild(bodyImage)
        main["body"] = body;
        main.addChild(body);

        //猪头，层级，用comp，设置原点,所有头部东西都加入，所以其他部件都用image
        var head = new egret.Sprite();
        head.x = -1;
        head.y = -177;
        main["head"] = head;
        main.addChild(head);
        //左耳
        var leftEar = new eui.Image();
        leftEar.source = "ydEarLeft_png";
        leftEar.anchorOffsetX = 114;
        leftEar.anchorOffsetY = 102;
        leftEar.x = -43;
        leftEar.y = -336 + 177;
        head["leftEar"] = leftEar;
        head.addChild(leftEar);
        //右耳
        var rightEar = new eui.Image();
        rightEar.source = "ydEarRight_png";
        // rightEar.anchorOffsetX = 114;
        rightEar.anchorOffsetY = 106;
        rightEar.x = 49;
        rightEar.y = -336 + 177;
        head["rightEar"] = rightEar;
        head.addChild(rightEar);
        //脸
        var face = new eui.Image();
        face.source = "ydFace_png";
        face.x = -243 / 2;
        face.y = -266;
        head.addChild(face);
        //眼睛2
        var eye2 = new eui.Image();
        eye2.source = "ydEye2_png";
        eye2.x = -133;
        eye2.y = -171;
        head["eye2"] = eye2;
        head.addChild(eye2);
        //眼睛1
        var eye1 = new eui.Image();
        eye1.source = "ydEye1_png";
        eye1.x = -133;
        eye1.y = -171;
        eye1.alpha = 0;
        head["eye1"] = eye1;
        head.addChild(eye1);
        //眼睛0
        var eye0 = new eui.Image();
        eye0.source = "ydEye0_png";
        eye0.x = -133;
        eye0.y = -171;
        eye0.alpha = 0;
        head["eye0"] = eye0;
        head.addChild(eye0);
        //嘴巴
        var mouth = new eui.Image();
        mouth.source = "ydMouth_png";
        mouth.anchorOffsetX = 56 / 2;
        mouth.anchorOffsetY = 31 / 2;
        mouth.x = 37;
        mouth.y = -68;
        head["mouth"] = mouth;
        head.addChild(mouth);
        //鼻子
        var nose = new eui.Image();
        nose.source = "ydNose_png";
        nose.x = -59;
        nose.y = -183;
        head["nose"] = nose;
        head.addChild(nose);

        //根据数据设置初始时装
        // this.scene.pigClothInit();

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

        this.aniUsual();
    }

    //根据vip等级更新鞋子
    public updateFeet(vipLevel) {
        this.main.leftFoot.source = "ydFootLeft" + vipLevel + "_png";
        this.main.rightFoot.source = "ydFootRight" + 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)
    }
    //运动猪普通状态动画
    private aniUsual() {
        var pig = this["main"];
        //猪头
        var head = egret.Tween.get(pig.head, { loop: true });
        head.to({ rotation: -2 }, 160)
            .to({ rotation: 0 }, 170)
            .to({ rotation: -2 }, 170)
            .to({ rotation: 0 }, 160)
            .to({ rotation: -2 }, 170)
            .to({ rotation: 0 }, 170)
            .wait(1000)
        // .call(() => {
        // if (!this.status) {
        //     this.ydPigUsualToStand();
        // }
        //这里统一回调
        // console.log(111)
        // }, this)
        //身体   初始位置-3   
        var body = egret.Tween.get(pig.body, { loop: true });
        body.to({ x: 0 }, 160)
            .to({ x: -3 }, 170)
            .to({ x: 0 }, 170)
            .to({ x: -3 }, 160)
            .to({ x: 0 }, 170)
            .to({ x: -3 }, 170)
            .wait(1000);
        //左手
        var leftHand = egret.Tween.get(pig.leftHand, { loop: true });
        leftHand.to({ rotation: 2 }, 160)
            .to({ rotation: 0 }, 170)
            .to({ rotation: 2 }, 170)
            .to({ rotation: 0 }, 160)
            .to({ rotation: 2 }, 170)
            .to({ rotation: 0 }, 170)
            .wait(1000)
        //右手
        var rightHand = egret.Tween.get(pig.rightHand, { loop: true });
        rightHand.to({ rotation: -2 }, 160)
            .to({ rotation: 0 }, 170)
            .to({ rotation: -2 }, 170)
            .to({ rotation: 0 }, 160)
            .to({ rotation: -2 }, 170)
            .to({ rotation: 0 }, 170)
            .wait(1000);
        //左腿
        var leftLeg = egret.Tween.get(pig.leftLeg, { loop: true });
        leftLeg.to({ rotation: 4 }, 160)
            .to({ rotation: 0 }, 170)
            .to({ rotation: 4 }, 170)
            .to({ rotation: 0 }, 160)
            .to({ rotation: 4 }, 170)
            .to({ rotation: 0 }, 170)
            .wait(1000)
        //右腿
        var rightLeg = egret.Tween.get(pig.rightLeg, { loop: true });
        rightLeg.to({ rotation: 2 }, 160)
            .to({ rotation: 0 }, 170)
            .to({ rotation: 2 }, 170)
            .to({ rotation: 0 }, 160)
            .to({ rotation: 2 }, 170)
            .to({ rotation: 0 }, 170)
            .wait(1000);
        //右脚
        var rightFoot = egret.Tween.get(pig.rightFoot, { loop: true });
        rightFoot.to({ rotation: 8 }, 160)
            .to({ rotation: 0 }, 170)
            .to({ rotation: 8 }, 170)
            .to({ rotation: 0 }, 160)
            .to({ rotation: 8 }, 170)
            .to({ rotation: 0 }, 170)
            .wait(1000);
        //嘴   初始位置x:37,y:-68,rotation:0
        pig.head.mouth.rotation = 10;
        pig.head.mouth.x = 37 - 5;
        pig.head.mouth.y = -68 + 2;
        var mouth = egret.Tween.get(pig.head.mouth, { loop: true });
        mouth.to({ rotation: 0, x: 37, y: -68 }, 230)
            // .to({ rotation: 0,x:37,y:-68 }, 1030)
            .wait(1030)
            .to({ rotation: 10, x: 32, y: -66 }, 200)
            .wait(540);
        //左耳
        pig.head.leftEar.rotation = 0;
        var leftEar = egret.Tween.get(pig.head.leftEar, { loop: true });
        leftEar.wait(1300)
            .to({ rotation: -4 }, 200)
            .to({ rotation: 0 }, 160)
            .to({ rotation: -4 }, 170)
            .to({ rotation: 0 }, 170)
        //右耳
        pig.head.leftEar.rotation = 0;
        var rightEar = egret.Tween.get(pig.head.rightEar, { loop: true });
        rightEar.wait(1300)
            .to({ rotation: 4 }, 200)
            .to({ rotation: 0 }, 160)
            .to({ rotation: 4 }, 170)
            .to({ rotation: 0 }, 170)
    }

    //运动猪从普通状态到站立状态
    private usualToStand() {

        //显示交互
        // this.threeActionBtnShow();

        var pig = this["main"];
        //如果点击了身体，根据标志位，清除特定tween。执行切换动画，及站立时的循环动画
        egret.Tween.removeTweens(pig.head.leftEar);
        egret.Tween.removeTweens(pig.head.rightEar);
        egret.Tween.removeTweens(pig.head.mouth);
        egret.Tween.removeTweens(pig.head);
        egret.Tween.removeTweens(pig.body);
        egret.Tween.removeTweens(pig.leftHand);
        egret.Tween.removeTweens(pig.rightHand);
        egret.Tween.removeTweens(pig.leftLeg);
        egret.Tween.removeTweens(pig.rightLeg);
        egret.Tween.removeTweens(pig.rightFoot);
        //执行切换
        //躯体  初始位置x:-3 y:-189
        egret.Tween.get(pig.body)
            .to({ x: -3 + 17 }, 30)
            .to({ y: -189 + 12 }, 100)
            .to({ y: -189 + 12 - 84 }, 130)
            .to({ y: -189 + 12 }, 120)
            .to({ y: -189 }, 70);
        //左手，位移和躯体一致 -42;-190    修改角度
        egret.Tween.get(pig.leftHand)
            .to({ x: -42 + 17, rotation: 8 }, 30)
            .to({ y: -190 + 12, rotation: 19 }, 100)
            .to({ y: -190 + 12 - 84, rotation: 8 }, 130)
            .to({ y: -190 + 12, rotation: 19 }, 120)
            .to({ y: -190, rotation: 8 }, 70);
        //右手，位移和躯体一致 22;-198
        egret.Tween.get(pig.rightHand)
            .to({ x: 22 + 17, rotation: 7 }, 30)
            .to({ y: -198 + 12, rotation: -4 }, 100)
            .to({ y: -198 + 12 - 84, rotation: 7 }, 130)
            .to({ y: -198 + 12, rotation: -4 }, 120)
            .to({ y: -198, rotation: 7 }, 70);
        //猪头 ，初始位置  -1   -177
        egret.Tween.get(pig.head)
            .to({ x: -1 + 17 }, 30)
            .to({ y: -177 + 7 + 12 }, 100)
            .to({ y: -177 - 84 }, 130)
            .to({ y: -177 + 4 }, 120)
            .to({ y: -177 }, 70);
        //左腿 初始位置 -30 -29
        egret.Tween.get(pig.leftLeg)
            .to({ rotation: 11 }, 30)
            .wait(100)
            .to({ y: -29 - 72 }, 130)
            .to({ y: -29 }, 120)
            .to({ y: -29 }, 70);
        //右腿 初始位置 58 -45
        egret.Tween.get(pig.rightLeg)
            .to({ rotation: 11 }, 30)
            .wait(100)
            .to({ y: -45 - 72 }, 130)
            .to({ y: -45 }, 120)
            .to({ y: -45 }, 70);
        //左脚  初始位置-83 -56
        egret.Tween.get(pig.leftFoot)
            // .to({ rotation: 18 }, 30)
            .wait(130)
            .to({ y: -56 - 72 }, 130)
            .to({ y: -56 }, 120)
            .to({ y: -56 }, 70);
        //右脚  初始位置 62 -29
        egret.Tween.get(pig.rightFoot)
            .to({ rotation: 18 }, 30)
            .wait(100)
            .to({ y: -29 - 72 }, 130)
            .to({ y: -29 }, 120)
            .to({ y: -29 }, 70);
        //地盘
        egret.Tween.get(this["land"])
            .wait(30)
            .to({ scaleX: 1.2, scaleY: 1.2 }, 100)
            .to({ scaleX: 0.8, scaleY: 0.8 }, 130)
            .to({ scaleX: 1.1, scaleY: 1.1 }, 120)
            .to({ scaleX: 1, scaleY: 1 }, 70)
            .call(() => {
                this.aniComplete = true;
                this.isStand = true;
            }, this);
        //左耳
        pig.head.leftEar.rotation = 0;
        egret.Tween.get(pig.head.leftEar, { loop: true })
            .wait(30)
            .to({ rotation: 10 }, 100)
            .to({ rotation: -4 }, 130)
            .to({ rotation: -1 }, 120)
            .to({ rotation: 0 }, 70)
            .wait(1300);
        //右耳
        pig.head.rightEar.rotation = 0;
        egret.Tween.get(pig.head.rightEar, { loop: true })
            .wait(30)
            .to({ rotation: -10 }, 100)
            .to({ rotation: 4 }, 130)
            .to({ rotation: 1 }, 120)
            .to({ rotation: 0 }, 70)
            .wait(1300)
        // .call(() => {
        // if (this.status) {
        //     this.ydPigStandToUsual();
        // }
        // }, this);
    }
    //运动猪从站立到普通状态  反向   在执行完交互后就执行
    private standToUsual() {
        //如果点击其他地方，反向复原后执行usual动画
        var pig = this["main"];
        egret.Tween.removeTweens(pig.head.leftEar);
        egret.Tween.removeTweens(pig.head.rightEar);
        //执行切换
        //躯体  初始位置x:-3 y:-189
        egret.Tween.get(pig.body)
            .to({ y: -189 + 12 }, 70)
            .to({ y: -189 + 12 - 84 }, 120)
            .to({ y: -189 + 12 }, 130)
            .to({ y: -189 }, 100)
            .to({ x: -3 }, 30)
        //左手，位移和躯体一致 -42;-190
        egret.Tween.get(pig.leftHand)
            .to({ y: -190 + 12 }, 70)
            .to({ y: -190 + 12 - 84 }, 120)
            .to({ y: -190 + 12 }, 130)
            .to({ y: -190 }, 100)
            .to({ x: -42, rotation: 0 }, 30)
        //右手，位移和躯体一致 22;-198
        egret.Tween.get(pig.rightHand)
            .to({ y: -198 + 12, rotation: -4 }, 70)
            .to({ y: -198 + 12 - 84, rotation: 7 }, 120)
            .to({ y: -198 + 12, rotation: -4 }, 130)
            .to({ y: -198, rotation: 7 }, 100)
            .to({ x: 22, rotation: 0 }, 30)
        //猪头 ，初始位置  -1   -177
        egret.Tween.get(pig.head)
            .to({ y: -177 + 4 }, 70)
            .to({ y: -177 - 84 }, 120)
            .to({ y: -177 + 7 + 12 }, 130)
            .to({ y: -177 }, 100)
            .to({ x: -1 }, 30)
        //左腿 初始位置 -30 -29
        egret.Tween.get(pig.leftLeg)
            .to({ y: -29 }, 70)
            .to({ y: -29 - 72 }, 120)
            .to({ y: -29 }, 130)
            .wait(100)
            .to({ rotation: 0 }, 30)
        //右腿 初始位置 58 -45
        egret.Tween.get(pig.rightLeg)
            .to({ y: -45 }, 70)
            .to({ y: -45 - 72 }, 120)
            .to({ y: -45 }, 130)
            .wait(100)
            .to({ rotation: 0 }, 30)
        //左脚  初始位置-83 -56
        egret.Tween.get(pig.leftFoot)
            .to({ y: -56 }, 70)
            .to({ y: -56 - 72 }, 120)
            .to({ y: -56 }, 130)
            .wait(130)
        //右脚  初始位置 62 -29
        egret.Tween.get(pig.rightFoot)
            .to({ y: -29 }, 70)
            .to({ y: -29 - 72 }, 120)
            .to({ y: -29 }, 130)
            .wait(100)
            .to({ rotation: 0 }, 30)

        //地盘
        egret.Tween.get(this["land"])
            .to({ scaleX: 1.1, scaleY: 1.1 }, 70)
            .to({ scaleX: 0.8, scaleY: 0.8 }, 120)
            .to({ scaleX: 1.2, scaleY: 1.2 }, 130)
            .to({ scaleX: 1, scaleY: 1 }, 100)
            .wait(40)
            .call(() => {
                // this.pig.touchEnabled = true;
                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;
        }
        var head = this.main.head;
        head.addChild(this.feedMouth);
        head.mouth.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.mouth.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)
    }
}