import Vector3 = Laya.Vector3;
import Sprite3D = Laya.Sprite3D;
import { GamePool } from "../Game/GamePool";
import { PlayerManager } from "../Game/PlayerManager";
import {MainRoleManager} from "../Manager/MainRoleManager";
import { MathTool } from "../Util/MathTool";
import { MatrixTransform } from "../Util/MatrixTransform";
import {CameraManager} from "../Manager/CameraManager";
import { MusicType } from "../Game/ColliderAndMusicType";
export  class DisplayerCarController {
    private static instance: DisplayerCarController;
    public static get Instance(): DisplayerCarController {
        return DisplayerCarController.instance || (DisplayerCarController.instance = new DisplayerCarController());
    }
    
    

    private player: Sprite3D;

    CreatePlayer(id?) {
        if (!id) id = PlayerManager.Instance.equipId;
        if (!this.player) {
            let p = GamePool.Instance.GetModel(GamePool.modeltype.Player + id);
            if (p) this.player = p;
            this.mainscene.addChild(this.player);
            this.player.transform.localPosition = MathTool.VecZero;
            this.player.transform.localRotationEuler = MathTool.VecZero;
            //return;
        } else {
            GamePool.Instance.RecoverModel(this.player);
            let p = GamePool.Instance.GetModel(GamePool.modeltype.Player + id);
            if (p) this.player = p;
            this.mainscene.addChild(this.player);
            this.player.transform.localPosition = MathTool.VecZero;
            this.player.transform.localRotationEuler = MathTool.VecZero;
        }
        let anima= this.player.getChildAt(0).getComponent(Laya.Animator);
       
         anima.play(MusicType.Dance);
         anima.speed = 1;
    }

    private mainscene: Laya.Scene3D;
    private maincamera: Laya.Camera;
    private light: Laya.DirectionLight;
    private _point: Laya.Sprite3D;

    Reset() {
        if (this.mainscene) {
            this.mainscene.removeSelf();
        }
        if (this.player) {
            GamePool.Instance.RecoverModel(this.player);
            this.player = null;
        }
    }

    public InitDis(par: Laya.Image) {
        if (!this.mainscene) {
            this.mainscene = new Laya.Scene3D();
            this.maincamera = new Laya.Camera();
            this.maincamera.enableHDR = false;
            this.mainscene.addChild(this.maincamera);
            this.light = new Laya.DirectionLight();
            this.light.transform.rotation = new Laya.Quaternion(
                -0.24776,
                0,
                0,
                0.9688215);
            this.light.intensity = 0.45;
            this.light.color = new Vector3(1, 1, 1);
            this.mainscene.addChild(this.light);
            //  this._point = new Laya.Sprite3D();
            //   this._point.transform.position = new Vector3(0, 0.2, 0);
            //   this._scene.addChild(this._point);
            let or = 1;// Laya.Browser.width / 720;
            this.maincamera.viewport = new Laya.Viewport(par.x * or, par.y * or, par.width * or, par.height * or);
            this.maincamera.orthographic = false;
            this.maincamera.enableHDR = false;
            this.maincamera.clearFlag = 2;
            this.maincamera.fieldOfView = 60;
            this.maincamera.transform.position = new Vector3(
                0,
                1.52,
                3.12);
            this.maincamera.transform.rotation = new Laya.Quaternion(
                0,
                0,
                0,
                1);
        }
        let or = 1;// Laya.Browser.width / 720;
        this.maincamera.viewport = new Laya.Viewport(par.x * or, par.y * or, par.width * or, par.height * or);
        par.addChild(this.mainscene);
        this.CreatePlayer();
    }
}