Beispiel #1
0
    private void Update()
    {
        // Left mouse button pressed
        if (Input.GetMouseButtonDown(0))
        {
            mouseStartPosition = Utilities.GetMouseWorldPosition();
            selectionArea.gameObject.SetActive(true);

            // Clears list of selected units
            foreach (Entity entity in selectedEntities)
            {
                entity.SelectEntity(false);
            }
            selectedEntities.Clear();

            // Clears selected phalanx
            if (selectedPhalanx != null)
            {
                selectedPhalanx.SetSelectedStatus(false);
            }
            selectedPhalanx = null;
        }

        // While left mouse button pressed
        if (Input.GetMouseButton(0))
        {
            Vector3 currentMousePosition = Utilities.GetMouseWorldPosition();
            Vector3 lowerLeft            = new Vector3(
                Mathf.Min(mouseStartPosition.x, currentMousePosition.x),
                Mathf.Min(mouseStartPosition.y, currentMousePosition.y)
                );
            Vector3 upperRight = new Vector3(
                Mathf.Max(mouseStartPosition.x, currentMousePosition.x),
                Mathf.Max(mouseStartPosition.y, currentMousePosition.y)
                );

            selectionArea.position   = lowerLeft;
            selectionArea.localScale = upperRight - lowerLeft;
        }


        // Left mouse button released
        if (Input.GetMouseButtonUp(0))
        {
            // Turns off selected area indicator
            selectionArea.gameObject.SetActive(false);

            // Gathers all colliders in selected area and adds all entities to selected entities
            Collider2D[] colliders = Physics2D.OverlapAreaAll(mouseStartPosition, Utilities.GetMouseWorldPosition());
            // TODO: Implement max entity selected limit
            foreach (Collider2D c in colliders)
            {
                Entity e = c.GetComponent <Entity>();
                if (e && e.faction == playerFaction && e.ActivePhalanx == null)
                {
                    selectedEntities.Add(e);
                    e.SelectEntity(true);
                }
            }
        }

        // Right mouse button pressed
        if (Input.GetMouseButtonDown(1))
        {
            Vector3        targetPosition = Utilities.GetMouseWorldPosition();
            List <Vector3> formation;

            // If phalanx is selected
            if (selectedPhalanx != null)
            {
                // formation = fGen.GetPhalanxFormation(targetPosition, formationDirection, selectedPhalanx.links.Count);
                // for (int i = 0; i < selectedPhalanx.links.Count; i++)
                // {
                //     selectedPhalanx.links[i].GetComponent<IMovePosition>().SetMovePosition(formation[i]);
                //     selectedPhalanx.links[i].phalanxDirection = formationDirection;
                // }

                selectedPhalanx.EstablishFormationAt(targetPosition, formationDirection);
            }

            // No phalanx selected
            else
            {
                formation = fGen.GetCircleFormation(targetPosition);
                int targetPositionsIndex = 0;

                foreach (Entity entity in selectedEntities)
                {
                    entity.MoveTo(formation[targetPositionsIndex]);
                    targetPositionsIndex = (targetPositionsIndex + 1) % formation.Count;
                }
            }
        }

        // Clears old formation indication
        HideFormationIndication();

        // Draws new formation indication if there is entities selected
        if (selectedEntities.Count > 0 || selectedPhalanx != null)
        {
            DrawFormationIndication();
        }

        // Rotating formation
        if (Input.GetKeyDown(KeyCode.R))
        {
            int nextVal = (int)formationDirection + 1;
            formationDirection = (Direction)(nextVal % Enum.GetNames(typeof(Direction)).Length);
        }

        // Grouping entities
        if (Input.GetKeyDown(KeyCode.G))
        {
            if (selectedPhalanx != null)
            {
                selectedPhalanx.Disband();
                phalanxes.Remove(selectedPhalanx);
                selectedPhalanx = null;
            }
            else if (selectedEntities.Count > 0)
            {
                // Create new phalanx
                // Add selected entities as phalanx links

                Phalanx newPhalanx = new Phalanx(settings);
                foreach (Entity entity in selectedEntities)
                {
                    entity.SelectEntity(false);
                    newPhalanx.AddEntity(entity);
                    entity.ActivePhalanx = newPhalanx;
                }

                selectedEntities.Clear();

                selectedPhalanx = newPhalanx;
                selectedPhalanx.SetSelectedStatus(true);
                phalanxes.Add(newPhalanx);
            }
        }

        // Cycles selected phalanx
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            if (selectedPhalanx == null)
            {
                if (phalanxes.Count > 0)
                {
                    selectedPhalanx = phalanxes[lastSelectedPhalanxIndex % phalanxes.Count];
                    selectedPhalanx.SetSelectedStatus(true);
                }
            }
            else
            {
                selectedPhalanx.SetSelectedStatus(false);
                lastSelectedPhalanxIndex = (phalanxes.FindIndex(item => item == selectedPhalanx) + 1) % phalanxes.Count;
                selectedPhalanx          = phalanxes[lastSelectedPhalanxIndex];
                selectedPhalanx.SetSelectedStatus(true);
            }
        }
    }