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.
421 lines
15 KiB
421 lines
15 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
namespace BuildSystem
|
|
{
|
|
public class CarData{
|
|
public List<Transform> transforms;
|
|
public int id;
|
|
public Dir dir;
|
|
|
|
public CarData(List<Transform> transforms, int id, Dir dir) {
|
|
this.transforms = transforms;
|
|
this.id = id;
|
|
this.dir = dir;
|
|
if (dir == Dir.up) {
|
|
this.dir = Dir.down;
|
|
}
|
|
else if(dir == Dir.down) {
|
|
this.dir = Dir.up;
|
|
}
|
|
else if (dir == Dir.left)
|
|
{
|
|
this.dir = Dir.right;
|
|
}
|
|
else if (dir == Dir.right)
|
|
{
|
|
this.dir = Dir.left;
|
|
}
|
|
}
|
|
}
|
|
public class RoadInfo : MonoBehaviour
|
|
{
|
|
public List<Transform> enterUp1;
|
|
public List<Transform> enterUp2;
|
|
public List<Transform> enterUp3;
|
|
|
|
public List<Transform> enterDown1;
|
|
public List<Transform> enterDown2;
|
|
public List<Transform> enterDown3;
|
|
|
|
public List<Transform> enterLeft1;
|
|
public List<Transform> enterLeft2;
|
|
public List<Transform> enterLeft3;
|
|
|
|
public List<Transform> enterRight1;
|
|
public List<Transform> enterRight2;
|
|
public List<Transform> enterRight3;
|
|
|
|
[Header("出")]
|
|
public List<Transform> outUp1;
|
|
public List<Transform> outUp2;
|
|
public List<Transform> outUp3;
|
|
|
|
public List<Transform> outDown1;
|
|
public List<Transform> outDown2;
|
|
public List<Transform> outDown3;
|
|
|
|
public List<Transform> outLeft1;
|
|
public List<Transform> outLeft2;
|
|
public List<Transform> outLeft3;
|
|
|
|
public List<Transform> outRight1;
|
|
public List<Transform> outRight2;
|
|
public List<Transform> outRight3;
|
|
|
|
[Header("中转点")]
|
|
public Transform upPos;
|
|
public Transform downPos;
|
|
public Transform leftPos;
|
|
public Transform rightPos;
|
|
|
|
|
|
/// <summary>
|
|
/// 根据给出的路径获取进入方向,出去方向和车道
|
|
/// </summary>
|
|
/// <param name="nowGrid">当前位于的格子</param>
|
|
/// <param name="nextGrid">下一个格子</param>
|
|
/// <param name="nextnextGrid">下下个格子</param>
|
|
/// <returns></returns>
|
|
public List<Transform> GetRoad(Grid nowGrid, Grid nextGrid, Grid nextnextGrid,ref int enterID,ref int outID)
|
|
{
|
|
Dir enterDir = GetDir(new Vector2Int(nowGrid.x, nowGrid.z), new Vector2Int(nextGrid.x, nextGrid.z));
|
|
Dir outDir = GetDir(new Vector2Int(nextGrid.x, nextGrid.z), new Vector2Int(nextnextGrid.x, nextnextGrid.z));
|
|
int[] rayRoad = new int[2] { enterID , outID };
|
|
List<Transform> road = new List<Transform>();
|
|
//根据进入方向给出移动点位
|
|
if (enterDir == Dir.up) {
|
|
if (rayRoad[0] == 0) {
|
|
foreach (Transform i in enterUp1)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
} else if (rayRoad[0] == 1) {
|
|
foreach (Transform i in enterUp2)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[0] == 2)
|
|
{
|
|
foreach (Transform i in enterUp3)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
if (upPos) {
|
|
road.Add(upPos);
|
|
}
|
|
|
|
}else if (enterDir == Dir.down)
|
|
{
|
|
if (rayRoad[0] == 0)
|
|
{
|
|
foreach (Transform i in enterDown1)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[0] == 1)
|
|
{
|
|
foreach (Transform i in enterDown2)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[0] == 2)
|
|
{
|
|
foreach (Transform i in enterDown3)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
if (downPos)
|
|
{
|
|
road.Add(downPos);
|
|
}
|
|
|
|
}
|
|
else if (enterDir == Dir.left)
|
|
{
|
|
if (rayRoad[0] == 0)
|
|
{
|
|
foreach (Transform i in enterLeft1)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[0] == 1)
|
|
{
|
|
foreach (Transform i in enterLeft2)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[0] == 2)
|
|
{
|
|
foreach (Transform i in enterLeft3)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
if (leftPos)
|
|
{
|
|
road.Add(leftPos);
|
|
}
|
|
}
|
|
else if (enterDir == Dir.right)
|
|
{
|
|
if (rayRoad[0] == 0)
|
|
{
|
|
foreach (Transform i in enterRight1)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[0] == 1)
|
|
{
|
|
foreach (Transform i in enterRight2)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[0] == 2)
|
|
{
|
|
foreach (Transform i in enterRight3)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
if (rightPos)
|
|
{
|
|
road.Add(rightPos);
|
|
}
|
|
}
|
|
|
|
|
|
//根据出去方向给出移动点位
|
|
if (outDir == Dir.up)
|
|
{
|
|
if (rayRoad[1] == 0)
|
|
{
|
|
foreach (Transform i in outUp1)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[1] == 1)
|
|
{
|
|
foreach (Transform i in outUp2)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[1] == 2)
|
|
{
|
|
foreach (Transform i in outUp3)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
}
|
|
else if (outDir == Dir.down)
|
|
{
|
|
if (rayRoad[1] == 0)
|
|
{
|
|
foreach (Transform i in outDown1)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[1] == 1)
|
|
{
|
|
foreach (Transform i in outDown2)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[1] == 2)
|
|
{
|
|
foreach (Transform i in outDown3)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
}
|
|
else if (outDir == Dir.left)
|
|
{
|
|
if (rayRoad[1] == 0)
|
|
{
|
|
foreach (Transform i in outLeft1)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[1] == 1)
|
|
{
|
|
foreach (Transform i in outLeft2)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[1] == 2)
|
|
{
|
|
foreach (Transform i in outLeft3)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
}
|
|
else if (outDir == Dir.right)
|
|
{
|
|
if (rayRoad[1] == 0)
|
|
{
|
|
foreach (Transform i in outRight1)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[1] == 1)
|
|
{
|
|
foreach (Transform i in outRight2)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
else if (rayRoad[1] == 2)
|
|
{
|
|
foreach (Transform i in outRight3)
|
|
{
|
|
road.Add(i);
|
|
}
|
|
}
|
|
}
|
|
enterID = -1;
|
|
outID = -1;
|
|
return road;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取移动方向
|
|
/// </summary>
|
|
/// <param name="a">初始坐标</param>
|
|
/// <param name="b">下一个目标点的坐标</param>
|
|
/// <returns></returns>
|
|
public Dir GetDir(Vector2Int a, Vector2Int b)
|
|
{
|
|
if (a.x - b.x != 0)
|
|
{
|
|
return a.x - b.x > 0 ? Dir.left : Dir.right;
|
|
}
|
|
if (a.y - b.y != 0)
|
|
{
|
|
return a.y - b.y > 0 ? Dir.down : Dir.up;
|
|
}
|
|
return Dir.none;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据进出方向获取车道
|
|
/// </summary>
|
|
/// <param name="grid"></param>
|
|
/// <param name="enterDir"></param>
|
|
/// <param name="outDir"></param>
|
|
/// <returns>第一个参数为进入的车道,第二个为出的车道</returns>
|
|
public int[] GetRayRoad(Grid grid, Dir enterDir, Dir outDir) {
|
|
int[] rayRoad = new int[2];
|
|
//随机进入车道车道
|
|
if (enterDir == Dir.up)
|
|
{
|
|
if (grid.type[2] == RoadNumberType.Two)
|
|
{
|
|
rayRoad[0] = Random.Range(0, 2);
|
|
}
|
|
else if (grid.type[2] == RoadNumberType.Three)
|
|
{
|
|
rayRoad[0] = Random.Range(0, 3);
|
|
}
|
|
}else if (enterDir == Dir.down)
|
|
{
|
|
if (grid.type[0] == RoadNumberType.Two)
|
|
{
|
|
rayRoad[0] = Random.Range(0, 2);
|
|
}
|
|
else if (grid.type[0] == RoadNumberType.Three)
|
|
{
|
|
rayRoad[0] = Random.Range(0, 3);
|
|
}
|
|
}
|
|
else if (enterDir == Dir.left)
|
|
{
|
|
if (grid.type[1] == RoadNumberType.Two)
|
|
{
|
|
rayRoad[0] = Random.Range(0, 2);
|
|
}
|
|
else if (grid.type[1] == RoadNumberType.Three)
|
|
{
|
|
rayRoad[0] = Random.Range(0, 3);
|
|
}
|
|
}
|
|
else if (enterDir == Dir.right)
|
|
{
|
|
if (grid.type[3] == RoadNumberType.Two)
|
|
{
|
|
rayRoad[0] = Random.Range(0, 2);
|
|
}
|
|
else if (grid.type[3] == RoadNumberType.Three)
|
|
{
|
|
rayRoad[0] = Random.Range(0, 3);
|
|
}
|
|
}
|
|
//随机出去车道
|
|
if (outDir == Dir.up)
|
|
{
|
|
if (grid.type[0] == RoadNumberType.Two)
|
|
{
|
|
rayRoad[1] = Random.Range(0, 2);
|
|
}
|
|
else if (grid.type[0] == RoadNumberType.Three)
|
|
{
|
|
rayRoad[1] = Random.Range(0, 3);
|
|
}
|
|
}
|
|
else if (outDir == Dir.down)
|
|
{
|
|
if (grid.type[2] == RoadNumberType.Two)
|
|
{
|
|
rayRoad[1] = Random.Range(0, 2);
|
|
}
|
|
else if (grid.type[2] == RoadNumberType.Three)
|
|
{
|
|
rayRoad[1] = Random.Range(0, 3);
|
|
}
|
|
}
|
|
else if (outDir == Dir.left)
|
|
{
|
|
if (grid.type[3] == RoadNumberType.Two)
|
|
{
|
|
rayRoad[1] = Random.Range(0, 2);
|
|
}
|
|
else if (grid.type[3] == RoadNumberType.Three)
|
|
{
|
|
rayRoad[1] = Random.Range(0, 3);
|
|
}
|
|
}
|
|
else if (outDir == Dir.right)
|
|
{
|
|
if (grid.type[1] == RoadNumberType.Two)
|
|
{
|
|
rayRoad[1] = Random.Range(0, 2);
|
|
}
|
|
else if (grid.type[1] == RoadNumberType.Three)
|
|
{
|
|
rayRoad[1] = Random.Range(0, 3);
|
|
}
|
|
}
|
|
return rayRoad;
|
|
}
|
|
}
|
|
}
|