using System.Collections; using System.Collections.Generic; using UnityEngine; namespace BuildSystem { public enum Dir { none,up,down,left,right } public class Car : MonoBehaviour { private Vector2Int now; public Transform fordir; public bool isCanMove = true; public bool isMoveEnd = true; public Queue passList = new Queue(); public Queue moveRoad = new Queue();//移动路径 bool isStart = false; bool isFindEnd = true; public Grid gr; float speed; public RaycastHit raycastHit; public float stupT = 0; /// /// 打开车辆的自动运动 /// /// public void OpenMove(Vector2Int pos,float speed=10) { this.speed = speed; isStart = true; now = pos; transform.position = BuildSystemManager.Instance.gridMap[now.x][now.y].pos; StartCoroutine(RandomFindRoad(BuildSystemManager.Instance.gridMap[now.x][now.y])); StartCoroutine(StartMove()); } private void Update() { if (!isStart) return; if (moveRoad.Count < 3 && isFindEnd) { StartCoroutine(RandomFindRoad(moveRoad.ToArray()[moveRoad.Count - 1])); } if (passList.Count > 3) { passList.Dequeue(); } Ray ray = new Ray(transform.position + Vector3.up, fordir.transform.position - transform.position); //Debug.DrawRay(transform.position + Vector3.up, fordir.transform.position - transform.position, Color.blue); int mask = 1 << 11; if (Physics.Raycast(ray, out raycastHit,15,mask)) { if (raycastHit.collider.tag == "Car") { isCanMove = false; stupT += Time.deltaTime; } else { isCanMove = true; } } else { isCanMove = true; } if (stupT > 30) { CarController.Instance.number--; Destroy(gameObject); } } /// /// 随机寻找下一个路径 /// /// /// IEnumerator RandomFindRoad(Grid grid) { isFindEnd = false; passList.Enqueue(grid); while (true) { yield return new WaitForEndOfFrame(); int rand = Random.Range(0, BuildSystemManager.Instance.gridMap[grid.x][grid.z].ground.Count); gr = BuildSystemManager.Instance.gridMap[grid.x][grid.z].ground[rand]; if (!passList.Contains(gr)) { break; } } moveRoad.Enqueue(gr); isFindEnd = true; yield return null; } /// /// 开启车辆移动 /// /// IEnumerator StartMove() { yield return new WaitUntil(()=> { return moveRoad.Count >= 3; }); while (moveRoad.Count != 0) { yield return new WaitUntil(() => { return isMoveEnd;}); isMoveEnd = false; Grid grid = moveRoad.Dequeue(); StartCoroutine(Move(grid)); } } /// /// 车辆移动到目的点 /// /// 下一个点的目标 /// IEnumerator Move(Grid nextGird) { Grid nextnextGrid = moveRoad.Peek(); List roadPos = nextGird.placeObject.GetComponent().GetRoad(BuildSystemManager.Instance.gridMap[now.x][now.y], nextGird, nextnextGrid); isMoveEnd = false; for (int i = 0; i < roadPos.Count;) { while (true) { yield return new WaitUntil(() => { return isCanMove; }); yield return new WaitForEndOfFrame(); Vector3 dir = (roadPos[i].position - transform.position).normalized; transform.position += dir * Time.deltaTime * speed; Quaternion quaternion = Quaternion.LookRotation(roadPos[i].position - transform.position); transform.rotation = quaternion; if (Vector3.Distance(transform.position, roadPos[i].position) <= 0.5f) { i++; break; } } } now = new Vector2Int(nextGird.x, nextGird.z); isMoveEnd = true; } } }