import { LevelManager } from "./LevelManager";
import { AssetManager } from "../Resources/AssetManager";
import { RoleMoveBehav } from "../Game/RoleMoveBehav";

export class PlayerManager{
    private static ins:PlayerManager;
    constructor(){
        PlayerManager.ins = this;
    }
    public static get Instance():PlayerManager{
        if(PlayerManager.ins == null){
            return new PlayerManager();
        }
        return PlayerManager.ins;
    }
    public PlayerNode:Laya.Sprite3D;
    public player:Laya.Sprite3D;


    Init(){
        if(this.PlayerNode == null){
            this.PlayerNode = new Laya.Sprite3D();
            LevelManager.Instance.spritepool.addChild(this.PlayerNode);
            if(this.player == null){
                let playerpref = AssetManager.Instance.Character.get(2) as Laya.Sprite3D;
                this.player = Laya.Sprite3D.instantiate(playerpref);
                this.PlayerNode.addChild(this.player);
            }
        }
        this.PlayerNode.transform.position = AssetManager.Instance.playerpos.transform.position.clone();
        this.PlayerNode.transform.rotation = AssetManager.Instance.playerpos.transform.rotation.clone();
        this.player.transform.localPosition = new Laya.Vector3(0,0,0) ;
    }
    EnablePlayer(){
        this.PlayerNode.addComponent(RoleMoveBehav);
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.OnMouseDown);
    }
    DisablePlayer(){
        let _move = this.PlayerNode.getComponent(RoleMoveBehav) as RoleMoveBehav;
        if(_move){
            _move.destroy();
        }
        Laya.stage.off(Laya.Event.MOUSE_DOWN,this,this.OnMouseDown);
    }
    trun = true;
    first = true;
    OnMouseDown(){
        if(this.first){
            this.first = false;
            return;
        }
        if(this.trun){
            this.PlayerNode.transform.rotationEuler = new Laya.Vector3(0,-90,0);
            this.trun = !this.trun;
        }
        else{
            this.PlayerNode.transform.rotationEuler = new Laya.Vector3(0,0,0);
            this.trun = !this.trun;
        }
        
    }
}