public Rect findRoom() { if (isLeaf()) { return(room); } if (left != null) { Rect leftroom = left.findRoom(); if (!leftroom.x.Equals(-1)) { return(leftroom); } } if (right != null) { Rect rightroom = right.findRoom(); if (!rightroom.x.Equals(-1)) { return(rightroom); } } return(new Rect(-1, -1, 0, 0)); }
public void CreateCorridor(SubDungeon left, SubDungeon right) { Rect leftroom = left.findRoom(); Rect rightroom = right.findRoom(); //Debug.Log("Creating corridor(s) between " + left.debugId + "(" + leftroom + ") and " + right.debugId + " (" + rightroom + ")"); //create the corridor to a random point in each room Vector2 leftpoint = new Vector2((int)Random.Range(leftroom.x + 1, leftroom.xMax - 1), (int)Random.Range(leftroom.y + 1, leftroom.yMax - 1)); Vector2 rightpoint = new Vector2((int)Random.Range(rightroom.x + 1, rightroom.xMax - 1), (int)Random.Range(rightroom.y + 1, rightroom.yMax - 1)); if (leftpoint.x > rightpoint.x) { Vector2 temp = leftpoint; leftpoint = rightpoint; rightpoint = temp; } int w = (int)(leftpoint.x - rightpoint.x); int h = (int)(leftpoint.y - rightpoint.y); //Debug.Log("lpoint: " + lpoint + ", rpoint: " + rpoint + ", w: " + w + ", h: " + h); // if the points are not horizontally if (w != 0) { // randomly choose to go horizontal then vertical or the opposite if (Random.Range(0, 1) > 2) { // add a corridor to the right corridors.Add(new Rect(leftpoint.x, leftpoint.y, Mathf.Abs(w) + 1, 1)); // if left point is below right point go up otherwise go down if (h < 0) { corridors.Add(new Rect(rightpoint.x, leftpoint.y, 1, Mathf.Abs(h))); } else { corridors.Add(new Rect(rightpoint.x, leftpoint.y, 1, -Mathf.Abs(h))); } } else { // go up or down if (h < 0) { corridors.Add(new Rect(leftpoint.x, leftpoint.y, 1, Mathf.Abs(h))); } else { corridors.Add(new Rect(leftpoint.x, rightpoint.y, 1, Mathf.Abs(h))); } // go right corridors.Add(new Rect(leftpoint.x, rightpoint.y, Mathf.Abs(w) + 1, 1)); } } else { // if the points are aligned horizontally go up or down if (h < 0) { corridors.Add(new Rect((int)leftpoint.x, (int)leftpoint.y, 1, Mathf.Abs(h))); } else { corridors.Add(new Rect((int)rightpoint.x, (int)rightpoint.y, 1, Mathf.Abs(h))); } } //Debug.Log("Corridors: "); //foreach (Rect corridor in corridors) //{ // Debug.Log("corridor: " + corridor); //} }