示例#1
0
    public void Update()
    {
        if (isActive)
        {
            // If we press the left mouse button, begin selection and remember the location of the mouse
            if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
            {
                isSelecting    = true;
                mousePosition1 = Input.mousePosition;

                foreach (var selectableObject in context.GetPlayerObjectPool().GetPlayerSelectableObjects())
                {
                    Deselect(selectableObject);
                }

                context.GetBuildChoiceUpdater().SetMainObjectForHud(null);
            }
            // If we let go of the left mouse button, end selection
            if (Input.GetMouseButtonUp(0) && isSelecting)
            {
                IPlayerSelectableObject mainObjectForHUD = null;

                foreach (var selectableObject in context.GetPlayerObjectPool().GetPlayerSelectableObjects())
                {
                    if (IsWithinSelectionBounds(selectableObject.GetGameObject()))
                    {
                        selectableObject.Select(true);
                        if (mainObjectForHUD == null || selectableObject.GetSelectionPriority() < mainObjectForHUD.GetSelectionPriority())
                        {
                            mainObjectForHUD = selectableObject;
                        }
                    }
                    else
                    {
                        selectableObject.Select(false);
                    }
                }

                context.GetBuildChoiceUpdater().SetMainObjectForHud(mainObjectForHUD);

                isSelecting = false;
            }

            // Highlight all objects within the selection box
            if (isSelecting)
            {
                foreach (var selectableObject in context.GetPlayerObjectPool().GetPlayerSelectableObjects())
                {
                    if (IsWithinSelectionBounds(selectableObject.GetGameObject()))
                    {
                        Select(selectableObject);
                    }
                    else
                    {
                        Deselect(selectableObject);
                    }
                }
            }
        }
    }
 public bool IsMainObjectForHud(IPlayerSelectableObject playerSelectableObject)
 {
     if (playerSelectableObject == null)
     {
         return(mainObjectForHUD == null);
     }
     return(playerSelectableObject.Equals(mainObjectForHUD));
 }
示例#3
0
 private void Deselect(IPlayerSelectableObject selectableObject)
 {
     selectableObject.DestroySelectionCircle();
 }
示例#4
0
 private void Select(IPlayerSelectableObject selectableObject)
 {
     selectableObject.CreateSelectionCircle(selectionCirclePrefab);
 }
 public void SetMainObjectForHud(IPlayerSelectableObject playerSelectableObject)
 {
     mainObjectForHUD = playerSelectableObject;
     TriggerBuildChoiceUpdate();
 }
示例#6
0
    public void Update()
    {
        if (context.GetPlayerObjectPool() != null)
        {
            List <IPlayerSelectableObject> selectedObjects = context.GetPlayerObjectPool().GetSelectedObjects();

            //TODO I don't know, if this will be the final way of triggering the unit building process.
            //It does not feel right and looks inperfomant. Very likely to be changed. But for now it should work.
            foreach (IPlayerSelectableObject playerSelectableObject in selectedObjects)
            {
                playerSelectableObject.Update();
            }

            //TODO The code below, could take longer to execute. It should be put into another thread.
            if (isActive && Input.GetMouseButtonUp(1))
            {
                Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);

                if (Physics.Raycast(ray, out hitInfo))
                {
                    //Calculate the center of the currently selected units
                    center.Set(0, 0, 0);

                    foreach (IPlayerSelectableObject playerSelectableObject in selectedObjects)
                    {
                        center += playerSelectableObject.GetGameObject().transform.position;
                    }

                    center /= selectedObjects.Count;

                    float maxDistanceToCenter = 0f;

                    foreach (IPlayerSelectableObject playerSelectableObject in selectedObjects)
                    {
                        maxDistanceToCenter = Mathf.Max(Vector3.Distance(center, playerSelectableObject.GetGameObject().transform.position), maxDistanceToCenter);
                    }

                    if (Vector3.Distance(hitInfo.point, center) > maxDistanceToCenter)
                    {
                        //If the click is outside the current formation,
                        //move the selected units, respecting their current offset from the center of the group (so they stay in the same formation)
                        foreach (IPlayerSelectableObject playerSelectableObject in selectedObjects)
                        {
                            Vector3 offset = playerSelectableObject.GetGameObject().transform.position - center;
                            playerSelectableObject.OnSecondaryAction(hitInfo.point + offset, new List <Vector3>());
                            ShowTargetClue(offset);
                        }
                    }
                    else
                    {
                        //If the click is inside the current formation, just move the units to the point (with a small random offset).
                        List <Vector3> offsets = new List <Vector3>();

                        float range = 2.5f;

                        for (int i = 0; i < selectedObjects.Count; i++)
                        {
                            Vector3 vectorToAdd = new Vector3(Random.Range(-range, range), 0, Random.Range(-range, range));

                            while (Utils.CheckIfBlocked(vectorToAdd, 1.0f, offsets))
                            {
                                vectorToAdd.x += Random.Range(-range, range);
                                vectorToAdd.z += Random.Range(-range, range);
                            }
                            offsets.Add(vectorToAdd);
                        }

                        for (int i = 0; i < selectedObjects.Count; i++)
                        {
                            IPlayerSelectableObject playerSelectableObject = selectedObjects[i];
                            Vector3 offset = offsets[i];
                            playerSelectableObject.OnSecondaryAction(hitInfo.point + offset, new List <Vector3>());
                            ShowTargetClue(offset);
                        }
                    }
                }
            }
        }
    }