Ejemplo n.º 1
0
    public bool PieceFits(GameObject piece, int[,] pieceValues)
    {
        PieceController p = piece.GetComponent <PieceController>();

        //figure out where it's going to snap to on the grid
        //find top left corner of piece sprite as vector3
        Vector3 topLeftCorner = p.GetTopLeftCorner();

        topLeftCorner = new Vector3(topLeftCorner.x, topLeftCorner.y + gridLength, topLeftCorner.z);
        //SpriteRenderer sprite = piece.GetComponent<SpriteRenderer>();

        //find x and y distance of that corner from top left corner of grid
        //SpriteRenderer gridSprite = transform.gameObject.GetComponent<SpriteRenderer>();
        float xdist = topLeftCorner.x - gridCorner.x;
        float ydist = topLeftCorner.y - (gridCorner.y + gridLength + 2 * squareWidth);       //THIS IS BAD
        //produce vector3 corresponding to position relative to top left of grid
        Vector3 relativePos = new Vector3(xdist, ydist, topLeftCorner.z);

        //add half grid square length to x and y of vector for rounding purposes
        relativePos = new Vector3(relativePos.x + squareWidth / 2, relativePos.y + squareWidth / 2, relativePos.z);

        //while said vector x isn't negative:
        //subtract grid square width and increment an int counter (starting at -1)
        xcounter = -1;
        while (relativePos.x >= 0)
        {
            relativePos = new Vector3(relativePos.x - squareWidth, relativePos.y, relativePos.z);
            xcounter++;
        }
        ycounter = -1;
        while (relativePos.y >= 0)         //ditto y
        {
            relativePos = new Vector3(relativePos.x, relativePos.y - squareWidth, relativePos.z);
            ycounter++;
        }

        //if either dimension is negative, return false
        ycounter = GRID_SIZE - 2 - ycounter;
        //Debug.Log (ycounter);
        if (ycounter < 0 || xcounter < 0)
        {
            return(false);
        }

        //counters are now the grid coordinates, probably

        //make sure its width/height actually fit
        if (xcounter + pieceValues.GetLength(1) > GRID_SIZE)
        {
            return(false);
        }
        if (ycounter + pieceValues.GetLength(0) > GRID_SIZE)
        {
            return(false);
        }
        //Debug.Log("ycounter is " + ycounter + " and xcounter is " + xcounter);
        //at this point we know it at least fits within the bounds of the grid

        //compare each grid square
        for (int i = 0; i < pieceValues.GetLength(0); i++)
        {
            for (int j = 0; j < pieceValues.GetLength(1); j++)
            {
                //Debug.Log("i is " + i + " and j is " + j);
                Occupancy o         = grid[ycounter + i, xcounter + j];
                int       shapecode = pieceValues[i, j];

                //do some simple checks
                if (shapecode == 0 || o.IsEmpty())
                {
                    continue;
                }
                if (shapecode == 1 && !o.IsEmpty())
                {
                    return(false);
                }
                if (shapecode != 0 && o.IsFull())
                {
                    return(false);
                }

                //do more complicated stuff
                if (shapecode == NORTHWEST_CODE && !(o.north == null && o.west == null))
                {
                    return(false);
                }
                if (shapecode == NORTHEAST_CODE && !(o.north == null && o.east == null))
                {
                    return(false);
                }
                if (shapecode == SOUTHEAST_CODE && !(o.south == null && o.east == null))
                {
                    return(false);
                }
                if (shapecode == SOUTHWEST_CODE && !(o.south == null && o.west == null))
                {
                    return(false);
                }
            }
        }
        //we've gone through and there've been no collisions
        return(true);
    }