Ejemplo n.º 1
0
    private void handleTileSwap(ObjectDragTest a, ObjectDragTest b)
    {
        doingSwap = true;
        translationImageStartPositions = getCurrentImagePositions();
        translationStartTime           = Time.time;
        doImageTranslations            = true;

        //swap positions (only grid positions)
        a.swapPositionsWith(b);
        //go through connections
        setConnectionRoot(a, a);
        setConnectionRoot(b, b);

        ObjectDragTest[] allObjects = FindObjectsOfType <ObjectDragTest> ();
        //if number of connections is zero swap back
        if (allObjects.Count(obj => obj.Connections.Count + 1 >= minNumberOfConnections) == 0 ||
            a.TileType == b.TileType)
        {
            SoundController.OnTileSwitchFail();

            //swap back
            a.swapPositionsWith(b);
            allObjects.ToList().ForEach(obj => obj.ResetConnections());
        }
        else
        {
            SoundController.OnTileSwitchSuccess();
        }

        OnTranslationEnd += handleSwapTranslationEnd;
    }
Ejemplo n.º 2
0
    public void AddConnection(ObjectDragTest obj)
    {
        if (Connections == null)
        {
            Connections = new List <ObjectDragTest> ();
        }

        obj.IsConnected = true;
        IsConnected     = true;
        Connections.Add(obj);
    }
Ejemplo n.º 3
0
    private void setConnectionRoot(ObjectDragTest a, ObjectDragTest root)
    {
        for (int i = 0; i < 4; i++)
        {
            ObjectDragTest connection = a.getConnectingObj(i);

            if (connection != null && !connection.IsConnected && connection.TileType == a.TileType)
            {
                root.AddConnection(connection);
                setConnectionRoot(connection, root);
            }
        }
    }
Ejemplo n.º 4
0
    public void swapPositionsWith(ObjectDragTest draggedObject)
    {
        Transform swappedParent         = draggedObject.transform.parent;
        int       swappedSibiliingIndex = draggedObject.transform.GetSiblingIndex();

        Transform currentParent       = transform.parent;
        int       currentSiblingIndex = transform.GetSiblingIndex();

        transform.SetParent(swappedParent);
        transform.SetSiblingIndex(swappedSibiliingIndex);

        draggedObject.transform.SetParent(currentParent);
        draggedObject.transform.SetSiblingIndex(currentSiblingIndex);
    }
Ejemplo n.º 5
0
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (!isBeingDragged)
        {
            return;
        }

        ObjectDragTest dragObj = other.gameObject.GetComponent <ObjectDragTest> ();

        if (dragObj)
        {
            swappableObject   = dragObj;
            swappingWithIndex = dragObj.RowInColmn;
            //			Debug.Log(swappingWithIndex);
        }
    }
Ejemplo n.º 6
0
    private void SetInitialGridPositions()
    {
        //using the pre established positions from prefab, record initial positions into grid
        gridImagePositions = new Vector2[ColumnLength, gridColumns[0].childCount];
        int    tileTypeMax = Enum.GetValues(typeof(TileType)).Length;
        Random ran         = new Random();;

        for (int i = 0; i < ColumnLength; i++)
        {
            for (int j = 0; j < gridColumns[0].childCount; j++)
            {
                ObjectDragTest dragableObj = gridColumns[i].GetChild(j).GetComponent <ObjectDragTest> ();
                dragableObj.OnSwap  += handleTileSwap;
                dragableObj.TileType = (TileType)ran.Next(0, tileTypeMax);

                Vector3 pos = gridColumns[i].GetChild(j).position;

                gridImagePositions[i, j] = new Vector2(pos.x, pos.y);
            }
        }
    }
Ejemplo n.º 7
0
 private void OnTriggerExit2D(Collider2D other)
 {
     swappableObject = null;
 }