private Room MakeCopy(Room r)
 {
     if (r is CircleRoom)
     {
         CircleRoom circle = (CircleRoom)Instantiate(r);
         circle.Init();
         circle.transform.position   = r.transform.position;
         circle.OriginalPosition     = circle.transform.position;
         circle.transform.localScale = r.transform.localScale;
         circle.Radius = r.transform.localScale.x / 2f;
         r.gameObject.SetActive(false);
         return(circle);
     }
     else
     {
         RectangleRoom rect = (RectangleRoom)Instantiate(r);
         rect.transform.position   = r.transform.position;
         rect.OriginalPosition     = rect.transform.position;
         rect.transform.rotation   = r.transform.rotation;
         rect.transform.localScale = r.transform.localScale;
         rect.Dimentions           = rect.transform.localScale;
         r.gameObject.SetActive(false);
         return(rect);
     }
 }
Example #2
0
        /// <summary> Uses Raycasting to attempt to place a room within the given bounds. </summary>
        /// <param name="isCircle"> Whether to place a rectangle or a circle. </param>
        /// <param name="verticalBounds"> The vertical limits in which to try to place a room. </param>
        /// <param name="horizontalBounds"> The horizontal limits in which to try to place a room. </param>
        /// <returns> The placed room if one can be placed. </returns>
        private Room RayCastDart(bool isCircle, Vector2 verticalBounds, Vector2 horizontalBounds)
        {
            int currentAttempt = 0;

            while (currentAttempt < this.numberOfAttempts)
            {
                if (isCircle)
                {
                    float      x        = Random.Range(horizontalBounds.x, horizontalBounds.y);
                    float      y        = Random.Range(verticalBounds.y, verticalBounds.x);
                    float      size     = this.resize ? Random.Range(this.sizeBound.x, this.sizeBound.y) : 1f;
                    Collider[] overlaps = Physics.OverlapSphere(new Vector2(x, y), this.circleRadius * size);
                    if (overlaps.Length == 0)
                    {
                        CircleRoom circle = Instantiate <CircleRoom>(this.circleTemplet);
                        circle.Init();
                        circle.transform.position   = new Vector2(x, y);
                        circle.OriginalPosition     = circle.transform.position;
                        circle.transform.localScale = Vector3.one * this.circleRadius * 2f * size;
                        circle.transform.localScale = new Vector3(circle.transform.localScale.x, circle.transform.localScale.y, 1);
                        circle.Radius = this.circleRadius * size;
                        return(circle);
                    }
                }
                else
                {
                    float      x        = Random.Range(horizontalBounds.x, horizontalBounds.y);
                    float      y        = Random.Range(verticalBounds.y, verticalBounds.x);
                    float      sizeX    = this.resize ? Random.Range(this.sizeBound.x, this.sizeBound.y) : 1f;
                    float      sizeY    = this.resize ? Random.Range(this.sizeBound.x, this.sizeBound.y) : 1f;
                    Quaternion rotation = this.rotate ? Quaternion.Euler(new Vector3(1, 1, Random.Range(0f, 90f))) : Quaternion.identity;
                    Collider[] overlaps = Physics.OverlapBox(new Vector2(x, y), new Vector3(this.rectDimensions.x * sizeX, this.rectDimensions.y * sizeY, 1) / 2f, rotation);
                    if (overlaps.Length == 0)
                    {
                        RectangleRoom rect = Instantiate <RectangleRoom>(this.rectangleTemplet);
                        rect.transform.position   = new Vector2(x, y);
                        rect.OriginalPosition     = rect.transform.position;
                        rect.transform.rotation   = rotation;
                        rect.transform.localScale = new Vector3(this.rectDimensions.x * sizeX, this.rectDimensions.y * sizeY, 1);
                        rect.Dimentions           = rect.transform.localScale;
                        return(rect);
                    }
                }

                currentAttempt++;
            }

            Debug.Log("Unable to place room after " + currentAttempt + " attempts.");
            return(null);
        }