import Vector3 = Laya.Vector3;
import Sprite3D = Laya.Sprite3D;
import { GamePool } from "../GameLogic/GamePool";
import { PlayerManager } from "../GameLogic/PlayerManager";
import MainRoleManager from "../Manager/MainRoleManager";
import { Mathf } from "../Util/Mathf";
import { MatrixTransform } from "../Util/MatrixTransform";
import CameraManager from "../Manager/CameraManager";
import { MusicType } from "../GameLogic/ColliderAndMusicType";
export default class DisplayerCarController {
    private static instance: DisplayerCarController;
    public static get Instance(): DisplayerCarController {
        return DisplayerCarController.instance || (DisplayerCarController.instance = new DisplayerCarController());
    }
    private _scene: Laya.Scene3D;
    private _camera: Laya.Camera;
    private _light: Laya.DirectionLight;
    private _point: Laya.Sprite3D;
    public InitDis(par: Laya.Image) {
        if (!this._scene) {
            this._scene = new Laya.Scene3D();
            this._camera = new Laya.Camera();
            this._camera.enableHDR = false;
            this._scene.addChild(this._camera);
            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._scene.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._camera.viewport = new Laya.Viewport(par.x * or, par.y * or, par.width * or, par.height * or);
            this._camera.orthographic = false;
            this._camera.enableHDR = false;
            this._camera.clearFlag = 2;
            this._camera.fieldOfView = 60;
            this._camera.transform.position = new Vector3(
                0,
                1.52,
                3.12);
            this._camera.transform.rotation = new Laya.Quaternion(
                0,
                0,
                0,
                1);
        }
        let or = 1;// Laya.Browser.width / 720;
        this._camera.viewport = new Laya.Viewport(par.x * or, par.y * or, par.width * or, par.height * or);
        par.addChild(this._scene);
        this.CreatePlayer();
    }

    private player: Sprite3D;

    CreatePlayer(id?) {
        if (!id) id = PlayerManager.Instance.equipId;
        if (!this.player) {
            let p = GamePool.Instance.SetActiveObject(GamePool.pooltype.player + id);
            if (p) this.player = p;
            this._scene.addChild(this.player);
            this.player.transform.localPosition = Mathf.VecZero;
            this.player.transform.localRotationEuler = Mathf.VecZero;
            //return;
        } else {
            GamePool.Instance.SetDisableObject(this.player);
            let p = GamePool.Instance.SetActiveObject(GamePool.pooltype.player + id);
            if (p) this.player = p;
            this._scene.addChild(this.player);
            this.player.transform.localPosition = Mathf.VecZero;
            this.player.transform.localRotationEuler = Mathf.VecZero;
        }
        let anima= this.player.getChildAt(0).getComponent(Laya.Animator);
       
         anima.play(MusicType.Dance);
         anima.speed = 1;
    }

    Reset() {
        if (this._scene) {
            this._scene.removeSelf();
        }
        if (this.player) {
            GamePool.Instance.SetDisableObject(this.player);
            this.player = null;
        }
    }



}