Exemplo n.º 1
0
    private void Update()
    {
        #region Right Clicked
        if (Input.GetMouseButtonDown(1))
        {
            for (int i = 0; i < Game.groups.Count; i++)
            {
                // Remove all idle groups
                if (Game.groups[i].unitGroupTask == UnitGroupTask.Idle)
                {
                    Game.groups.Remove(Game.groups[i]);
                }
            }

            // Add Group from Selected
            if (selectedEntities.Count > 1)
            {
                var units = new List <GameObject>();
                foreach (var go in selectedEntities.Values)
                {
                    go.GetComponent <Unit>().LeaveCurrentGroup(); // Remove old group if any
                    units.Add(go);                                // Add to new group
                }

                if (units.Count > 1)
                {
                    Game.groups.Add(new UnitGroup(units, planetScript.transform));
                }
            }

            // Since we were telling units to leave their old groups, we have to check if any groups have less than 2 members
            // Check if any groups have < 1 members
            for (int i = 0; i < Game.groups.Count; i++)
            {
                Game.groups[i].RemoveGroupIfMemberCountLow();
            }

            // Give Movement Command
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);

            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, camScript.distanceFromPlanetSurface + planetScript.shapeSettings.radius, LayerMask.GetMask("Planets")))
            {
                var selectedGroups = Game.groups.Where(group => group.IsSelected()).ToList();

                if (selectedGroups.Count == 1)
                {
                    selectedGroups[0].MoveToTarget(hit.point);
                }
                else if (selectedGroups.Count > 1)
                {
                    var unitsForNewGroup = new List <GameObject>();

                    foreach (var group in selectedGroups)
                    {
                        // Selected more than 1 group
                        // Merge units selected from other groups into 1 new group
                        for (int i = 0; i < group.units.Count; i++)
                        {
                            var unit = group.units[i].GetComponent <Unit>();

                            if (!unit.selected)
                            {
                                continue;
                            }

                            unit.LeaveCurrentGroup();
                            unitsForNewGroup.Add(group.units[i]);
                        }

                        group.RemoveGroupIfMemberCountLow();
                    }

                    var newGroup = new UnitGroup(unitsForNewGroup, planet.transform);
                    Game.groups.Add(newGroup);
                    newGroup.MoveToTarget(hit.point);
                }
            }
        }
        #endregion

        #region Left Clicked
        if (Input.GetMouseButtonDown(0))
        {
            p1 = Input.mousePosition;
        }
        #endregion

        #region Hold Left Click
        if (Input.GetMouseButton(0))
        {
            if ((p1 - Input.mousePosition).magnitude > 20)
            {
                dragSelect = true;
            }
        }
        #endregion

        #region Left Click Released
        if (Input.GetMouseButtonUp(0))
        {
            if (dragSelect)
            {
                MarqueeSelect();
            }
            else
            {
                SingleSelect();
            }
        }
        #endregion
    }