예제 #1
0
        private bool ConnectGridsAxisAligned(GridShape startShape, GridShape endShape, ref Vector2Int startPos, ref Vector2Int endPos)
        {
            //we pad with min hallway size to cover our bases - not always going to work out
            //KPD TODO revisit padding here.
            Vector2Int minA = new Vector2Int(startShape.MinX() + HallwayMinSize, startShape.MinY() + HallwayMinSize);
            Vector2Int maxA = new Vector2Int(startShape.MaxX() - HallwayMinSize, startShape.MaxY() - HallwayMinSize);

            Vector2Int minB = new Vector2Int(endShape.MinX() + HallwayMinSize, endShape.MinY() + HallwayMinSize);
            Vector2Int maxB = new Vector2Int(endShape.MaxX() - HallwayMinSize, endShape.MaxY() - HallwayMinSize);

            startPos = new Vector2Int(Random.Range(minA.x, maxA.x), Random.Range(minA.y, maxA.y));
            endPos   = new Vector2Int(Random.Range(minB.x, maxB.x), Random.Range(minB.y, maxB.y));


            bool overlapOnX = (minA.x <= maxB.x && maxA.x >= minB.x);
            bool overlapOnY = (minA.y <= maxB.y && maxA.y >= minB.y);

            if (overlapOnX && overlapOnY)
            {
                return(false); //already overlapping on both axes.
            }
            if (overlapOnX)
            {
                startPos.x = Random.Range(Mathf.Max(minA.x, minB.x), Mathf.Min(maxA.x, maxB.x));
                startPos.y = Random.Range(minA.y, maxA.y);
                endPos.x   = startPos.x;
                endPos.y   = Random.Range(minB.y, maxB.y);
                return(false);
            }
            else if (overlapOnY)
            {
                startPos.y = Random.Range(Mathf.Max(minA.y, minB.y), Mathf.Min(maxA.y, maxB.y));
                startPos.x = Random.Range(minA.x, maxA.x);
                endPos.y   = startPos.y;
                endPos.x   = Random.Range(minB.x, maxB.x);
                return(false);
            }


            //at this point we need a bend, so we make the joint
            int jointX = startPos.x;
            int jointY = endPos.y;

            //and then do all the work to get and set these 2 paths.
            m_Shapes = GetLineBetweenPoints(startPos, new Vector2Int(jointX, jointY));
            m_Shapes.AddRange(GetLineBetweenPoints(new Vector2Int(jointX, jointY), endPos));
            List <Vector2Int> m_targetPositions = new List <Vector2Int>();

            foreach (GridShape gs in m_Shapes)
            {
                gs.GetIndicesInShape(ref m_targetPositions);
            }
            foreach (Vector2Int pos in m_targetPositions)
            {
                if (GridToArray(pos) < m_result.Length && GridToArray(pos) >= 0)
                {
                    m_result[GridToArray(pos)] = MapValue;
                }
            }
            //returning true tells the calling method that it doesn't need to generate it's path
            //as we've done it above.
            return(true);
        }