public void StartGenerateLConnectionLib(List <Room> _rooms) { for (int i = 0; i < roomPossibleConnections.Count; i++) { RoomConnectionLib rcl = roomPossibleConnections[i]; StartGenerateLConnectionBetweenRoom(rcl.r1, rcl.r2, _rooms, ref rcl); } }
/// <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); } } }
void StartGenerateLConnectionBetweenRoom(Room r1, Room r2, List <Room> _rooms, ref RoomConnectionLib rcl) { //Condition if L Connection is the only options!! if (rcl.connections.Count == 0) { Vector2 r1Direction = Vector2.zero; Vector2 r2Direction = Vector2.zero; Vector2 lR1 = Vector2.zero; Vector2 lR2 = Vector2.zero; if (r1.rect.x < r2.rect.x) { r1Direction.x = 1; r2Direction.x = -1; lR1.x = Mathf.Abs((r2.rect.x + r2.rect.width) - (r1.rect.x + r1.rect.width)); lR2.x = Mathf.Abs(r2.rect.x - r1.rect.x); } else { if (r1.rect.x == r2.rect.x) #if DEBUG_MODE { Debug.LogWarning("Still no implementation for this shit! Open this warning to know what happen"); } #endif { r1Direction.x = -1; } r2Direction.x = 1; lR1.x = Mathf.Abs(r1.rect.x - r2.rect.x); lR2.x = Mathf.Abs((r1.rect.x + r1.rect.width) - (r2.rect.x + r2.rect.width)); } if (r1.rect.y < r2.rect.y) { r1Direction.y = 1; r2Direction.y = -1; lR1.y = Mathf.Abs((r2.rect.y + r2.rect.height) - (r1.rect.y + r1.rect.height)); lR2.y = Mathf.Abs(r2.rect.y - r1.rect.y); } else { if (r1.rect.y == r2.rect.y) #if DEBUG_MODE { Debug.LogWarning("Still no implementation for this shit! Open this warning to know what happen"); } #endif { r1Direction.y = -1; } r2Direction.y = 1; lR1.y = Mathf.Abs(r1.rect.y - r2.rect.y); lR2.y = Mathf.Abs((r1.rect.y + r1.rect.height) - (r2.rect.y + r2.rect.height)); } //if((r1.id == 7 || r1.id == 16) && (r2.id == 7 || r2.id == 16)) //{ // Debug.Log(lR1); // Debug.Log(lR2); //} CreateLConnectionLib(r1, r2, lR1.x * r1Direction.x, lR2.y * r2Direction.y, _rooms, ref rcl); CreateLConnectionLib(r2, r1, lR2.x * r2Direction.x, lR1.y * r1Direction.y, _rooms, ref rcl); } }
public void StartGenerateStraightConnectionLib(List <Room> _rooms) { //corridors.Clear(); roomPossibleConnections.Clear(); foreach (LineSegment l in roomConnection.Keys) { List <Room> connectedRoom = roomConnection[l]; RoomConnectionLib rcl = new RoomConnectionLib(connectedRoom[0], connectedRoom[1]); rcl.connections = new List <RoomConnectionLib.Line>(); int xPos = 0; int yPos = 0; //Check vertical first, will shifting x xPos = 0; while (xPos < connectedRoom[0].rect.width) { Vector2 intersection1, intersection2; Vector2 p1, p2; p1 = connectedRoom[0].rect.position + new Vector2(xPos, yPos) + Vector2.one * 0.1f; p2 = new Vector2(connectedRoom[0].rect.x, connectedRoom[1].rect.y) + new Vector2(xPos, 0) + Vector2.one * 0.1f; if (connectedRoom[1].rect.Intersect(p1, p2, out intersection2)) { if (connectedRoom[0].rect.Intersect(p1, p2, out intersection1)) { rcl.connections.Add(new RoomConnectionLib.Line(intersection1, intersection2)); } rcl.corridorDir = (CorridorDir)1; rcl.connections[rcl.connections.Count - 1].direction = (p2 - p1).normalized; //will check if any room between connected room foreach (Room r in _rooms) { if (r == connectedRoom[0] || r == connectedRoom[1]) { continue; } if ((Mathf.Max(connectedRoom[0].position.y, connectedRoom[1].position.y + connectedRoom[1].size.y) >= r.position.y + r.size.y) && (Mathf.Min(connectedRoom[0].position.y + connectedRoom[0].size.y, connectedRoom[1].position.y) <= r.position.y)) { Vector2 tempIntersect; if (r.rect.Intersect(p1, p2, out tempIntersect)) { #if DEBUG_MODE Debug.Log("Dude, that was a room between connected room, now I will delete that connection for you <3"); #endif rcl.connections.RemoveAt(rcl.connections.Count - 1); break; } } } } xPos += 1; } //if no intersection when vertical checking, check with horizontal if (rcl.connections.Count == 0) { xPos = 0; yPos = 0; //Check horizontal, will shifting y while (yPos < connectedRoom[0].rect.height) { Vector2 intersection1, intersection2; Vector2 p1, p2; p1 = connectedRoom[0].rect.position + new Vector2(xPos, yPos) + Vector2.one * 0.1f; p2 = new Vector2(connectedRoom[1].rect.x, connectedRoom[0].rect.y) + new Vector2(0, yPos) + Vector2.one * 0.1f; if (connectedRoom[1].rect.Intersect(p1, p2, out intersection2)) { if (connectedRoom[0].rect.Intersect(p1, p2, out intersection1)) { rcl.connections.Add(new RoomConnectionLib.Line(intersection1, intersection2)); } rcl.corridorDir = (CorridorDir)2; rcl.connections[rcl.connections.Count - 1].direction = (p2 - p1).normalized; //will check if any room between connected room foreach (Room r in _rooms) { if (r == connectedRoom[0] || r == connectedRoom[1]) { continue; } if ((Mathf.Max(connectedRoom[0].position.x, connectedRoom[1].position.x + connectedRoom[1].size.x) >= r.position.x + r.size.x) && (Mathf.Min(connectedRoom[0].position.x + connectedRoom[0].size.x, connectedRoom[1].position.x) <= r.position.x)) { Vector2 tempIntersect; if (r.rect.Intersect(p1, p2, out tempIntersect)) { #if DEBUG_MODE Debug.Log("Dude, that was a room between connected room, now I will delete that connection for you <3"); #endif rcl.connections.RemoveAt(rcl.connections.Count - 1); break; } } } } yPos += 1; } } //Remove first and last possible connection, why?? we don't want generate corridor at the edge / corner if (rcl.connections.Count > 0) { rcl.connections.RemoveAt(0); if (rcl.connections.Count > 0) { rcl.connections.RemoveAt(rcl.connections.Count - 1); } //Also we need adjust the possible connection with the width of the corridor. //the formula is remove list from behind until (corridorSize - 1) int possibleSize = rcl.connections.Count; for (int i = 0; i < corridorWidth - 1; i++) { if (rcl.connections.Count > 0) { rcl.connections.RemoveAt(rcl.connections.Count - 1); } else { #if DEBUG_MODE Debug.LogWarning("Connection between Room : " + connectedRoom[0].id + " and Room : " + connectedRoom[1].id + " are imposible!! Corridor size is bigger than possible size between room : " + possibleSize); #endif break; } } } roomPossibleConnections.Add(rcl); } }