Exemple #1
0
    private void ClearSelectionArea()
    {
        unitSelectionArea.gameObject.SetActive(false);

        float bias = new Vector2(mainCamera.scaledPixelHeight, mainCamera.scaledPixelWidth).magnitude * 0.02f;

        Debug.Log(bias);

        if (unitSelectionArea.sizeDelta.magnitude < bias)
        {
            Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());

            if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask))
            {
                return;
            }
            if (!hit.collider.TryGetComponent <Unit>(out Unit unit))
            {
                return;
            }
            if (!unit.hasAuthority)
            {
                return;
            }

            SelectedUnits.Add(unit);

            foreach (Unit selectedUnit in SelectedUnits)
            {
                selectedUnit.Select();
            }

            return;
        }

        Vector2 min = unitSelectionArea.anchoredPosition - (unitSelectionArea.sizeDelta / 2);
        Vector2 max = unitSelectionArea.anchoredPosition + (unitSelectionArea.sizeDelta / 2);

        foreach (Unit unit in player.GetMyUnits())
        {
            if (SelectedUnits.Contains(unit))
            {
                continue;
            }

            Vector3 screenPosition = mainCamera.WorldToScreenPoint(unit.transform.position);

            if (
                screenPosition.x > min.x && screenPosition.x < max.x &&
                screenPosition.y > min.y && screenPosition.y < max.y
                )
            {
                SelectedUnits.Add(unit);
                unit.Select();
            }
        }
    }
    private void ClearSelectionArea()
    {
        unitSelectionArea.gameObject.SetActive(false);

        //if we only click a unit without dragging and as such 0 magnitude on unitSelectionArea
        if (unitSelectionArea.sizeDelta.magnitude == 0)
        {
            Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());

            if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask))
            {
                return;
            }

            if (!hit.collider.TryGetComponent <Unit>(out Unit unit))
            {
                return;
            }

            if (!unit.hasAuthority)
            {
                return;
            }

            SelectedUnits.Add(unit);

            foreach (Unit selectedUnit in SelectedUnits)
            {
                selectedUnit.Select();
            }

            return;
        }


        Vector2 min = unitSelectionArea.anchoredPosition - (unitSelectionArea.sizeDelta / 2);
        Vector2 max = unitSelectionArea.anchoredPosition + (unitSelectionArea.sizeDelta / 2);

        foreach (Unit unit in player.GetMyUnits())
        {
            if (SelectedUnits.Contains(unit))
            {
                continue;
            }

            Vector3 screenPosition = mainCamera.WorldToScreenPoint(unit.transform.position);

            if (screenPosition.x >= min.x &&
                screenPosition.x <= max.x &&
                screenPosition.y >= min.y &&
                screenPosition.y <= max.y)
            {
                SelectedUnits.Add(unit);
                unit.Select();
            }
        }
    }
    private void ClearSelectionArea()
    {
        unitSelectionArea.gameObject.SetActive(false);

        if (unitSelectionArea.sizeDelta.magnitude == 0)
        {
            //Single Click selection. Placed here so that by default if the player doesn't drag this is ran.
            Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());

            if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask))
            {
                return;
            }
            if (!hit.collider.TryGetComponent(out Unit unit))
            {
                return;
            }
            if (!unit.hasAuthority)
            {
                return;
            }

            SelectedUnits.Add(unit);

            foreach (Unit selectedUnit in SelectedUnits)        //select each of the units list
            {
                selectedUnit.Select();
            }

            return;
        }

        //Logic for multiple unit selection.
        Vector2 min = unitSelectionArea.anchoredPosition - (unitSelectionArea.sizeDelta / 2);
        Vector2 max = unitSelectionArea.anchoredPosition + (unitSelectionArea.sizeDelta / 2);

        foreach (Unit unit in player.GetMyUnits())
        {
            //Validates we are only adding a unit 1 time to selected Units list.
            if (SelectedUnits.Contains(unit))
            {
                continue;
            }

            Vector3 screenPosition = mainCamera.WorldToScreenPoint(unit.transform.position);

            //Verifies that the units screenposition is within our selection area
            if (screenPosition.x > min.x && screenPosition.x < max.x && screenPosition.y > min.y &&
                screenPosition.y < max.y)
            {
                SelectedUnits.Add(unit);
                unit.Select();
            }
        }
    }
Exemple #4
0
    private void ClearSelectionArea()
    {
        unitSelectionArea.gameObject.SetActive(false);

        // if size of selection area is 0, only check for selecting one unit
        if (unitSelectionArea.sizeDelta.magnitude == 0)
        {
            Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());

            if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask))
            {
                return;
            }                                                                                     // if raycast hits nothing, exit method

            if (!hit.collider.TryGetComponent <Unit>(out Unit unit))
            {
                return;
            }                                                                   // if gameobject we hit is not a unit, exit method

            if (!unit.hasAuthority)
            {
                return;
            }                                   // if unit is not owned by the player, exit method

            SelectedUnits.Add(unit);

            foreach (Unit selectedUnit in SelectedUnits)
            {
                selectedUnit.Select();
            }

            return;
        }

        // multi-unit selection area check
        Vector2 min = unitSelectionArea.anchoredPosition - (unitSelectionArea.sizeDelta / 2);
        Vector2 max = unitSelectionArea.anchoredPosition + (unitSelectionArea.sizeDelta / 2);

        foreach (Unit unit in player.GetMyUnits())
        {
            if (SelectedUnits.Contains(unit))
            {
                continue;
            }                                                                                // if unit already in list, go onto next iteration of the loop

            Vector3 screenPosition = mainCamera.WorldToScreenPoint(unit.transform.position); // get screen position of unit

            if (screenPosition.x > min.x && screenPosition.x < max.x &&                      // if screen position of unit is within selection box
                screenPosition.y > min.y && screenPosition.y < max.y)
            {
                SelectedUnits.Add(unit); // add unit to list of selected units
                unit.Select();           // select unit
            }
        }
    }
    private void ClearSelectionArea()
    {
        //Disable select area UI element
        unitSelectionArea.gameObject.SetActive(false);

        //TODO
        if (unitSelectionArea.sizeDelta.magnitude == 0)
        {
            //Create raycast
            Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());

            //Return if ray hits nothing
            if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask))
            {
                return;
            }

            //If the hit isnt a unit do nothing
            if (!hit.collider.TryGetComponent <Unit>(out Unit unit))
            {
                return;
            }

            //If player doesn't have authority over unit do nothing
            if (!unit.hasAuthority)
            {
                return;
            }

            //Add selected unit to list
            SelectedUnits.Add(unit);

            //Select all units in the list
            foreach (Unit selectedUnit in SelectedUnits)
            {
                selectedUnit.Select();
            }

            return;
        }

        //TODO
        Vector2 min = unitSelectionArea.anchoredPosition - (unitSelectionArea.sizeDelta / 2);
        Vector2 max = unitSelectionArea.anchoredPosition + (unitSelectionArea.sizeDelta / 2);

        foreach (Unit unit in player.GetMyUnits())
        {
            //TODO
            if (SelectedUnits.Contains(unit))
            {
                continue;
            }

            Vector3 screenPosition = mainCamera.WorldToScreenPoint(unit.transform.position);

            //Checks if unit is inside selection area
            if (screenPosition.x > min.x &&
                screenPosition.x < max.x &&
                screenPosition.y > min.y &&
                screenPosition.y < max.y)
            {
                SelectedUnits.Add(unit);
                unit.Select();
            }
        }
    }
Exemple #6
0
    private void ClearSelectionArea()
    {
        //Debug.Log($"ClearSelectionArea Unit Touch Phase ended");
        unitSelectionArea.gameObject.SetActive(false);

        if (unitSelectionArea.sizeDelta.magnitude == 0)
        {
            Vector2 pos = Input.touchCount > 0 ? Input.GetTouch(0).position : Mouse.current.position.ReadValue();;

            Ray ray = mainCamera.ScreenPointToRay(pos);

            if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask))
            {
                return;
            }

            if (!hit.collider.TryGetComponent <Unit>(out Unit unit))
            {
                return;
            }

            if (!unit.hasAuthority)
            {
                return;
            }

            //Debug.Log($"Add Selected Area Unit {unit}");
            SelectedUnits.Add(unit);

            foreach (Unit selectedUnit in SelectedUnits)
            {
                selectedUnit.Select();
            }

            return;
        }

        Vector2 min = unitSelectionArea.anchoredPosition - (unitSelectionArea.sizeDelta / 2);
        Vector2 max = unitSelectionArea.anchoredPosition + (unitSelectionArea.sizeDelta / 2);

        foreach (Unit unit in player.GetMyUnits())
        {
            if (SelectedUnits.Contains(unit))
            {
                continue;
            }

            Vector3 screenPosition = mainCamera.WorldToScreenPoint(unit.transform.position);

            if (screenPosition.x > min.x &&
                screenPosition.x < max.x &&
                screenPosition.y > min.y &&
                screenPosition.y < max.y)
            {
                //Debug.Log($"Add Selected My Unit {unit}");
                SelectedUnits.Add(unit);
                unit.Select();
            }
        }

        //Debug.Log($"SelectedUnits {SelectedUnits.Count}");
    }
    //----We hit Something, Is it Unit, Is this Unit Belongs to Us.
    private void ClearSelectionArea()
    {
        //When We Realese Mouse Button Deactivate SelectionArea
        unitSelectionArea.gameObject.SetActive(false);

        //Clicking But Not Dragging(Selecting One Unit)
        if (unitSelectionArea.sizeDelta.magnitude == 0)
        {
            Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());

            //If We Do not hit Then Return
            if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask))
            {
                return;
            }

            //If We Do not Hit Unit Then Return
            if (!hit.collider.TryGetComponent <Unit>(out Unit unit))
            {
                return;
            }

            //Is The Unit Our Client Unit
            if (!unit.hasAuthority)
            {
                return;
            }

            //Adding Selected Unit To The List.
            SelectedUnits.Add(unit);

            //Turn On Green Highlight
            foreach (Unit selectedUnit in SelectedUnits)
            {
                selectedUnit.Selected();
            }

            return;
        }

        //Multiselecting Units
        foreach (Unit unit in player.GetMyUnits())
        {
            //If the list contains Already Selected Units Then Continue Next One
            if (SelectedUnits.Contains(unit))
            {
                continue;
            }

            //Raycasting is ScreenSpace But Tanks Are WorldSpace, So To Select Tanks to get this positions
            Vector3 screenPosition = mainCamera.WorldToScreenPoint(unit.transform.position);

            //Calculating Vector Points, Is the Corner Points Greater Than unit X and Y value (It Means units are inside of the SelectionArea)

            Vector2 min = unitSelectionArea.anchoredPosition - (unitSelectionArea.sizeDelta / 2);
            Vector2 max = unitSelectionArea.anchoredPosition + (unitSelectionArea.sizeDelta / 2);

            if (screenPosition.x > min.x &&
                screenPosition.x < max.x &&
                screenPosition.y > min.y &&
                screenPosition.y < max.y)
            {
                SelectedUnits.Add(unit);
                unit.Selected();
            }
        }
    }