//第一段到6
const msg1 = [
    //0
    {
        source: "runStraight1",
        x: 33,
        y: 646,
        s: 1,
    },
    //1
    {
        source: "runStraight2",
        x: 81,
        y: 646,
        s: 1,
    },
    //2
    {
        source: "runStraight3",
        x: 118,
        y: 646,
        s: 1,
    },
    //3
    {
        source: "runStraight4",
        x: 159,
        y: 646,
        s: 1,
    },
    //4
    {
        source: "runStraight5",
        x: 201,
        y: 646,
        s: 1,
    },
    //5
    {
        source: "runStraight6",
        x: 237,
        y: 646,
        s: 1,
    },
    //6
    {
        source: "runStraight7",
        x: 282,
        y: 646,
        s: 1,
    },
]
//第二段到12
const msg2 = [
    //0
    {
        source: "runStraight2",
        x: 319,
        y: 646,
        s: 1,
    },
    //1
    {
        source: "runStraight3",
        x: 357,
        y: 646,
        s: 1,
    },
    //2
    {
        source: "runStraight4",
        x: 398,
        y: 646,
        s: 1,
    },
    //3
    {
        source: "runStraight5",
        x: 435,
        y: 646,
        s: 1,
    },
    //4
    {
        source: "runStraight6",
        x: 469,
        y: 646,
        s: 1,
    },
    //5
    {
        source: "runRight1",
        x: 544,
        y: 655,
        s: 1,
    },
]

//第三段
const msg3 = [
    {
        source: "runRight2",
        x: 602,
        y: 673,
        s: 1,
    },
    {
        source: "runRight3",
        x: 648,
        y: 747,
        s: 1,
    },
    {
        source: "runRight4",
        x: 613,
        y: 797,
        s: 1,
    },
    {
        source: "runRight5",
        x: 567,
        y: 845,
        s: 1,
    },
    {
        source: "runRight6",
        x: 497,
        y: 868,
        s: 1,
    }
]

//第四段
const msg4 = [
    {
        source: "runStraight2",
        x: 422,
        y: 860,
        s: -1,
    },
    {
        source: "runStraight3",
        x: 386,
        y: 860,
        s: -1,
    },
    {
        source: "runStraight4",
        x: 351,
        y: 860,
        s: -1,
    },
    {
        source: "runStraight5",
        x: 313,
        y: 860,
        s: -1,
    },
    {
        source: "runStraight6",
        x: 277,
        y: 860,
        s: -1,
    },
    {
        source: "runStraight7",
        x: 232,
        y: 860,
        s: -1,
    }
]
//第五段
const msg5 = [
    {
        source: "runStraight2",
        x: 196,
        y: 860,
        s: -1,
    },
    {
        source: "runStraight3",
        x: 161,
        y: 860,
        s: -1,
    },
    {
        source: "runStraight4",
        x: 127,
        y: 860,
        s: -1,
    },
    {
        source: "runStraight5",
        x: 91,
        y: 860,
        s: -1,
    },
    {
        source: "runStraight6",
        x: 59,
        y: 860,
        s: -1,
    },
    {
        source: "runLeft1",
        x: 51,
        y: 869,
        s: 1,
    },
]

//第六段
const msg6 = [
    {
        source: "runLeft2",
        x: 45,
        y: 938,
        s: 1,
    },
    {
        source: "runLeft3",
        x: 80,
        y: 990,
        s: 1,
    },
    {
        source: "runLeft4",
        x: 101,
        y: 1021,
        s: 1,
    },
    {
        source: "runLeft5",
        x: 128,
        y: 1057,
        s: 1,
    },
    {
        source: "runLeft6",
        x: 181,
        y: 1065,
        s: 1,
    },
    {
        source: "runStraight5",
        x: 167,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight7",
        x: 200,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight3",
        x: 227,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight6",
        x: 254,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight7",
        x: 288,
        y: 1060,
        s: 1,
    },
]
//第七段
const msg7 = [
    {
        source: "runStraight2",
        x: 330,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight3",
        x: 375,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight4",
        x: 425,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight5",
        x: 481,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight6",
        x: 534,
        y: 1060,
        s: 1,
    },
    {
        source: "runStraight7",
        x: 591,
        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,
}


