public void LCorridorGeneration() { int i = 0; foreach (RoomConnectionLib rcl in roomPossibleConnections) { //Create the letter L corridor rcl.corridorDir = CorridorDir.L; if (rcl.lConnections.Count != 0) { int choosenIndex = Random.Range(0, rcl.lConnections.Count); RoomConnectionLib.LConnection lcon = rcl.lConnections[choosenIndex]; Door dr1 = new Door(); Door dr2 = new Door(); CalcDoorLocation(ref dr1, ref dr2, lcon.l1, lcon.l2); dr1.corridor = new Rect(lcon.l1.v1, (lcon.l1.v1.x == lcon.l1.v2.x ? new Vector2(corridorWidth, lcon.l1.v2.y - lcon.l1.v1.y) : new Vector2(lcon.l1.v2.x - lcon.l1.v1.x, corridorWidth))); dr2.corridor = new Rect(lcon.l2.v1, (lcon.l2.v1.x == lcon.l2.v2.x ? new Vector2(corridorWidth, lcon.l2.v2.y - lcon.l2.v1.y) : new Vector2(lcon.l2.v2.x - lcon.l2.v1.x, corridorWidth))); //Fix the minor bug // |__ // will result colliding within corridor if (lcon.l1.v1.y > lcon.l1.v2.y && lcon.l2.v1.x > lcon.l2.v2.x) { Rect r = dr1.corridor; r.height += corridorWidth; dr1.corridor = r; } else if (lcon.l2.v1.y > lcon.l2.v2.y && lcon.l1.v1.x > lcon.l1.v2.x) { Rect r = dr2.corridor; r.height += corridorWidth; dr2.corridor = r; } // __ // | // will result no connection if (lcon.l1.v1.x < lcon.l1.v2.x && lcon.l2.v1.y < lcon.l2.v2.y) { Rect r = dr1.corridor; r.width += corridorWidth; dr1.corridor = r; } else if (lcon.l2.v1.x < lcon.l2.v2.x && lcon.l1.v1.y < lcon.l1.v2.y) { Rect r = dr2.corridor; r.width += corridorWidth; dr2.corridor = r; } dr1.corridorDir = rcl.corridorDir; dr2.corridorDir = rcl.corridorDir; rcl.r1.doorPositions.Add(dr1); rcl.r2.doorPositions.Add(dr2); /* * if (corridors.Count != roomPossibleConnections.Count) * corridors.Add(corridor); * else * corridors[i] = corridor; */ } i++; } }
/// <summary> /// /// </summary> /// <param name="r1">Room 1</param> /// <param name="r2">Room 2</param> /// <param name="width">Max Line Width</param> /// <param name="height">Max Line Height</param> /// <param name="_rooms"></param> /// <param name="rcl"></param> void CreateLConnectionLib(Room r1, Room r2, float width, float height, List <Room> _rooms, ref RoomConnectionLib rcl) { int startIndex = rcl.lConnections.Count; List <int> sizeEachColumn = new List <int>(); //loop vertical (height from room 1) for (int y = 0; y < r1.rect.height; y++) { Vector2 start = new Vector2(r1.rect.x, y + r1.rect.y + 0.1f); if (width > 0) { start.x = r1.rect.x + r1.rect.width - 0.1f; } Vector2 end = new Vector2(start.x + width + 0.5f * Mathf.Sign(width), start.y); RoomConnectionLib.Line l1; if (IsLineCollideWithRoomsOrCorridor(start, end, out l1, _rooms, r1, false, corridorWidth)) { //Make it imposible to do corridor if (l1.Length < corridorWidth) { l1.v2 = l1.v1; } //if length less than 0.3f, just let it go if (l1.Length < 0.3f) { continue; } } else { l1 = new RoomConnectionLib.Line(start, end); } l1.direction = (end - start).normalized; int w = 0; //loop horizontal (width from room 2) //find connection for (int x = 0; x < r2.rect.width; x++) { start.x = r2.rect.x + x + 0.1f; start.y = r2.rect.y + 0.1f; if (height > 0) { start.y = r2.rect.y + r2.rect.height - 0.1f; } end.x = start.x; end.y = start.y + height + 0.5f * Mathf.Sign(height); RoomConnectionLib.Line l2; if (IsLineCollideWithRoomsOrCorridor(start, end, out l2, _rooms, r2, false, corridorWidth)) { //Make it imposible to do corridor if (l2.Length < corridorWidth) { l2.v2 = l2.v1; } //if length less than 0.3f, just let it go if (l2.Length < 0.3f) { continue; } } else { l2 = new RoomConnectionLib.Line(start, end); } l2.direction = (end - start).normalized; //if ((r1.id == 7 || r1.id == 26) && (r2.id == 7 || r2.id == 26)) //{ // debugLine.Add(l1); // //debugLine.Add(l2); //} //if 2 line intersection, then this will become corridor Vector2 itsc; if (LineSegmentsIntersection.Math2d.LineSegmentsIntersection(l2.v1, l2.v2, l1.v1, l1.v2, out itsc)) { //ignore first connection, it will result good connection if (w != 0) { RoomConnectionLib.LConnection lCon = new RoomConnectionLib.LConnection(); l1.v1.x = Mathf.Round(l1.v1.x); l1.v1.y = Mathf.Round(l1.v1.y); l2.v1.x = Mathf.Round(l2.v1.x); l2.v1.y = Mathf.Round(l2.v1.y); itsc.x = Mathf.Round(itsc.x); itsc.y = Mathf.Round(itsc.y); lCon.l1 = new RoomConnectionLib.Line(l1.v1, itsc); lCon.l2 = new RoomConnectionLib.Line(l2.v1, itsc); lCon.l1.direction = l1.direction; lCon.l2.direction = l2.direction; rcl.lConnections.Add(lCon); } w++; } } //remove the last possible connection(width) for (int i = 0; i < corridorWidth; i++) { if (rcl.lConnections.Count == 0 || w <= 1) { break; } rcl.lConnections.RemoveAt(rcl.lConnections.Count - 1); w--; } if (w != 0) { sizeEachColumn.Add(w - 1); } } //remove the last possible connection(height) for (int i = 0; i < corridorWidth; i++) { if (sizeEachColumn.Count - 1 - i < 0) { break; } int del = sizeEachColumn[sizeEachColumn.Count - 1 - i]; for (int j = 0; j < del; j++) { if (rcl.lConnections.Count == 0) { break; } rcl.lConnections.RemoveAt(rcl.lConnections.Count - 1); } } //remove the first row, this will be resulting good connection if (sizeEachColumn.Count > 0) { for (int i = 0; i < sizeEachColumn[0]; i++) { if (rcl.lConnections.Count <= startIndex) { break; } rcl.lConnections.RemoveAt(startIndex); } } }