|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.AI;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 车辆管理类
|
|
|
|
|
/// 功能:车辆生成
|
|
|
|
|
/// 车辆AI
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class CarSys : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//计时器
|
|
|
|
|
private float timer = 0f;
|
|
|
|
|
//车辆种类列表
|
|
|
|
|
public GameObject[] carTypeList;
|
|
|
|
|
//已生成车辆列表
|
|
|
|
|
//public List<GameObject> carList=new List<GameObject>(200);
|
|
|
|
|
[HideInInspector]
|
|
|
|
|
public Dictionary<int, GameObject> carList = new Dictionary<int, GameObject>();
|
|
|
|
|
//
|
|
|
|
|
public int CarID = 0;
|
|
|
|
|
|
|
|
|
|
public bool canCreat = false;
|
|
|
|
|
|
|
|
|
|
//移动点列表
|
|
|
|
|
private Dictionary<string, Transform> MoveList = new Dictionary<string, Transform>();
|
|
|
|
|
//生成点列表
|
|
|
|
|
private Dictionary<string, Transform> BornList = new Dictionary<string, Transform>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
CheckBornoint();
|
|
|
|
|
CheckMovePoint();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 车辆AI
|
|
|
|
|
//检索汽车生成点
|
|
|
|
|
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<Drive>();
|
|
|
|
|
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 >= drive.checktime)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
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.SetCarOutText(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 汽车随机生成
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 生成汽车递归携程,在GameRoot中启用一次即可
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
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 WaitForSeconds(Time.deltaTime);
|
|
|
|
|
timer += Time.deltaTime;
|
|
|
|
|
|
|
|
|
|
if (timer >= t)
|
|
|
|
|
{
|
|
|
|
|
timer = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
float checkTime = t / 5;
|
|
|
|
|
CreatCar(parent,checkTime);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StartCoroutine(RandomTimeCreat());
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 汽车生成函数
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="parent">生成之后的父物体</param>
|
|
|
|
|
/// <param name="checktime">生成后开始行驶检测的时间</param>
|
|
|
|
|
public void CreatCar(Transform parent,float checktime)
|
|
|
|
|
{
|
|
|
|
|
// 随机创建车辆种类
|
|
|
|
|
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>();
|
|
|
|
|
drive.carId = CarID;
|
|
|
|
|
drive.checktime = checktime;
|
|
|
|
|
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.SetCarInText(1);//+1
|
|
|
|
|
//执行寻路AI
|
|
|
|
|
NavMeshAgent agent = car.GetComponent<NavMeshAgent>();
|
|
|
|
|
if (agent != null)
|
|
|
|
|
{
|
|
|
|
|
StartCoroutine(CarMove(parent.name, agent));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|