using System.Collections; using System.Collections.Generic; using UnityEngine; namespace BuildSystem { public class RoadInfo : MonoBehaviour { public List enterUp1; public List enterUp2; public List enterUp3; public List enterDown1; public List enterDown2; public List enterDown3; public List enterLeft1; public List enterLeft2; public List enterLeft3; public List enterRight1; public List enterRight2; public List enterRight3; [Header("出")] public List outUp1; public List outUp2; public List outUp3; public List outDown1; public List outDown2; public List outDown3; public List outLeft1; public List outLeft2; public List outLeft3; public List outRight1; public List outRight2; public List outRight3; [Header("中转点")] public Transform upPos; public Transform downPos; public Transform leftPos; public Transform rightPos; /// /// 根据车道和移动方向获取移动点位 /// /// 车道 /// 进入的方向 /// 出去的方向 /// public List 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 road = new List(); 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; } /// /// 获取移动方向 /// /// 初始坐标 /// 下一个目标点的坐标 /// 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; } /// /// 根据进出方向获取车道 /// /// /// /// /// 第一个参数为进入的车道,第二个为出的车道 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; } } }