Beispiel #1
0
    private void AddRoomOpeningsAtSide(int side)
    {
        // CANDO: #optimization. ONLY add Grounds that are on the sides.
        float searchUnit = 1; // how granular our searches are. The smaller this value, the more steps we take along sides of the room.
        Rect  bl         = BoundsLocalBL;
        // Determine where we start search, and what dir to go.
        float   sideLength = 0;
        Vector2 cornerPos  = Vector2.zero; // default to whatever.
        Vector2 dir        = Vector2.one;  // default to whatever.

        switch (side)
        {
        case Sides.L:     // BL to TL
            sideLength = bl.height;
            cornerPos  = new Vector2(bl.xMin, bl.yMin);
            dir        = Vector2.up;
            break;

        case Sides.R:                                                          // BR to TR
            sideLength = bl.height;
            cornerPos  = new Vector2(bl.xMax, bl.yMin) - new Vector2(0.1f, 0); // add small bloat so we don't miss any Grounds.
            dir        = Vector2.up;
            break;

        case Sides.B:     // BL to BR
            sideLength = bl.width;
            cornerPos  = new Vector2(bl.xMin, bl.yMin);
            dir        = Vector2.right;
            break;

        case Sides.T:                                                          // TL to TR
            sideLength = bl.width;
            cornerPos  = new Vector2(bl.xMin, bl.yMax) - new Vector2(0, 0.1f); // add small bloat so we don't miss any Grounds.
            dir        = Vector2.right;
            break;
        }
        Rect searchRect = new Rect {
            size = new Vector2(searchUnit, searchUnit)
        };
        // Step slowly along this side!
        int     numSteps        = Mathf.CeilToInt(sideLength / searchUnit) + 1; // Note: Idk why +1.
        Vector2 openingStartPos = Vector2Extensions.NaN;                        // when this ISN'T NaN, we're making an opening!

        for (int i = 0; i < numSteps; i++)
        {
            // Update the pos of the search-rect.
            searchRect.position = cornerPos + dir * i;
            bool isGround = IsGround(searchRect);
            // We're NOT yet making an opening...
            if (Vector2Extensions.IsNaN(openingStartPos))
            {
                // There's NO ground...!
                if (!isGround)
                {
                    openingStartPos = searchRect.position;
                }
            }
            // We ARE making an opening...!
            else
            {
                // There IS Ground (OR it's the final search-step)...!
                if (isGround || i == numSteps - 1)
                {
                    AddRoomOpening(side, openingStartPos, searchRect.position);
                    openingStartPos = Vector2Extensions.NaN; // clear this out! So we start looking for a new opening again.
                }
            }
        }
    }