Esempio n. 1
0
    IEnumerator PieceConfetti()
    {
        GameObject[] pieces = GameObject.FindGameObjectsWithTag("Piece");
        for (int i = 0; i < pieces.Length; i++)
        {
            GameObject   bit        = pieces[i];
            PieceWrapper wrapper    = bit.GetComponent <PieceWrapper>();
            Color        pieceColor = wrapper.backColor;
            pieceColor.a = 255;
            Destroy(bit.gameObject);
            GameObject     confetti          = Resources.Load("Confetti") as GameObject;
            ParticleSystem confettiParticles = confetti.GetComponent <ParticleSystem>();
            confettiParticles.startColor = pieceColor;
            Instantiate(confetti, bit.transform.position, Quaternion.Euler(-90, 0, 0));
            yield return(new WaitForSeconds(0.3f));

            yield return(0);
        }

        //WHEN THIS COROUTINE IS OVER, RESTART THE GAME!
        yield return(new WaitForSeconds(4.0f));

        Application.LoadLevel(Application.loadedLevel);
        //Application.LoadLevel ("credits");
    }
Esempio n. 2
0
    public void onButtonPressed()
    {
        GameObject[] pieces = GameObject.FindGameObjectsWithTag("Piece");

        foreach (GameObject piece in pieces)
        {
            PieceWrapper pieceWrapper = piece.GetComponent <PieceWrapper>();
            if (!pieceWrapper.isHint())
            {
                int x = UnityEngine.Random.Range(7, 12);
                int y = UnityEngine.Random.Range(0, 5);
                if (UnityEngine.Random.value < 0.5)
                {
                    x *= -1;
                }
                if (UnityEngine.Random.value < 0.5)
                {
                    y *= -1;
                }

                piece.transform.position = new Vector3(x, y, 0);
            }
        }

        GameObject[] errors = GameObject.FindGameObjectsWithTag("Error");

        foreach (GameObject error in errors)
        {
            Object.Destroy(error);
        }
    }
Esempio n. 3
0
        public void MovePiece(Piece piece, int sq64)
        {
            Vector3      worldPoint = ToWorldPoint(sq64);
            PieceWrapper wrapper    = FindPieceWrapper(piece);

            wrapper.Square             = (Square)Board.Sq120(sq64);
            wrapper.transform.position = new Vector3(worldPoint.x, wrapper.transform.position.y, worldPoint.z);
        }
Esempio n. 4
0
        public PieceWrapper SpawnPiece(Piece piece)
        {
            Vector3   worldPoint = ToWorldPoint(Board.Sq64((int)piece.Square));
            Transform transform  = Instantiate(piecePrefabs[piece.Index]);

            transform.position = new Vector3(worldPoint.x, transform.position.y, worldPoint.z);
            transform.parent   = Pieces.transform;
            PieceWrapper wrapper = transform.GetComponent <PieceWrapper>();

            wrapper.Square = piece.Square;
            return(wrapper);
        }
Esempio n. 5
0
 public PieceWrapper FindPieceWrapper(Piece piece)
 {
     foreach (Transform child in Pieces.transform)
     {
         PieceWrapper current = child.GetComponent <PieceWrapper>();
         if (current.Square == piece.Square)
         {
             return(current);
         }
     }
     return(null);
 }
Esempio n. 6
0
 public void DestroyPiece(Piece piece)
 {
     try
     {
         PieceWrapper wrapper = FindPieceWrapper(piece);
         Destroy(wrapper.gameObject);
     }
     catch (Exception e)
     {
         Debug.Log(gc.Board.ToString());
         throw e;
     }
 }
Esempio n. 7
0
        public void RemoveHighlightFromPiece(Piece piece)
        {
            if (gameController.OnTurn is Bot)
            {
                return;
            }
            PieceWrapper wrapper = spawner.FindPieceWrapper(piece);

            try
            {
                if (wrapper != null)
                {
                    var mat = (piece.Color == ChessEngine.Color.White ? materials[(int)Materials.White] : materials[(int)Materials.Black]);
                    var ren = wrapper.GetComponent <Renderer>();
                    ren.material = mat;
                }
            }
            catch (Exception ex)
            {
                Debug.Log(ex.Message);
            }
        }
Esempio n. 8
0
        public void HighlightPiece(Piece piece)
        {
            if (gameController.OnTurn is Bot)
            {
                return;
            }
            PieceWrapper wrapper = spawner.FindPieceWrapper(piece);

            try
            {
                if (wrapper != null)
                {
                    var mat = materials[(int)Materials.Gold];
                    var ren = wrapper.GetComponent <Renderer>();
                    ren.material = mat;
                }
            }
            catch (Exception ex)
            {
                Debug.Log(ex.Message);
            }
        }
Esempio n. 9
0
    private void addSnappedPiecesToBoard()
    {
        pieceContainers = GameObject.FindGameObjectsWithTag("Piece");

        foreach (GameObject obj in pieceContainers)
        {
            PieceWrapper pieceWrapper = obj.GetComponent <PieceWrapper>();

            if (pieceWrapper.isSnapped())
            {
                Piece piece = pieceWrapper.getPiece();
                int[,] pieceNumbers = piece.to2DArray();

                float xOffset = (piece.getWidth() - 1) / 2.0f;
                float yOffset = (piece.getHeight() - 1) / 2.0f;

                int pieceRow = 4 - (int)(obj.transform.position.y + yOffset);
                int pieceCol = 4 + (int)(obj.transform.position.x - xOffset);

                for (int i = 0; i < piece.getHeight(); i++)
                {
                    for (int j = 0; j < piece.getWidth(); j++)
                    {
                        try {
                            if (pieceNumbers[i, j] != 0)
                            {
                                boardNumbers[pieceRow + i, pieceCol + j] = pieceNumbers[i, j];
                            }
                        }
                        catch {
                            // Do nothing
                        }
                    }
                }
            }
        }
    }
Esempio n. 10
0
    private void generateVoronoiPieces(int numPoints, int numHints, int minSize)
    {
        if (numPoints <= 0 || numPoints > 81)
        {
            Debug.LogError("Invalid number of points");
        }

        int[,] voronoiPoints  = new int[9, 9];
        int[,] pointLocations = new int[numPoints, 2];

        int pointCounter = 0;

        while (pointCounter < numPoints)
        {
            int r = UnityEngine.Random.Range(0, 9);
            int c = UnityEngine.Random.Range(0, 9);

            if (voronoiPoints[r, c] == 0)
            {
                voronoiPoints[r, c] = 1;

                pointLocations[pointCounter, 0] = r;
                pointLocations[pointCounter, 1] = c;

                pointCounter++;
            }
        }

        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                voronoiMask[i, j] = indexOfClosestVoronoiPoint(i, j, pointLocations);
            }
        }

        for (int i = 0, colorIndex = UnityEngine.Random.Range(0, colorTable.Length); i < numPoints; i++)
        {
            int[,] untrimmedPiece = new int[9, 9];

            for (int j = 0; j < 9; j++)
            {
                for (int k = 0; k < 9; k++)
                {
                    if (voronoiMask[j, k] == i)
                    {
                        untrimmedPiece[j, k] = solvedNumbers[j, k];
                    }
                }
            }

            Piece p = new Piece(untrimmedPiece);

            Color numberColor = Color.black;
            Color tileColor   = Color.black;

            float x = UnityEngine.Random.Range(7, 12);
            float y = UnityEngine.Random.Range(0, 5);
            if (UnityEngine.Random.value < 0.5)
            {
                x *= -1;
            }
            if (UnityEngine.Random.value < 0.5)
            {
                y *= -1;
            }

            GameObject   t            = (GameObject)GameObject.Instantiate(piecePrefab, new Vector3(x, y, 0), Quaternion.identity);
            PieceWrapper pieceWrapper = t.GetComponent <PieceWrapper>();

            if (i < numHints || p.getNumTiles() < minSize)
            {
                pieceWrapper.makeHint();
            }
            else
            {
                if (colorTable.Length > 0)
                {
                    ColorPair colorPair = colorTable[colorIndex++ % colorTable.Length];
                    numberColor = colorPair.numberColor;
                    tileColor   = colorPair.tileColor;
                }

                for (int j = 0; j < 3; j++)
                {
                    if (UnityEngine.Random.value < 0.5)
                    {
                        p.rotateClockwise();
                    }
                }
            }

            pieceWrapper.SetData(p, numberColor, tileColor);
        }
    }
    public void MakePieces(List <int[, ]> piecesList, int minSize, int numHints, int numLargeHints)
    {
        foreach (int[,] piece in piecesList)
        {
            Piece p = new Piece(piece);
            jigsawPiecesPlaceholder.Add(p);
        }

        int counter = 0;

        // sort jigsawPiecesPlaceHolder according to number of tiles in pieces.
        Piece[] sortedPieces = new Piece[jigsawPiecesPlaceholder.Count];
        Piece   current_p;

        foreach (Piece p in jigsawPiecesPlaceholder)
        {
            int  placeIndex = -1;
            bool move       = false;
            for (int i = 0; i < sortedPieces.Length; i++)
            {
                current_p = sortedPieces [i];

                if (current_p != null)
                {
                    if (p.getNumTiles() < current_p.getNumTiles())
                    {
                        // once we find a larger piece, we stop and break
                        placeIndex = i;
                        move       = true;
                        break;
                    }
                    else if (p.getNumTiles() >= current_p.getNumTiles())
                    {
                        // continue till we find a larger piece
                        continue;
                    }
                }
                else                                                     // it reaches here if p is equal to sortedPieces[i-1] and sortedPieces[i] is null
                {
                    placeIndex = i;
                    move       = false;
                    break;
                }
            }
            // now place p at placeIndex in sortedPieces and move other pieces in array up if we need to
            if (move)
            {
                for (int i = sortedPieces.Length - 1; i >= placeIndex; i--)
                {
                    if (sortedPieces [i] == null)
                    {
                        // only move actual pieces forward
                        continue;
                    }
                    else if (i > placeIndex)
                    {
                        sortedPieces [i + 1] = sortedPieces [i];
                        sortedPieces [i]     = null;
                    }
                    else if (i == placeIndex)
                    {
                        sortedPieces [i + 1] = sortedPieces [i];
                        sortedPieces [i]     = p;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else
            {
                sortedPieces [placeIndex] = p;
            }
        }

        int hints      = 0;
        int smallHints = 0;
        int largeHints = 0;

        foreach (Piece p in sortedPieces)
        {
            // Do not use random colors; they look bad. Instead, pull predetermined color pairs from an array.

            Color numberColor = Color.black;                                     // Default colors
            Color tileColor   = Color.black;



            int x = UnityEngine.Random.Range(7, 12);
            int y = UnityEngine.Random.Range(0, 5);
            if (UnityEngine.Random.value < 0.5)
            {
                x *= -1;
            }
            if (UnityEngine.Random.value < 0.5)
            {
                y *= -1;
            }

            GameObject   t            = (GameObject)GameObject.Instantiate(piecePrefab, new Vector3(x, y, 0), Quaternion.identity);                         //, new Vector3(x, y, 0), Quaternion.identity);
            PieceWrapper pieceWrapper = t.GetComponent <PieceWrapper> ();


            if (p.getNumTiles() < minSize)
            {
                pieceWrapper.makeHint();
                hints++;
                smallHints++;
            }
            else if (hints < numHints && largeHints <= numLargeHints)
            {
                pieceWrapper.makeHint();
                hints++;
                largeHints++;
            }
            else if (colorTable.Length > 0)
            {
                ColorPair colorPair = colorTable [counter % colorTable.Length];
                numberColor = colorPair.numberColor;
                tileColor   = colorPair.tileColor;


                for (int j = 0; j < 3; j++)
                {
                    if (UnityEngine.Random.value < 0.5)
                    {
                        p.rotateClockwise();
                    }
                }
            }



            pieceWrapper.SetData(p, numberColor, tileColor);


            //No need to call awake/start (They are automatically called after this.)
            counter++;
        }
    }