static Vector3Int RoadCurPos(RoadDir prvDir, Vector3Int prvPrvPos, Vector3Int prvPos) { Vector3 t = prvPos.V3(); Quaternion r = Q.LookRot(t - new Vector3(prvPrvPos.x, prvPos.y, prvPrvPos.z)); Vector3 s = Vector3.one; Vector3 pos = new List <Vector3Int>() { new Vector3Int(-1, -1, 0), new Vector3Int(0, -1, 1), new Vector3Int(1, -1, 0), new Vector3Int(-1, 0, 0), new Vector3Int(0, 0, 1), new Vector3Int(1, 0, 0), new Vector3Int(-1, 1, 0), new Vector3Int(0, 1, 1), new Vector3Int(1, 1, 0), }[(int)prvDir].V3(); return(Vector3Int.RoundToInt(Matrix4x4.TRS(t, r, s).MultiplyPoint3x4(pos))); }
static void RoadAddDatas(RoadDir prvDir, Vector3Int prvPrvPos, Vector3Int prvPos, List <RoadDir> dirs, List <RoadData> datas, ref RoadDir refCurDir, ref Vector3Int refPrvPos, ref Vector3Int refCurPos) { List <Vector3Int> idxs = new List <Vector3Int>(); for (int i = 0; i < dirs.Count; i++) { refPrvPos = i == 0 ? prvPos : idxs[i - 1]; refCurDir = dirs[i]; refCurPos = RoadCurPos( i == 0 ? prvDir : dirs[i - 1], i == 0 ? prvPrvPos : i == 1 ? prvPos : idxs[i - 2], refPrvPos ); idxs.Add(refCurPos); datas.Add(new RoadData(refCurDir, refCurPos, RoadCurRot(refPrvPos, refCurPos))); } }
static bool RoadIsOverlap(RoadDir prvDir, Vector3Int prvPrvPos, Vector3Int prvPos, List <RoadDir> dirs, List <RoadData> datas) { List <Vector3Int> posLis = new List <Vector3Int>(); for (int i = 0; i < dirs.Count; i++) { posLis.Add(RoadCurPos( i == 0 ? prvDir : dirs[i - 1], i == 0 ? prvPrvPos : i == 1 ? prvPos : posLis[i - 2], i == 0 ? prvPos : posLis[i - 1] )); } for (int i = 0; i < datas.Count; i++) { for (int j = 0; j < posLis.Count; j++) { // if (datas[i].pos == posLis[j] - Vector3Int.up * 2 || // datas[i].pos == posLis[j] - Vector3Int.up || // datas[i].pos == posLis[j] || // datas[i].pos == posLis[j] + Vector3Int.up || // datas[i].pos == posLis[j] + Vector3Int.up * 2) { // return true; // } if (datas[i].pos == posLis[j] || (((int)dirs[j] / 3 == 2) && ( // ^ (datas[i].pos == posLis[j] + Vector3Int.up) || // ^* (datas[i].pos == posLis[j] + Vector3Int.up * 2 && (int)datas[i].dir / 3 == 0) // ^^v )) || (((int)dirs[j] / 3 == 1) && ( // - (datas[i].pos == posLis[j] + Vector3Int.up && (int)datas[i].dir / 3 == 0) || // ^v (datas[i].pos == posLis[j] + Vector3Int.down && (int)datas[i].dir / 3 == 2) // v^ )) || (((int)dirs[j] / 3 == 0) && ( // v (datas[i].pos == posLis[j] + Vector3Int.down) || // v* (datas[i].pos == posLis[j] + Vector3Int.down * 2 && (int)datas[i].dir / 3 == 2) // vv^ )) ) { return(true); } } } return(false); }
static void RoadCreateData(RoadDir prvDir, RoadDir curDir, Vector3Int prvPrvPos, Vector3Int prvPos, int iLen, int len, List <RoadData> datas, ref bool isDone) { if (isDone) { return; } if (iLen >= len) { isDone = true; return; } Vector3Int curPos = RoadCurPos(prvDir, prvPrvPos, prvPos); if (RoadIsOverlap(prvDir, prvPrvPos, prvPos, new List <RoadDir>() { curDir, RoadDir.F, RoadDir.F }, datas)) { return; } Quaternion curRot = RoadCurRot(prvPos, curPos); datas.Add(new RoadData(curDir, curPos, curRot)); List <RoadDir> rndDirs = new List <RoadDir> { RoadDir.LD, RoadDir.FD, RoadDir.RD, RoadDir.L, RoadDir.F, RoadDir.R, RoadDir.LU, RoadDir.FU, RoadDir.RU }; Shuffle <RoadDir>(ref rndDirs); for (int i = 0; i < rndDirs.Count; i++) { RoadCreateData(curDir, rndDirs[i], prvPos, curPos, iLen + 1, len, datas, ref isDone); } if (isDone) { return; } datas.RemoveAt(datas.Count - 1); }
public RoadData(RoadDir dir, Vector3Int pos, Quaternion rot) { this.dir = dir; this.pos = pos; this.rot = rot; }
public void AddCrossRoad(int numRoad, Rect rc, Vector3 center, Vector3 startPos, Vector3 endPos, int cdVal, RoadDir roadDir, float carSpeedMin, float carSpeedMax) { //一条路的资源被分成几份 //1条道的路分成2份 //2条道的路分成4份 //3条道的路分成6份 Vector3 offset = Vector3.forward * rc.height / (numRoad * 2); for (int i = 0; i < numRoad; i++) { //方向随机 float p = Random.Range(0.1f, 1.0f); var _roadDir = roadDir; var _startPos = startPos; var _endPos = endPos; if (p > 0.5f) { _roadDir = RoadDir.RightToLeft; Vector3 tmp = _startPos; _startPos = _endPos; _endPos = tmp; } //路的最南面起点 Vector3 startbottom = _startPos; startbottom += Vector3.back * rc.height * 0.5f; //GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = startbottom; Vector3 endbottom = _endPos; endbottom += Vector3.back * rc.height * 0.5f; //GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = endbottom; // i 第几条道 需要加的offset 公式=i * 2 + 1 // 0 1 1 = 0 * 2 + 1 // 1 2 3 = 1 * 2 + 1 // 2 3 5 = 2 * 2 + 1 int offsetMul = i * 2 + 1; var calcStart = startbottom + offset * offsetMul; float screenClampX = CrossRoadStageManager.GetInstance().carStartOffsetX; //if (roadDir == RoadDir.LeftToRight) //{ // screenClampX = GetXInLineByY(calcStart.z, // Math_F.Vector3_2D(CrossRoadModelManager.GetInstance().spLBW), // Math_F.Vector3_2D(CrossRoadModelManager.GetInstance().spLTW)); // calcStart.x = Mathf.Max(calcStart.x, screenClampX); //} //else //{ // screenClampX = GetXInLineByY(calcStart.z, // Math_F.Vector3_2D(CrossRoadModelManager.GetInstance().spRBW), // Math_F.Vector3_2D(CrossRoadModelManager.GetInstance().spRTW)); // calcStart.x = Mathf.Min(calcStart.x, screenClampX); //} if (_roadDir == RoadDir.LeftToRight) { calcStart.x = center.x + (-1 * screenClampX); } else { calcStart.x = center.x + (1 * screenClampX); } #if UNITY_EDITOR var goStart = GameObject.CreatePrimitive(PrimitiveType.Cube); goStart.transform.position = calcStart; goStart.name = "CalcStart-" + calcStart.ToString() + " " + _roadDir; #endif var calcEnd = endbottom + offset * offsetMul; #if UNITY_EDITOR var goEnd = GameObject.CreatePrimitive(PrimitiveType.Cube); goEnd.transform.position = calcEnd; goEnd.name = "CalcEnd-" + calcEnd.ToString() + " " + _roadDir; #endif var crossRoad = new CrossRoad(); crossRoad.startPos = calcStart; crossRoad.endPos = calcEnd; crossRoad.spawnCarCDVal = cdVal; crossRoad.minCarSpeed = carSpeedMin; crossRoad.maxCarSpeed = carSpeedMax; crossRoad.roadDir = _roadDir; crossRoadList.Add(crossRoad); } crossRoadRectArea.Add(rc); }
void CreateRoad2x2(int lowerLeftCellX, int lowerLeftCellZ, RoadDir dir) { //2x2 の セルの表現 //int ur;//右上 int rx = lowerLeftCellX + 1; //右 int lx = lowerLeftCellX; //左 int lz = lowerLeftCellZ; //下 int uz = lowerLeftCellZ + 1; //上 //横 if (dir == RoadDir.Z) { //プラス while (true) { if (cellTypes[lz, lx] == CityCellType.Road) { lz += 2; uz = lz + 1; continue; } float x = (float)(rx + lx) / 2; float z = (float)(lz + uz) / 2; var position = (new Vector3(x, 0, z) + roadOffset) * CityGenerator.DefaultDeltaDistance; SetCityCellTypes(rx, lx, lz, uz); var rot = Quaternion.identity; // GameObject obj = null; if (lz % (deltaHeight + 2) == 0) { obj = Instantiate(road2x2Cross, position, transform.rotation); SetRoadTypes2x2(lx, lz, RoadType.Crossroad); } else { obj = Instantiate(road2x2Staright, position, transform.rotation); SetRoadTypes2x2(lx, lz, RoadType.Straight); } //obj.transform.Rotate(new Vector3(0, 90, 0), Space.World); obj.transform.SetParent(roadParentGameObject.transform); //lX = (lX + 2 + deltaWidth); lz += 2; uz = lz + 1; if (uz >= cellTypes.GetLength(0)) { break; } } rx = lowerLeftCellX + 1; //右 lx = lowerLeftCellX; //左 lz = lowerLeftCellZ; //下 uz = lowerLeftCellZ + 1; //上 //マイナス while (lz >= 0) { if (cellTypes[lz, lx] == CityCellType.Road) { lz -= 2; uz = lz + 1; continue; } float x = (float)(rx + lx) / 2; float z = (float)(lz + uz) / 2; var position = (new Vector3(x, 0, z) + roadOffset) * CityGenerator.DefaultDeltaDistance; SetCityCellTypes(rx, lx, lz, uz); var rot = Quaternion.identity; GameObject obj = null; if (lz % (deltaHeight + 2) == 0) { obj = Instantiate(road2x2Cross, position, transform.rotation); SetRoadTypes2x2(lx, lz, RoadType.Crossroad); } else { obj = Instantiate(road2x2Staright, position, transform.rotation); SetRoadTypes2x2(lx, lz, RoadType.Straight); } //obj.transform.Rotate(new Vector3(0, 90, 0), Space.World); obj.transform.SetParent(roadParentGameObject.transform); //lX = (lX + 2 + deltaWidth); lz -= 2; uz = lz + 1; } } //縦 else { //プラス while (true) { if (cellTypes[lz, lx] == CityCellType.Road) { lx += 2; rx = lx + 1; continue; } float x = (float)(rx + lx) / 2; float z = (float)(lz + uz) / 2; var position = (new Vector3(x, 0, z) + roadOffset) * CityGenerator.DefaultDeltaDistance; SetCityCellTypes(rx, lx, lz, uz); var rot = Quaternion.identity; var obj = Instantiate(road2x2Staright, position, transform.rotation); SetRoadTypes2x2(lx, lz, RoadType.Straight); obj.transform.Rotate(new Vector3(0, 90, 0), Space.World); obj.transform.SetParent(roadParentGameObject.transform); //lX = (lX + 2 + deltaWidth); lx += 2; rx = lx + 1; if (rx >= cellTypes.GetLength(1)) { break; } } rx = lowerLeftCellX + 1; //右 lx = lowerLeftCellX; //左 lz = lowerLeftCellZ; //下 uz = lowerLeftCellZ + 1; //上 //マイナス while (lx >= 0) { if (cellTypes[lz, lx] == CityCellType.Road) { lx -= 2; rx = lx + 1; continue; } float x = (float)(rx + lx) / 2; float z = (float)(lz + uz) / 2; var position = (new Vector3(x, 0, z) + roadOffset) * CityGenerator.DefaultDeltaDistance; SetCityCellTypes(rx, lx, lz, uz); var rot = Quaternion.identity; var obj = Instantiate(road2x2Staright, position, transform.rotation); SetRoadTypes2x2(lx, lz, RoadType.Straight); //obj.transform.Rotate(new Vector3(0, 90, 0), Space.World); obj.transform.SetParent(roadParentGameObject.transform); //lX = (lX + 2 + deltaWidth); lx -= 2; rx = lx + 1; } } }