Ejemplo n.º 1
0
    public void EndDrag()
    {
        isDragging   = false;
        initial.code = "";
        initial.variables.Clear();

        if (container != initial.deleteBlock)
        {
            if (isOverContainer && container != originalContainer)      // The block is moved to a new container
            {
                if (isFieldUsed[int.Parse(container.name) - 1] == true) // There is a block already placed in the container
                {
                    Transform replacedBlock = container.transform.GetChild(0);
                    replacedBlock.SetParent(originalContainer.transform, true);
                    replacedBlock.localPosition = new Vector3(90, 20, 0);
                    transform.SetParent(container.transform, true);   // Move the block to new container
                    transform.localPosition = new Vector3(90, 20, 0); // Set the relative position to center of the new container
                    originalContainer.GetComponent <CanvasGroup>().alpha = 1f;
                    originalContainer.GetComponent <Image>().color       = Color.white;
                    container.GetComponent <CanvasGroup>().alpha         = 1f;
                    container.GetComponent <Image>().color = Color.white;
                }
                else // There is no block placed in the new container
                {
                    transform.SetParent(container.transform, true);// Move the block to new container
                    transform.localPosition = new Vector3(90, 20, 0);// Set the relative position to center of the new container
                    container.GetComponent <CanvasGroup>().alpha       = 1f;
                    container.GetComponent <Image>().color             = Color.white;
                    isFieldUsed[int.Parse(originalContainer.name) - 1] = false;
                    isFieldUsed[int.Parse(container.name) - 1]         = true;
                }
            }
            else
            {
                transform.position = startPosition;// Send the block back to the original position
            }
        }
        else
        {
            isFieldUsed[int.Parse(originalContainer.name) - 1] = false;
            Destroy(transform.gameObject);
        }

        // Get rid of the empty containers between blocks
        bool sorted = true;

        for (var i = 1; i < field.Count; i++)
        {
            if ((isFieldUsed[i] == true) && (isFieldUsed[i - 1] == false))
            {
                sorted = false;
            }
        }
        while (sorted == false)
        {
            initial.AutoSort();
            sorted = true;
            for (var i = 1; i < field.Count; i++)
            {
                if ((isFieldUsed[i] == true) && (isFieldUsed[i - 1] == false))
                {
                    sorted = false;
                }
            }
        }
    }