private void SetSwapCellsAdjacents()
    {
        foreach (GameObject cell in cells)
        {
            SwapCell swapCell = cell.GetComponent <SwapCell>();

            float xCordinate = cell.GetComponent <RectTransform>().anchoredPosition.x;
            float yCordinate = cell.GetComponent <RectTransform>().anchoredPosition.y;


            if (randomPositionsOfCells.ContainsKey(new Vector2(xCordinate, yCordinate + pixel)))
            {
                swapCell.SetForward(randomPositionsOfCells[new Vector2(xCordinate, yCordinate + pixel)]);
            }

            if (randomPositionsOfCells.ContainsKey(new Vector2(xCordinate, yCordinate - pixel)))
            {
                swapCell.SetBackward(randomPositionsOfCells[new Vector2(xCordinate, yCordinate - pixel)]);
            }

            if (randomPositionsOfCells.ContainsKey(new Vector2(xCordinate + pixel, yCordinate)))
            {
                swapCell.SetRight(randomPositionsOfCells[new Vector2(xCordinate + pixel, yCordinate)]);
            }

            if (randomPositionsOfCells.ContainsKey(new Vector2(xCordinate - pixel, yCordinate)))
            {
                swapCell.SetLeft(randomPositionsOfCells[new Vector2(xCordinate - pixel, yCordinate)]);
            }
        }
    }
Ejemplo n.º 2
0
    public void SwapBackward()
    {
        if (backward == null)
        {
            return;
        }

        SetTempValues();

        GameObject swapObject = backward;
        SwapCell   swapCell   = swapObject.GetComponent <SwapCell>();

        SetForwardAdjacentsValue(swapObject);
        SetLeftAdjacentsValue(swapObject);
        SetRightAdjacentsValue(swapObject);

        swapCell.SetBackwardAdjacentsValue(gameObject);
        swapCell.SetRightAdjacentsValue(gameObject);
        swapCell.SetLeftAdjacentsValue(gameObject);

        forward  = backward;
        backward = swapCell.GetBackward();
        left     = swapCell.GetLeft();
        right    = swapCell.GetRight();


        swapCell.SetForward(tempForward);
        swapCell.SetBackward(this.gameObject);
        swapCell.SetLeft(tempLeft);
        swapCell.SetRight(tempRight);

        ChangingPositions(swapObject, swapCell);
    }
    private void BuilCell(Vector2 randomCellPosition, Quaternion rotation, int index, float i, float j, Vector2 pivot, Rect rec, int maxGlowWidth)
    {
        //Cordinates for cell
        float xCordinate = (pixel * i) + (pixel / 2f);
        float yCordinate = (pixel * j) + (pixel / 2f);

        Debug.Log("index: " + index.ToString());
        Debug.Log("truePosition: " + new Vector2(xCordinate, yCordinate).ToString());
        Debug.Log("randomPosition: " + randomCellPosition.ToString());
        Debug.Log("rotation: " + rotation.ToString());

        //Creating cells
        GameObject cell = new GameObject();

        //Bounding with script
        cell.AddComponent <TouchSwapCell>();
        //---------------------

        RectTransform rectTransform = cell.AddComponent <RectTransform>() as RectTransform;
        BoxCollider2D box2D         = cell.AddComponent <BoxCollider2D>() as BoxCollider2D;

        //Creating with z: +90 position, because default z position is -180 and we want it to be 0
        if (isRotateEnabled)
        {
            cell.transform.SetPositionAndRotation(randomCellPosition,
                                                  rotation);
        }
        else
        {
            cell.transform.SetPositionAndRotation(randomCellPosition,
                                                  Quaternion.Euler(0, 0, 0));
        }


        //Naming Cell and Setting his parent
        cell.name        = "cell_" + xCordinate.ToString() + "x" + yCordinate.ToString();
        cellsParent.name = "Parent_Of_Cells\\" + pieceCount.ToString() + "-Children";
        cell.transform.SetParent(cellsParent.transform);
        //Scalling
        rectTransform.sizeDelta = new Vector2(pixel, pixel);
        box2D.size = new Vector2(pixel, pixel);
        cell.transform.localScale = new Vector3(1, 1, 1);
        //Positioning; anchorMin and anchorMax are for starting position bottom left corner of parent.
        rectTransform.anchorMin        = new Vector2(0f, 0f);
        rectTransform.anchorMax        = new Vector2(0f, 0f);
        rectTransform.anchoredPosition = new Vector3(x: randomCellPosition.x, y: randomCellPosition.y);
        //Creating Sprite Texture---------------------------------------------------------------------
        Texture2D spriteTexture = new Texture2D((int)(pixel * 100f), (int)(pixel * 100f));
        var       pixels        = painting.texture.GetPixels((int)((pixel * 100) * i), (int)((pixel * 100) * j), (int)(pixel * 100f), (int)(pixel * 100f));

        spriteTexture.SetPixels(pixels);
        spriteTexture.Apply();
        //------------------------------------------------------------------------------------
        //Setting sprite to Cell
        SpriteRenderer spriteRenderer = cell.AddComponent <SpriteRenderer>() as SpriteRenderer;

        spriteRenderer.sprite           = Sprite.Create(spriteTexture, rec, pivot);
        spriteRenderer.sortingLayerName = "Cells";
        spriteRenderer.material.shader  = glowShader;
        //GlowEffect----------------
        SpriteGlowEffect glowEffect = cell.AddComponent <SpriteGlowEffect>();

        glowEffect.OutlineWidth   = 0;
        glowEffect.AlphaThreshold = 0.01f;
        //-------------------------
        //-----SwapCell Values---------
        SwapCell swapCell = cell.AddComponent <SwapCell>();

        swapCell.SetTruePosition(new Vector2(xCordinate, yCordinate));
        swapCell.SetIsPositionTrue(swapCell.GetTruePosition().Equals(randomCellPosition));
        swapCell.SetMaxGlowWidth(maxGlowWidth);
        swapCell.SetForward(null);
        swapCell.SetBackward(null);
        swapCell.SetLeft(null);
        swapCell.SetRight(null);
        //-----------------------
        //Finally, adding our cell to the list.
        cells.SetValue(cell, index);
    }