|
|
@ -21,7 +21,8 @@ public class CarSys : MonoBehaviour
|
|
|
|
public Dictionary<int, GameObject> carList = new Dictionary<int, GameObject>();
|
|
|
|
public Dictionary<int, GameObject> carList = new Dictionary<int, GameObject>();
|
|
|
|
//
|
|
|
|
//
|
|
|
|
public int CarID = 0;
|
|
|
|
public int CarID = 0;
|
|
|
|
private bool canCreat = true;
|
|
|
|
|
|
|
|
|
|
|
|
public bool canCreat = false;
|
|
|
|
|
|
|
|
|
|
|
|
//移动点列表
|
|
|
|
//移动点列表
|
|
|
|
private Dictionary<string, Transform> MoveList = new Dictionary<string, Transform>();
|
|
|
|
private Dictionary<string, Transform> MoveList = new Dictionary<string, Transform>();
|
|
|
@ -37,6 +38,7 @@ public class CarSys : MonoBehaviour
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region CarAI
|
|
|
|
//检索移动点
|
|
|
|
//检索移动点
|
|
|
|
public void CheckBornoint()
|
|
|
|
public void CheckBornoint()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -143,21 +145,51 @@ public class CarSys : MonoBehaviour
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerator RandomTimeCreatCar(float time,Transform parent)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//检测前方车辆
|
|
|
|
|
|
|
|
public bool CheckFrontCar(Drive drive)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
if (drive.FrontCar == null) return false;
|
|
|
|
|
|
|
|
|
|
|
|
if (timer < time) {
|
|
|
|
if (Vector3.Distance(drive.transform.position, drive.FrontCar.transform.position) < Const.SafeDistance)
|
|
|
|
canCreat = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
canCreat = true;
|
|
|
|
|
|
|
|
timer = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (canCreat == false) yield break;
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Debug.Log(time);
|
|
|
|
#region 汽车随机生成
|
|
|
|
yield return new WaitForSeconds(time);
|
|
|
|
|
|
|
|
|
|
|
|
public void CreatCar(Transform parent)
|
|
|
|
|
|
|
|
{
|
|
|
|
// 随机创建车辆种类
|
|
|
|
// 随机创建车辆种类
|
|
|
|
int carIndex = Random.Range(0, carTypeList.Length - 1);
|
|
|
|
int carIndex = Random.Range(0, carTypeList.Length - 1);
|
|
|
|
//实例化车辆
|
|
|
|
//实例化车辆
|
|
|
@ -183,69 +215,41 @@ public class CarSys : MonoBehaviour
|
|
|
|
{
|
|
|
|
{
|
|
|
|
StartCoroutine(CarMove(parent.name, agent));
|
|
|
|
StartCoroutine(CarMove(parent.name, agent));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int temp = 0;
|
|
|
|
public IEnumerator RandomTimeCreat()
|
|
|
|
//汽车生成函数
|
|
|
|
|
|
|
|
public void CreatCar()
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//计时
|
|
|
|
|
|
|
|
timer += Time.deltaTime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//随机获取出生点
|
|
|
|
//随机获取出生点
|
|
|
|
Transform parent = null;
|
|
|
|
Transform parent = null;
|
|
|
|
int pointIndex = Random.Range(1, BornList.Count);
|
|
|
|
int pointIndex = Random.Range(1, BornList.Count);
|
|
|
|
BornList.TryGetValue("0" + pointIndex.ToString(), out parent);
|
|
|
|
BornList.TryGetValue("0" + pointIndex.ToString(), out parent);
|
|
|
|
//创建车辆
|
|
|
|
|
|
|
|
if (parent != null)
|
|
|
|
if (parent != null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
//随机时间
|
|
|
|
int t = Random.Range(Const.RefreshCarminTime, Const.RefreshCarmaxTime);
|
|
|
|
int t = Random.Range(Const.RefreshCarminTime, Const.RefreshCarmaxTime);
|
|
|
|
|
|
|
|
Debug.Log(t);
|
|
|
|
if (t == temp) return;
|
|
|
|
while (true)
|
|
|
|
temp = t;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StartCoroutine(RandomTimeCreatCar(t,parent));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//检测前方车辆
|
|
|
|
|
|
|
|
public bool CheckFrontCar(Drive drive)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (drive.FrontCar==null) return false;
|
|
|
|
yield return new WaitForEndOfFrame();
|
|
|
|
|
|
|
|
timer += Time.deltaTime;
|
|
|
|
|
|
|
|
|
|
|
|
if (Vector3.Distance(drive.transform.position, drive.FrontCar.transform.position) < Const.SafeDistance)
|
|
|
|
if (timer >= t)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
timer = 0;
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
CreatCar(parent);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
StartCoroutine(RandomTimeCreat());
|
|
|
|
//return false;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|