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.

394 lines
14 KiB

1 year ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BuildSystem
{
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="roadWay">车道</param>
/// <param name="enterDir">进入的方向</param>
/// <param name="outDir">出去的方向</param>
/// <returns></returns>
public List<Transform> GetRoad(Grid nowGrid, Grid nextGrid, Grid nextnextGrid)
{
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 = GetRayRoad(nextGrid, enterDir, outDir);
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);
}
}
}
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] == PlaceObjectType.Two)
{
rayRoad[0] = Random.Range(0, 2);
}
else if (grid.type[2] == PlaceObjectType.Three)
{
rayRoad[0] = Random.Range(0, 3);
}
}else if (enterDir == Dir.down)
{
if (grid.type[0] == PlaceObjectType.Two)
{
rayRoad[0] = Random.Range(0, 2);
}
else if (grid.type[0] == PlaceObjectType.Three)
{
rayRoad[0] = Random.Range(0, 3);
}
}
else if (enterDir == Dir.left)
{
if (grid.type[1] == PlaceObjectType.Two)
{
rayRoad[0] = Random.Range(0, 2);
}
else if (grid.type[1] == PlaceObjectType.Three)
{
rayRoad[0] = Random.Range(0, 3);
}
}
else if (enterDir == Dir.right)
{
if (grid.type[3] == PlaceObjectType.Two)
{
rayRoad[0] = Random.Range(0, 2);
}
else if (grid.type[3] == PlaceObjectType.Three)
{
rayRoad[0] = Random.Range(0, 3);
}
}
//随机出车道
if (outDir == Dir.up)
{
if (grid.type[0] == PlaceObjectType.Two)
{
rayRoad[1] = Random.Range(0, 2);
}
else if (grid.type[0] == PlaceObjectType.Three)
{
rayRoad[1] = Random.Range(0, 3);
}
}
else if (outDir == Dir.down)
{
if (grid.type[2] == PlaceObjectType.Two)
{
rayRoad[1] = Random.Range(0, 2);
}
else if (grid.type[2] == PlaceObjectType.Three)
{
rayRoad[1] = Random.Range(0, 3);
}
}
else if (outDir == Dir.left)
{
if (grid.type[3] == PlaceObjectType.Two)
{
rayRoad[1] = Random.Range(0, 2);
}
else if (grid.type[3] == PlaceObjectType.Three)
{
rayRoad[1] = Random.Range(0, 3);
}
}
else if (outDir == Dir.right)
{
if (grid.type[1] == PlaceObjectType.Two)
{
rayRoad[1] = Random.Range(0, 2);
}
else if (grid.type[1] == PlaceObjectType.Three)
{
rayRoad[1] = Random.Range(0, 3);
}
}
return rayRoad;
}
1 year ago
}
}