using System.Collections; using System.Collections.Generic; using UnityEngine; namespace BuildSystem { public class CarData{ public List transforms; public int id; public Dir dir; public CarData(List 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 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,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 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); } } } enterID = -1; outID = -1; 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] == 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; } } }