import { AssetManager } from "../Tools/AssetManager";
import GamePool from "../Tools/GamePool";
import { EventManager, EventState } from "../Tools/EventManager";
import { PlayerControl } from "./PlayerControl";
import { ViewManager, ViewType } from "../View/ViewManager";


export class GameManager{
    private static ins:GameManager;
    
    //敌人数组
    public EnemyArray:Array<Laya.Sprite3D> = [];
    public mainscene:Laya.Scene3D;
    // //瞄准线位置
    // private raypos:Laya.Sprite3D;
    // //弓位置
    // private archerpos:Laya.Sprite3D;
    // //箭位置
    // private arrowpos:Laya.Sprite3D;

    constructor(){
        GameManager.ins = this;
        EventManager.EventOn(EventState.SceneInit,this,this.Init)
    }
    public static get Instance():GameManager{
        if(GameManager.ins){
            return GameManager.ins
        }
        else{
            return new GameManager();
        }
    }
    Init(){
        this.CreatCharacter();
        this.mainscene = AssetManager.Instance.mainscene;
        Laya.stage.addChildAt(this.mainscene,0);
        ViewManager.Instance.ShowView(ViewType.MainView);
    }
    CreatCharacter(){
        this.CreatPlayer();
        this.CreatEnemy();
    }
    CreatPlayer(){
        var playermodel = GamePool.Instance.GetModel(AssetManager.Instance.Player.get(1),"");
        var horsemodel = GamePool.Instance.GetModel(AssetManager.Instance.Horse.get(1),"");
        playermodel.addChild(horsemodel);
        playermodel.transform.position = new Laya.Vector3(0,0,0);
        horsemodel.transform.localScale = new  Laya.Vector3(1,1,1);
        horsemodel.transform.localPosition = new Laya.Vector3(0,0,0);
        var raypos = playermodel.getChildByName("raypos") as Laya.Sprite3D;
        var archerpos = playermodel.getChildByName("archerpos") as Laya.Sprite3D;
        var arrowpos = playermodel.getChildByName("arrowpos") as Laya.Sprite3D;

        this.CreatRay(raypos.transform.position.clone(),raypos.transform.rotationEuler.clone(),raypos);
        this.CreatArcher(archerpos.transform.position.clone(),archerpos.transform.rotationEuler.clone(),archerpos);
        this.CreatArrow(arrowpos.transform.position.clone(),arrowpos.transform.rotationEuler.clone(),arrowpos);

        PlayerControl.Instance.Init(playermodel);
        AssetManager.Instance.mainscene.addChild(playermodel);
    }
    CreatEnemy(){
        
    }
    //创建瞄准线
    CreatRay(pos:Laya.Vector3,euler:Laya.Vector3,raypos:Laya.Sprite3D){
        var ray = this.CreatRayPrefab();
        raypos.addChild(ray);
        ray.transform.position =new Laya.Vector3(pos.x-0.3,pos.y,pos.z+0.5);
        ray.transform.rotationEuler =new Laya.Vector3(-90,euler.y,euler.z);
        let scale = ray.transform.scale.clone();
        scale.z = 0.1;
        scale.y = 3;
        scale.x = 2;
        ray.transform.scale = scale;
    }
    //创建瞄准线预设
    CreatRayPrefab():Laya.Sprite3D{
        var ray = GamePool.Instance.GetModel(AssetManager.Instance.Prop.get(8),"");
        return ray
    }
    //创建弓箭预设
    CreatArcher(_pos:Laya.Vector3,_euler:Laya.Vector3,archerpos:Laya.Sprite3D){
        var archer = GamePool.Instance.GetModel(AssetManager.Instance.Prop.get(6),"");
        archerpos.addChild(archer);
        archer.transform.position = _pos;
        archer.transform.rotationEuler =new Laya.Vector3(-90,0,0);
        var scale = archer.transform.scale.clone();
        scale.z = 0.8;
        archer.transform.scale = scale;
    }
    CreatArrow(_pos:Laya.Vector3,_euler:Laya.Vector3,arrowpos:Laya.Sprite3D){
        var arrow = GamePool.Instance.GetModel(AssetManager.Instance.Prop.get(5),"");
        arrowpos.addChild(arrow);
        arrow.transform.position = _pos;
        var euler = _euler;
        arrow.transform.rotationEuler = new Laya.Vector3(-90,euler.y,euler.z);
    }
    
}