import { Runner } from "./Runner";
import PoolManager from "./PoolManager";
import { ResourcesManager } from "./ResourcesManager";
import { ScenceManager } from "./ScenceManager";
import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import Vector4 = Laya.Vector4;
import { GameUtils } from "../../GameUtils/GameUtils";
import { Queue } from "./Queue";
import { LevelManager } from "./LevelManager";

export class AImanager
{
    private static instance:AImanager;
    constructor()
    {
        AImanager.instance = this;
    }
    public static get Instance():AImanager
    {
        if(AImanager.instance)
        {
            return AImanager.instance;
        }
        else
        {
            return new AImanager();
        }
    }

    m_aiNum = 5;
    public m_aiList:Runner[] = [];
    randombodycolor = 
    [
        new Vector4(0.25,0.5,1,1),
        new Vector4(0.5,0.25,1,1),
        new Vector4(1,0.25,1,1),
        new Vector4(1,0.25,0.5,1),
    ]
    
    public CreatAi()
    {
        var level = LevelManager.Instance.curLevel;
        for(var i = 0;i < this.m_aiNum;i++)
        {
            var res = ResourcesManager.Instance.roleassets;
            var spr = res[GameUtils.getRandomInt(1,6)].getChildAt(0) as Sprite3D;
            var model = PoolManager.Instance.CreatMeshSprite3D(spr,"");
            ScenceManager.Instance.GameScence.addChild(model);
            var startpoint = new Vector3(-1.4+(i*0.7),0,13);
            // if(i == 2)
            // {
            //     startpoint = new Vector3(startpoint.x + 0.2,startpoint.y,startpoint.z);
            // }
            model.transform.position = startpoint;
            var runner = model.addComponent(Runner) as Runner;
            runner.Ai = true;
            runner.m_speed = GameUtils.getRandoms(-0.03,0.02) + level.npcspeed;
            //runner.SetExtraSpeed(0.15,1);
            runner.aioffestx = GameUtils.getRandoms(-0.001,0.001);
            runner.Id = i;
            runner.name = "草莓" + (runner.Id +1) + "号";
            runner.aimiss = level.NPCmiss;
            this.m_aiList.push(runner);

            Queue.Instance.signup(runner);
        }
    }

    ClearAis()
    {
        for(var i = 0;i < this.m_aiList.length;i++)
        {
            if(this.m_aiList[i].m_sprite.active)
            {
                this.m_aiList[i].m_flySprite.active = true;
                this.m_aiList[i].m_crown.active = true;
                PoolManager.Instance.RecoverMeshSprite3D(this.m_aiList[i].m_crown);
                PoolManager.Instance.RecoverMeshSprite3D(this.m_aiList[i].m_flySprite);
                PoolManager.Instance.RecoverMeshSprite3D(this.m_aiList[i].m_sprite);
            }
        }
        this.m_aiList = [];
    }

    RessetAis()
    {
        this.ClearAis();
        this.CreatAi();
    }
}