Example #1
0
    void Update()
    {
        if (hasWon)
        {
            return;
        }
        if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
        {
            Vector3      mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit2D hit      = Physics2D.Raycast(mousePos, Vector2.zero);

            if (hit)
            {
                Pixel pixel = hit.collider.GetComponent <Pixel>();
                if (pixel)
                {
                    if (!isDragging)
                    {
                        mouseColor = pixel.color;
                    }
                    isDragging = true;
                    if (mouseColor.Equals(pixel.color))
                    {
                        print(currentColor);
                        if (Input.GetMouseButton(0))
                        {
                            pixel.OnSelect(currentColor);
                        }
                        else if (Input.GetMouseButton(1))
                        {
                            pixel.OnSelect(null);
                        }
                        if (CheckWin())
                        {
                            ScoreManager.AddScore(currentLevel, Mathf.RoundToInt(currentTime));
                        }
                    }
                }

                ColorChooser chooser = hit.collider.gameObject.GetComponent <ColorChooser>();
                if (chooser)
                {
                    if (chosenColor)
                    {
                        chosenColor.StartCoroutine("Shrink");
                    }
                    chooser.StartCoroutine("Grow");
                    currentColor = chooser.color;
                    chosenColor  = chooser;
                }
            }
        }
        else if (!Input.GetMouseButton(0) && !Input.GetMouseButton(1))
        {
            isDragging = false;
            mouseColor = null;
        }

        currentTime += Time.deltaTime;
    }