/// <summary> /// Always return a Rect[] of 2 length /// </summary> /// <returns></returns> private Rect[] GetDivideRoomSize() { int throwDice = UtilityMethod.RollDice(); Rect[] rooms = new Rect[2]; //Horizontal if (throwDice == 0) { if (rect.width / rect.height > AllowRatioDifference) { return(GetDivideRoomSize()); } float randomY = UtilityMethod.RandRangeToInt(rect.height * 0.4f, rect.height * 0.6f); rooms[0] = new Rect(rect.x, rect.y, rect.width, randomY); rooms[1] = new Rect(rect.x, rect.y + randomY, rect.width, rect.height - randomY); //Vertical } else { if (rect.height / rect.width > AllowRatioDifference) { return(GetDivideRoomSize()); } float randomX = UtilityMethod.RandRangeToInt(rect.width * 0.4f, rect.width * 0.6f); rooms[0] = new Rect(rect.x, rect.y, randomX, rect.height); rooms[1] = new Rect(rect.x + randomX, rect.y, rect.width - randomX, rect.height); } return(rooms); }
public Rect CalculateRoomRect(Rect p_holderRect) { Vector2 roomPosition = new Vector2( UtilityMethod.RandRangeToInt(p_holderRect.x + (p_holderRect.width * 0.1f), p_holderRect.x + (p_holderRect.width * 0.3f)), UtilityMethod.RandRangeToInt(p_holderRect.y + (p_holderRect.height * 0.1f), p_holderRect.y + (p_holderRect.height * 0.3f)) ); float maxWidth = p_holderRect.width - (roomPosition.x - p_holderRect.x); float maxheight = p_holderRect.height + (p_holderRect.y - roomPosition.y); Vector2 size = new Vector2( UtilityMethod.RandRangeToInt(maxWidth * 0.8f, maxWidth * 0.9f), UtilityMethod.RandRangeToInt(maxheight * 0.8f, maxheight * 0.9f) ); return(new Rect(roomPosition, size)); }
private void LinkCorridorBetween(BSPRoom left, BSPRoom right) { tempCorridors.Clear(); Rect lroom = left.spaceRect; Rect rroom = right.spaceRect; // Debug.Log("Creating corridor(s) between " + left.debugId + "(" + lroom + ") and " + right.debugId + " (" + rroom + ")"); // attach the corridor to a random point in each room Vector2 lpoint = new Vector2(UtilityMethod.RandRangeToInt(lroom.x + 1, lroom.xMax - 1), UtilityMethod.RandRangeToInt(lroom.y + 1, lroom.yMax - 1)); Vector2 rpoint = new Vector2(UtilityMethod.RandRangeToInt(rroom.x + 1, rroom.xMax - 1), UtilityMethod.RandRangeToInt(rroom.y + 1, rroom.yMax - 1)); //Debug.Log("lPoint " + lpoint +", rPoint " + rpoint); // always be sure that left point is on the left to simplify the code if (lpoint.x > rpoint.x) { Vector2 temp = lpoint; lpoint = rpoint; rpoint = temp; } int w = (int)(lpoint.x - rpoint.x); int h = (int)(lpoint.y - rpoint.y); int corridorSize = 1; // if the points are not aligned horizontally if (w != 0) { // choose at random to go horizontal then vertical or the opposite if (Random.Range(0, 1) > 2) { // add a corridor to the right tempCorridors.Add(new BSPCorridor(new Rect(lpoint.x, lpoint.y, Mathf.Abs(w) + 1, corridorSize))); // if left point is below right point go up // otherwise go down if (h < 0) { tempCorridors.Add(new BSPCorridor(new Rect(rpoint.x, lpoint.y, corridorSize, Mathf.Abs(h)))); } else { tempCorridors.Add(new BSPCorridor(new Rect(rpoint.x, lpoint.y, corridorSize, -Mathf.Abs(h)))); } } else { // go up or down if (h < 0) { tempCorridors.Add(new BSPCorridor(new Rect(lpoint.x, lpoint.y, corridorSize, Mathf.Abs(h)))); } else { tempCorridors.Add(new BSPCorridor(new Rect(lpoint.x, rpoint.y, corridorSize, Mathf.Abs(h)))); } // then go right tempCorridors.Add(new BSPCorridor(new Rect(lpoint.x, rpoint.y, Mathf.Abs(w) + 1, corridorSize))); } } else { //假設 房間的x軸對齊的話 //根據y軸的差距 決定往下 或 往上走 if (h < 0) { tempCorridors.Add(new BSPCorridor(new Rect((int)lpoint.x, (int)lpoint.y, corridorSize, Mathf.Abs(h)))); } else { tempCorridors.Add(new BSPCorridor(new Rect((int)rpoint.x, (int)rpoint.y, corridorSize, Mathf.Abs(h)))); } } corridors.AddRange(tempCorridors); //left.FindDoorIntersection(tempCorridors, corridorSize); //right.FindDoorIntersection(tempCorridors, corridorSize); foreach (BSPCorridor corridor in corridors) { //Debug.Log ("corridor: " + corridor.spaceRect); } }