Example #1
0
    void MouseUpdate()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //first frame where left mouse button is clicked
            MouseDown(ref mouseSelection);
        }
        else if (Input.GetMouseButton(0))
        {
            //subseqpent frames where left mouse button is still held down
            MouseDrag(ref mouseSelection);

            //check if mouse over city
            var overCity = FindOverCity();
            if (overCity != null) //mouse dragged over city while dragging something
            {
                if (selectedCity == overCity)
                {
                    return;
                }
                selectedCity = overCity;
                Debug.Log("2Collided with: " + selectedCity.name);
                var sr = selectedCity.GetComponent <SpriteRenderer>();
                if (pawns.CanMoveToCity(mouseSelection, selectedCity) == true)
                {
                    sr.color = new Color(.1f, 1f, .1f, 1f); //bright green
                }
                else
                {
                    sr.color = new Color(1f, .1f, .1f, 1f); //bright red
                }
            }
            else
            {
                //pawn not collided with city anymore
                DeselectCity();
            }
        }
        else //if (Input.GetMouseButtonUp(0))
        {
            if (mouseSelection != null && selectedCity != null &&
                pawns.CanMoveToCity(mouseSelection, selectedCity) == true)
            {
                pawns.MoveToCity(mouseSelection, selectedCity);
            }
            DeselectCity();             //mouse up when over a city
            //clean up
            Cursor.visible = true;
            firstClicked   = true;
            mouseSelection = null;
        }
    }