using DG.Tweening; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public enum CarTipType { 超速提示, 语音提示, } public class DriveManager : MonoBehaviour { public static DriveManager Instance = null; [Header("车牌信息")] public string CarID_Didian = ""; public string CarID_QuHao = ""; private string[] CarID_Nums= new string[5]; private char[] CarID_zm = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K' }; private char[] CarID_list = { 'A','B','C','D','E','F','G','H','J','K','1','2','3','4','5','6','7','8','9'}; public float _speed=15f; public float TurnTime = 0.25f; public GameObject[] carPrefabList; public Transform[] BornPointArr; public Transform[] WayPointArr; public Car eventCar; public CarTipType carTipType = CarTipType.超速提示; public List carList = new List(); public Transform ZoneCanvans; [Header("间隔时间")] public float craetTime = 2; [Header("设定的超速阈值")] public float OverSpeedMaxValue=120f; [Header("行驶高度补偿")] public float height = 0.25f; public float safeDistance=20; public void Init() { Instance = this; StartCoroutine(CraetCar()); } public IEnumerator CraetCar() { yield return new WaitForSeconds(craetTime); int carType = Random.Range(0, carPrefabList.Length); int bornNum = Random.Range(0, BornPointArr.Length); int wayNum = bornNum; GameObject go = carPrefabList[carType]; if (go != null) { GameObject car = ObjectPool.Instance.Spawn(go, BornPointArr[bornNum]); //获取Car脚本 Car carInfo = car.GetComponent(); carList.Add(carInfo); carInfo.DriveRoad = WayPointArr[wayNum]; //添加车牌号 string CarIDCard=""; CarIDCard += CarID_Didian; if (CarID_QuHao == "") { int ran=Random.Range(0, CarID_zm.Length-1); CarIDCard += CarID_zm[ran].ToString(); } for (int i = 0; i < CarID_Nums.Length; i++) { int ran = Random.Range(0, CarID_list.Length - 1); char value = CarID_list[ran]; CarIDCard += value; } carInfo.CarIDCard = CarIDCard; //初始化 carInfo.Init(); // StartCoroutine(TransMove(car.transform, WayPointArr[wayNum])); } StartCoroutine(CraetCar()); } public IEnumerator TransMove(Transform car, Transform pointArr) { if (car != null) { float speed = 0f; float t = 0f; Transform[] moveList = new Transform[pointArr.childCount]; for (int i = 0; i < pointArr.childCount; i++) { moveList[i] = pointArr.GetChild(i); Transform point = moveList[i]; //WayPoint wayPoint = point.GetComponent(); Vector3 Dir = ((point.position ) - car.transform.position).normalized; Vector3 lookDir = new Vector3(point.position.x, point.position.y, point.position.z) - car.transform.position; Quaternion rot = Quaternion.LookRotation(lookDir); //******循环判断离路径点距离******** while (true) { yield return new WaitForSeconds(0.02f); if (t < 1) { t += 0.002f; } speed = Mathf.Lerp(8f, _speed, t); float distance = Vector3.Distance(car.transform.position, point.position); car.transform.position += Dir * speed * Time.deltaTime; car.transform.DORotate(new Vector3(rot.eulerAngles.x, rot.eulerAngles.y, rot.eulerAngles.z), TurnTime, RotateMode.Fast); if (distance <= 1.5f) { break; } } } //回收车辆 Car carData = car.GetComponent(); carList.Remove(carData); // Destroy(car.gameObject); ObjectPool.Instance.Unspawn(car.gameObject); } } public void SetTimeScale(float scale) { Time.timeScale = scale; } //public void SetCarSoundTip(Car car) //{ // if (GameManagerForZhuanWan.Instance.isNormalRoadState) return; // GameObject go; // ZoneUI uI; // float time = Random.Range(2, 4); // switch (carTipType) // { // case CarTipType.超速提示: // go = GameObject.Instantiate(Resources.Load("超速提示"), ZoneCanvans); // uI = go.GetComponent(); // uI._camera = GameObject.FindGameObjectWithTag("MainViewCamera").GetComponent(); // uI.target = car.transform; // uI.offset.y = 5f; // uI.isScreen = false; // uI.enabled = true; // GameManagerForZhuanWan.Instance.StartTimeAction(time, () => { Destroy(go); }); // break; // case CarTipType.语音提示: // go = GameObject.Instantiate(Resources.Load("语音提示"), ZoneCanvans); // uI = go.GetComponent(); // uI._camera = GameObject.FindGameObjectWithTag("MainViewCamera").GetComponent(); // uI.target = car.transform; // uI.offset.y = 5f; // uI.isScreen = false; // uI.enabled = true; // GameManagerForZhuanWan.Instance.StartTimeAction(time, () => { Destroy(go); }); // break; // } //} public void SetEventCar(Car car) { if (eventCar != null) return; eventCar = car; } public void ClearEventCar() { eventCar = null; } }