import Sprite3D = Laya.Sprite3D;

import BitmapFont = Laya.BitmapFont;
import Stage = Laya.Stage;
import Text = Laya.Text;
import Browser = Laya.Browser;
import Handler = Laya.Handler;
import WebGL = Laya.WebGL;

export  class ConstructManager {
    private static instance: ConstructManager;
    public static get Instance(): ConstructManager {
        return this.instance || (this.instance = new ConstructManager());
    }
    private tarDis = 30;
    private constructArr = [];
    public Init(builds: Sprite3D) {
        this.constructArr.push(builds.getChildAt(2));
        this.constructArr.push(builds.getChildAt(1));
        this.constructArr.push(builds.getChildAt(0));
    }

    public CheckMainRolePos(_zp: number) {
        let d = this.tarDis - _zp;
        if (d > 0) return;
        this.SwitchRoad();
    }

    /*********************************************************************************** */
    private fontName: string = "diyFont";
    private loadFont(): void {
        var bitmapFont: BitmapFont = new BitmapFont();
        bitmapFont.loadFont("res/bitmapFont/test.fnt", new Handler(this, this.onFontLoaded, [bitmapFont]));
    }
    private onFontLoaded(bitmapFont: BitmapFont): void {
        bitmapFont.setSpaceWidth(10);
        Text.registerBitmapFont(this.fontName, bitmapFont);
        this.createText(this.fontName);
    }
    private createText(font: string): void {
        var txt: Text = new Text();
        txt.width = 250;
        txt.wordWrap = true;
        txt.text = "Do one thing at a time, and do well.";
        txt.font = font;
        txt.leading = 5;
        txt.pos(Laya.stage.width - txt.width >> 1, Laya.stage.height - txt.height >> 1);
        Laya.stage.addChild(txt);
    }
    /*********************************************************************************** */
    
    private SwitchRoad() {
        let rs = this.constructArr.pop();
        rs.transform.localPositionZ = this.tarDis + 60;
        this.constructArr.unshift(rs);
        this.tarDis = this.tarDis + 30;
    }

    public SetConstruct() {
        for (let index = this.constructArr.length - 1; index >= 0; index--) {
            const element = this.constructArr[index] as Sprite3D;
            element.transform.localPositionZ = (2 - index) * 30;
        }
        this.tarDis = 30;
    }
   
}