Ejemplo n.º 1
0
    private EventSelection Select()
    {
        if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
        {
            isSelectionArea = true;
            selectionArea.SetActive(true);
            selectionFrom = Input.mousePosition;
        }

        if (Input.GetMouseButton(0) && isSelectionArea)
        {
            selectionTo = Input.mousePosition;
            var min = new Vector2(Mathf.Min(selectionFrom.x, selectionTo.x), Mathf.Min(selectionFrom.y, selectionTo.y));
            var max = new Vector2(Mathf.Max(selectionFrom.x, selectionTo.x), Mathf.Max(selectionFrom.y, selectionTo.y));
            min.x -= Screen.width / 2.0f;
            max.x -= Screen.width / 2.0f - 1.0f;
            min.y -= Screen.height / 2.0f;
            max.y -= Screen.height / 2.0f - 1.0f;
            selectionAreaTransform.offsetMin = min;
            selectionAreaTransform.offsetMax = max;
        }

        if (Input.GetMouseButtonUp(0) && isSelectionArea)
        {
            Unselect();
            isSelectionArea = false;
            selectionArea.SetActive(false);
            var units = UnitsManager.instance.GetUnitsFromSelectionArea(selectionFrom, selectionTo, 6);
            troop.ChangeTo(units);
            if (units.Count > 0)
            {
                mode = Mode.SelectedTroop;
                MenuManager.instance.UpdateMenu();
                return(EventSelection.Selected);
            }
        }

        if (!EventSystem.current.IsPointerOverGameObject() && !isSelectionArea)
        {
            RaycastHit hit;
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Input.GetMouseButtonUp(0))
            {
                Unselect();
                if (Physics.Raycast(ray, out hit, 100.0f, 1 << 10))                   // Building layer
                {
                    building = hit.transform.GetComponent <Building>();
                    if (building.owner == 0)
                    {
                        mode = Mode.SelectedBuilding;
                        MenuManager.instance.UpdateMenu();
                        return(EventSelection.Selected);
                    }
                }

                if (Physics.Raycast(ray, out hit, 50.0f, 1 << 11))                   // Unit layer
                {
                    var unit = hit.transform.GetComponent <Unit>();
                    if (unit.owner == 0)
                    {
                        var units = new List <Unit>();
                        units.Add(unit);
                        troop.ChangeTo(units);
                        mode = Mode.SelectedTroop;
                        MenuManager.instance.UpdateMenu();
                        return(EventSelection.Selected);
                    }
                }
                return(EventSelection.Unselected);
            }
        }
        return(EventSelection.None);
    }