示例#1
0
    /// <summary>
    /// Move the piece in the direction given
    /// </summary>
    /// <param name="dir">The direction the piece will move</param>
    /// <returns></returns>
    private bool MovePiece(Vector2 dir)
    {
        //Get the new position the piece will be
        Vector2 newPos = gridPos + dir;

        //Test if the new position is in the play area
        if (field.InBounds(newPos))
        {
            //Make sure there isn't anything in the new position
            if (field.gameObjects[newPos] == null)
            {
                //Set the current position to have nothing there
                field.gameObjects[gridPos] = null;

                //Set the new position and occupy the new space
                gridPos = newPos;
                field.gameObjects[gridPos] = gameObject;
                return(true);
            }
        }

        return(false);
    }
示例#2
0
    /// <summary>
    /// Rotate the pill in a 2*2 square
    /// </summary>
    /// <param name="direction">The direction the pill will rotate</param>
    /// <returns></returns>
    private bool RotatePill(int direction)
    {
        //Get the next rotation the pill will be at
        int localRot = ((rotation + 360) + 90 * direction) % 360;

        //The new position for the pill
        Vector2 newPos1 = gridPos1;
        Vector2 newPos2 = gridPos2;

        if (direction > 0)
        {
            //Set the new position based on the rotation
            switch (localRot)
            {
            case 0:
                newPos2 += new Vector2(1, 1);
                break;

            case 90:
                newPos1 -= new Vector2(0, 1);
                newPos2 -= new Vector2(1, 0);
                break;

            case 180:
                newPos1 += new Vector2(1, 1);
                break;

            case 270:
                newPos1 -= new Vector2(1, 0);
                newPos2 -= new Vector2(0, 1);
                break;
            }
        }
        else
        {
            switch (localRot)
            {
            case 0:
                newPos1 += new Vector2(0, 1);
                newPos2 += new Vector2(1, 0);
                break;

            case 90:
                newPos1 -= new Vector2(1, 1);
                break;

            case 180:
                newPos1 += new Vector2(1, 0);
                newPos2 += new Vector2(0, 1);
                break;

            case 270:
                newPos2 -= new Vector2(1, 1);
                break;
            }
        }

        if (Mathf.Abs(direction) == 2)
        {
            Vector2 oldPos = gridPos1;
            newPos1  = gridPos2;
            newPos2  = oldPos;
            localRot = rotation + 180;
        }

        //Test if the movement will be in bounds
        if (field.InBounds(newPos1) &&
            field.InBounds(newPos2))
        {
            //Make sure there isn't an  object in the new position
            if ((field.gameObjects[newPos1] == null ||
                 field.gameObjects[newPos1] == gameObject) &&
                (field.gameObjects[newPos2] == null ||
                 field.gameObjects[newPos2] == gameObject))
            {
                //Rotate the pill
                rotation += (90 * direction);
                rotation %= 360;
                transform.localRotation = Quaternion.Euler(new Vector3(0, 0, -rotation));

                //Remove the pill from the current position
                field.gameObjects[gridPos1] = null;
                field.gameObjects[gridPos2] = null;

                //Change the position of the pill
                gridPos1 = newPos1;
                gridPos2 = newPos2;

                //Set the new occupation of the pill
                field.gameObjects[gridPos1] = gameObject;
                field.gameObjects[gridPos2] = gameObject;
                return(true);
            }
        }
        return(false);
    }