You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
154 lines
5.0 KiB
154 lines
5.0 KiB
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<Grid> passList = new Queue<Grid>();
|
|
public Queue<Grid> moveRoad = new Queue<Grid>();//移动路径
|
|
|
|
bool isStart = false;
|
|
bool isFindEnd = true;
|
|
public Grid gr;
|
|
float speed;
|
|
public RaycastHit raycastHit;
|
|
|
|
public float stupT = 0;
|
|
|
|
|
|
/// <summary>
|
|
/// 打开车辆的自动运动
|
|
/// </summary>
|
|
/// <param name="pos"></param>
|
|
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;
|
|
stupT = 0;
|
|
}
|
|
|
|
if (stupT > 30) {
|
|
CarController.Instance.carNumber--;
|
|
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 随机寻找下一个路径
|
|
/// </summary>
|
|
/// <param name="grid"></param>
|
|
///
|
|
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;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 开启车辆移动
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 车辆移动到目的点
|
|
/// </summary>
|
|
/// <param name="nextGird">下一个点的目标</param>
|
|
/// <returns></returns>
|
|
IEnumerator Move(Grid nextGird) {
|
|
Debug.Log(moveRoad);
|
|
Debug.Log(now);
|
|
Grid nextnextGrid = moveRoad.Peek();
|
|
List<Transform> roadPos = nextGird.placeObject.GetComponent<RoadInfo>().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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|