using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; /// /// 车辆管理类 /// 功能:车辆生成 /// 车辆AI /// /// public class CarSys : MonoBehaviour { //计时器 private float timer = 0f; //车辆种类列表 public GameObject[] carTypeList; //已生成车辆列表 //public List carList=new List(200); [HideInInspector] public Dictionary carList = new Dictionary(); // public int CarID = 0; public bool canCreat = false; //移动点列表 private Dictionary MoveList = new Dictionary(); //生成点列表 private Dictionary BornList = new Dictionary(); public void Init() { CheckBornoint(); CheckMovePoint(); } #region CarAI //检索移动点 public void CheckBornoint() { GameObject[] bornPoints = GameObject.FindGameObjectsWithTag("BornPoint"); for (int i = 0; i < bornPoints.Length; i++) { BornList.Add(bornPoints[i].name, bornPoints[i].transform); } } //检索移动点 public void CheckMovePoint() { GameObject[] moveObjs = GameObject.FindGameObjectsWithTag("MoveList"); for (int i = 0; i < moveObjs.Length; i++) { MoveList.Add(moveObjs[i].name, moveObjs[i].transform); } } //车辆AI移动函数 public IEnumerator CarMove(string lineName, NavMeshAgent agent) { if (lineName != "" && agent != null) { Transform moveListParent = null; MoveList.TryGetValue(lineName, out moveListParent); Drive drive = agent.GetComponent(); if (moveListParent != null) { Transform[] moveList = new Transform[moveListParent.childCount]; //遍历移动列表 for (int i = 0; i < moveListParent.childCount; i++) { moveList[i] = moveListParent.GetChild(i); //路径方向 Vector3 Dir = (moveList[i].transform.position - drive.transform.position).normalized; if (moveList[i] != null) { float t = 0; //向移动列表支点移动 agent.SetDestination(moveList[i].position); //携程检测距离与路况 while (true) { //**********重要***********// yield return new WaitForSeconds(0.02f); drive.drivetime += Time.deltaTime; //前方车辆安全距离检测 if (drive.drivetime > Const.SafeCheckTime) { drive.isnormalDrive = CheckFrontCar(drive) ? false : true; } if (drive.isnormalDrive == false) { agent.velocity = Vector3.zero; continue; } float distance = Vector3.Distance(agent.transform.position, moveList[i].position); //终点检测 if (distance <= 0.4f) { agent.velocity = Vector3.zero; t = 0; break; } //弯道减速 if (distance <= 20f && distance > 0.4f) { drive.dirveState = DriveState.Turn; agent.velocity = Dir * 15; } else { drive.dirveState = DriveState.Line; t += Time.deltaTime * Const.CarTurnSpeed; agent.velocity = Vector3.Lerp(Vector3.zero, Dir * 20, t); } } } } //回收车辆 carList.Remove(drive.carId); Destroy(agent.gameObject); GameRoot.Instance.infoWnd.SetCarText(-1); } } } //检测前方车辆 public bool CheckFrontCar(Drive drive) { if (drive.FrontCar == null) return false; if (Vector3.Distance(drive.transform.position, drive.FrontCar.transform.position) < Const.SafeDistance) { return true; } return false; #region MyRegion //for (int i = 0; i < carList.Count; i++) //{ // Vector3 target = carList[i].transform.position; // Vector3 dir = target - drive.transform.position; // drive.dot = Vector3.Dot(drive.transform.forward.normalized,dir.normalized); // //判断是否前方有小于安全距离的车辆 // if (Vector3.Dot(drive.transform.forward.normalized, dir.normalized) > Mathf.Cos(Mathf.PI/6) && Vector3.Distance(target, drive.transform.position) <= Const.SafeDistance) // { // Debug.Log(drive.gameObject.name); // return true; // } // else // { // continue; // } //} //return false; #endregion } #endregion #region 汽车随机生成 public void CreatCar(Transform parent) { // 随机创建车辆种类 int carIndex = Random.Range(0, carTypeList.Length - 1); //实例化车辆 GameObject car = GameObject.Instantiate(carTypeList[carIndex], parent.position, Quaternion.identity, parent); car.transform.localEulerAngles = parent.localEulerAngles; //car驾驶类赋值 Drive drive = car.GetComponent(); drive.carId = CarID; carList.Add(CarID, drive.gameObject); CarID++; if (drive.carId != 0) { GameObject go = null; carList.TryGetValue(drive.carId - 1, out go); drive.FrontCar = go; } //统计数量更新 GameRoot.Instance.infoWnd.SetCarText(1);//+1 //执行寻路AI NavMeshAgent agent = car.GetComponent(); if (agent != null) { StartCoroutine(CarMove(parent.name, agent)); } } public IEnumerator RandomTimeCreat() { //随机获取出生点 Transform parent = null; int pointIndex = Random.Range(1, BornList.Count); BornList.TryGetValue("0" + pointIndex.ToString(), out parent); if (parent != null) { //随机时间 int t = Random.Range(Const.RefreshCarminTime, Const.RefreshCarmaxTime); Debug.Log(t); while (true) { yield return new WaitForEndOfFrame(); timer += Time.deltaTime; if (timer >= t) { timer = 0; break; } } CreatCar(parent); } StartCoroutine(RandomTimeCreat()); } #endregion }