//第一段到11
const msg1 = [
    //0
    {
        source: "runOneDay1",
        x: 50,
        y: 646,
        s: 1,
    },
    //1
    {
        source: "runOneDay2",
        x: 73,
        y: 646,
        s: 1,
    },
    //2
    {
        source: "runOneDay3",
        x: 96,
        y: 646,
        s: 1,
    },
    //3
    {
        source: "runOneDay4",
        x: 119,
        y: 646,
        s: 1,
    },
    //4
    {
        source: "runOneDay5",
        x: 142,
        y: 646,
        s: 1,
    },
    //5
    {
        source: "runOneDay6",
        x: 165,
        y: 646,
        s: 1,
    },
    //6
    {
        source: "runOneDay7",
        x: 188,
        y: 646,
        s: 1,
    },
    //7
    {
        source: "runOneDay8",
        x: 211,
        y: 646,
        s: 1,
    },
    //8
    {
        source: "runOneDay9",
        x: 234,
        y: 646,
        s: 1,
    },
    //9
    {
        source: "runOneDay10",
        x: 257,
        y: 646,
        s: 1,
    },
    //10
    {
        source: "runOneDay11",
        x: 282,
        y: 646,
        s: 1,
    }
]
//第二段到
const msg2 = [
    {
        source: "runStraight2",
        x: 307,
        y: 646,
        s: 1,
    },
    {
        source: "runStraight3",
        x: 332,
        y: 646,
        s: 1,
    },
    {
        source: "runStraight4",
        x: 357,
        y: 646,
        s: 1,
    },
    {
        source: "runStraight5",
        x: 382,
        y: 646,
        s: 1,
    },
    {
        source: "runStraight6",
        x: 407,
        y: 646,
        s: 1,
    },
    {
        source: "runStraight7",
        x: 432,
        y: 646,
        s: 1,
    },
    {
        source: "runStraight8",
        x: 457,
        y: 646,
        s: 1,
    },
    {
        source: "runStraight9",
        x: 482,
        y: 646,
        s: 1,
    },
    {
        source: "runStraight10",
        x: 507,
        y: 646,
        s: 1,
    },
    {
        source: "runStraight11",
        x: 534,
        y: 646,
        s: 1,
    }
]

//第三段
const msg3 = [
    {
        source: "runRight2",
        x: 572,
        y: 663,
        s: 1,
    },
    {
        source: "runRight3",
        x: 608,
        y: 695,
        s: 1,
    },
    {
        source: "runRight4",
        x: 610,
        y: 727,
        s: 1,
    },
    {
        source: "runRight5",
        x: 580,
        y: 759,
        s: 1,
    },
    {
        source: "runRight6",
        x: 555,
        y: 791,
        s: 1,
    },
    {
        source: "runRight7",
        x: 525,
        y: 823,
        s: 1,
    },
    {
        source: "runRight8",
        x: 495,
        y: 835,
        s: 1,
    },
    {
        source: "runRight9",
        x: 480,
        y: 858,
        s: 1,
    },
    {
        source: "runRight10",
        x: 460,
        y: 858,
        s: 1,
    },
]

//第四段
const msg4 = [
    {
        source: "runStraight2",
        x: 437,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight3",
        x: 414,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight4",
        x: 390,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight5",
        x: 366,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight6",
        x: 343,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight7",
        x: 320,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight8",
        x: 297,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight9",
        x: 274,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight10",
        x: 251,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight11",
        x: 222,
        y: 858,
        s: -1,
    }
]
//第五段
const msg5 = [
    {
        source: "runStraight2",
        x: 201,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight3",
        x: 179,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight4",
        x: 158,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight5",
        x: 136,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight6",
        x: 115,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight7",
        x: 93,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight8",
        x: 72,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight9",
        x: 50,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight10",
        x: 29,
        y: 858,
        s: -1,
    },
    {
        source: "runStraight11",
        x: 7,
        y: 858,
        s: -1,
    }
]

//第六段
const msg6 = [
    {
        source: "runLeft2",
        x: 15,
        y: 879,
        s: 1,
    },
    {
        source: "runLeft3",
        x: 25,
        y: 901,
        s: 1,
    },
    {
        source: "runLeft4",
        x: 45,
        y: 932,
        s: 1,
    },
    {
        source: "runLeft5",
        x: 65,
        y: 953,
        s: 1,
    },
    {
        source: "runLeft6",
        x: 80,
        y: 980,
        s: 1,
    },
    {
        source: "runLeft7",
        x: 115,
        y: 1005,
        s: 1,
    },
    {
        source: "runLeft8",
        x: 160,
        y: 1032,
        s: 1,
    },
    {
        source: "runLeft9",
        x: 200,
        y: 1042,
        s: 1,
    },
    {
        source: "runLeft10",
        x: 249,
        y: 1055,
        s: 1,
    },
    {
        source: "runLeft11",
        x: 288,
        y: 1060,
        s: 1,
    },
]
//第七段
const msg7 = [
    {
        source: "runStraight2",
        x: 314,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight3",
        x: 340,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight4",
        x: 366,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight5",
        x: 392,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight6",
        x: 418,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight7",
        x: 444,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight8",
        x: 470,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight9",
        x: 496,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight10",
        x: 522,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight11",
        x: 530,
        y: 1060,
        s: 1,
    }
]

export class RunAni extends egret.DisplayObjectContainer {

    /**
     * 步数，1到7
     * 0表示原位
     */
    step: number = 0;
    endIndex: number = 0;
    sourceAll: string[];
    currentFrame: number = 0;
    totalFrames: number;
    isPlay: boolean;
    callback;
    /**
     * 
     */
    showImage: egret.Bitmap;
    data: any[];
    constructor() {
        super();
        this.showImage = new egret.Bitmap();
        this.addChild(this.showImage);
        this.data = []
        var aaa = [msg1, msg2, msg3, msg4, msg5, msg6, msg7]
        for (var i = 0; i < aaa.length; i++) {
            this.data = this.data.concat(aaa[i]);
            stepToEndIndex[i + 1] = this.data.length - 1;
        }
        this.go(0);
        console.log(stepToEndIndex)
        this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this)
    }

    private count = 0;
    onEnterFrame() {
        if (!this.isPlay) {
            this.count = 0
            return
        }
        this.count++;
        if (this.count % 5 == 0) {
            this.currentFrame++;
            if (this.currentFrame == this.endIndex) {
                this.isPlay = false
                this.callback && this.callback();
            }
            this.go(this.currentFrame);
        }
    }

    /**
     * 运动
     * @param step 终点状态
     * @param callback 
     */
    play(step: number, callback?:Function) {
        this.step = step;
        this.endIndex = stepToEndIndex[step];
        if (this.currentFrame == this.endIndex) {
            callback && callback();
            return
        }
        this.isPlay = true;
        this.callback = callback;
    }
    /**
     * 重置的当前状态
     */
    reset(step:number) {
        this.currentFrame=stepToEndIndex[step]
        this.go(stepToEndIndex[step]);
    }

   private go(index) {
        var data = this.data[index];
        this.showImage.texture = RES.getRes(data.source + "_png");
        this.showImage.y = data.y
        if (data.s == -1) {
            this.showImage.scaleX = -1;
            this.showImage.x = data.x + 146
        } else {
            this.showImage.scaleX = 1;
            this.showImage.x = data.x
        }
    }
}

const stepToEndIndex = {
    0: 0,
    1: 0,
    2: 0,
    3: 0,
    4: 0,
    5: 0,
    6: 0,
    7: 0,
}


