예제 #1
0
        private void Start()
        {
            LocalPlayerIndex = m_viewport.LocalPlayerIndex;


            m_boxSelector = Dependencies.GameView.GetBoxSelector(LocalPlayerIndex);



            GetViewportAndCamera();
            ReadPlayerCamSettings();
            SetCameraPosition();
            InitCameraPixelRect();
            CreateAndInitVoxelCamera();
            InitPivot();

            int playerIndex = m_gameState.LocalToPlayerIndex(LocalPlayerIndex);
            var units       = m_gameState.GetUnits(playerIndex);

            foreach (long unit in units)
            {
                IVoxelDataController dc = m_gameState.GetVoxelDataController(playerIndex, unit);
                if (VoxelData.IsControllableUnit(dc.ControlledData.Type))
                {
                    MapPivot = dc.Coordinate.ToWeight(GameConstants.MinVoxelActorWeight).MapPos;
                    break;
                }
            }
        }
        private void Select(int myPlayerIndex)
        {
            if (m_selectedTarget != null)
            {
                do
                {
                    m_selectedTarget = m_selectedTarget.Next;
                }while (m_selectedTarget != null &&
                        (m_selectedTarget.VoxelRef == null && !IsTargetForAnyone(m_selectedTarget) || m_unitSelection.IsSelected(myPlayerIndex, myPlayerIndex, m_selectedTarget.UnitOrAssetIndex)));

                if (m_selectedTarget != null)
                {
                    m_previouslySelected = m_selectedTarget;
                    TargetSelectionSelect(myPlayerIndex, m_selectedTarget.Owner, m_selectedTarget.UnitOrAssetIndex);
                }
                else
                {
                    m_targetSelection.ClearSelection(myPlayerIndex);
                    m_isTargetAutoSelectionMode = false;
                }

                return;
            }

            List <int>    allPlayers        = new List <int>();
            List <long[]> allUnitsAndAssets = new List <long[]>();

            for (int i = 0; i < m_gameState.PlayersCount; ++i)
            {
                long[] unitsAndAssets = m_gameState.GetUnits(i).Union(m_gameState.GetAssets(i)).ToArray();
                allUnitsAndAssets.Add(unitsAndAssets);
                allPlayers.Add(i);
            }

            long selectedIndex = -1;


            long closestUnitOrAssetIndex;
            int  closestPlayerIndex;

            FindClosestTo(selectedIndex, allPlayers.ToArray(), allUnitsAndAssets.ToArray(), out closestPlayerIndex, out closestUnitOrAssetIndex);

            if (closestUnitOrAssetIndex >= 0)
            {
                TargetSelectionSelect(myPlayerIndex, closestPlayerIndex, closestUnitOrAssetIndex);

                VoxelData closestData   = null;
                MapPos    closestMapPos = new MapPos();
                GetUnitOrAsset(closestPlayerIndex, closestUnitOrAssetIndex, ref closestData, ref closestMapPos);

                if (closestData == null)
                {
                    Debug.LogError("Wrong index " + closestUnitOrAssetIndex);
                    return;
                }

                const int  dontCare = -1;
                Coordinate coord    = new Coordinate(closestMapPos, closestData.Weight, dontCare);
                if (coord.MapPos != new Coordinate(m_mapCursor, m_cameraController.Weight, 0).ToWeight(closestData.Weight).MapPos)
                {
                    coord = coord.ToWeight(m_cameraController.Weight);
                    m_cameraController.MapPivot = coord.MapPos;
                    m_cameraController.SetVirtualMousePosition(coord, true, false);
                }

                m_mapCursor          = m_cameraController.MapCursor;
                m_selectedTarget     = closestData;
                m_previouslySelected = closestData;
            }
        }
예제 #3
0
        private void Update()
        {
            if (m_gameState.IsActionsMenuOpened(LocalPlayerIndex))
            {
                if (!m_cameraController.IsInputEnabled)
                {
                    m_cameraController.IsInputEnabled = true;
                }
                return;
            }

            if (m_gameState.IsMenuOpened(LocalPlayerIndex))
            {
                if (!m_cameraController.IsInputEnabled)
                {
                    m_cameraController.IsInputEnabled = true;
                }
                return;
            }

            if (m_gameState.IsContextActionInProgress(LocalPlayerIndex))
            {
                if (!m_cameraController.IsInputEnabled)
                {
                    m_cameraController.IsInputEnabled = true;
                }
                return;
            }

            if (m_gameState.IsPaused || m_gameState.IsPauseStateChanging)
            {
                return;
            }

            if (m_gameState.IsReplay)
            {
                return;
            }

            int playerIndex = PlayerIndex;

            m_selectInterval   -= Time.deltaTime;
            m_unselectInterval -= Time.deltaTime;

            bool multiselect = m_inputManager.GetButton(InputAction.RB, LocalPlayerIndex);

            if (m_inputManager.GetButtonDown(InputAction.LMB, LocalPlayerIndex))
            {
                RaycastHit hitInfo;
                if (Physics.Raycast(m_cameraController.Ray, out hitInfo))
                {
                    Voxel voxel = hitInfo.transform.GetComponentInParent <Voxel>();
                    if (voxel != null)
                    {
                        VoxelData data = voxel.VoxelData;
                        if (VoxelData.IsControllableUnit(data.Type) && data.Owner == playerIndex)
                        {
                            m_unitSelection.ClearSelection(playerIndex);
                            m_unitSelection.Select(playerIndex, playerIndex, new[] { data.UnitOrAssetIndex });
                        }
                        else
                        {
                            m_unitSelection.ClearSelection(playerIndex);
                        }
                    }
                    else
                    {
                        m_unitSelection.ClearSelection(playerIndex);
                    }
                }
                else
                {
                    m_unitSelection.ClearSelection(playerIndex);
                }

                if (m_boxSelector != null)
                {
                    m_boxSelector.Activate();
                }
            }

            if (m_inputManager.GetButtonDown(InputAction.LB, LocalPlayerIndex))
            {
                bool select = true;

                if (multiselect)
                {
                    long[] units     = m_gameState.GetUnits(playerIndex).ToArray();
                    long   unitIndex = GetAt(units, m_cameraController.MapCursor);

                    if (m_unitSelection.IsSelected(playerIndex, playerIndex, unitIndex))
                    {
                        m_unitSelection.Unselect(playerIndex, playerIndex, new[] { unitIndex });
                        m_wasSelected.Remove(unitIndex);
                        select = false;
                    }
                }

                if (select)
                {
                    Select(playerIndex, multiselect);
                    m_selectInterval   = 0.3f;
                    m_unselectInterval = float.PositiveInfinity;
                    m_multiselectMode  = true;
                }
                else
                {
                    m_selectInterval   = float.PositiveInfinity;
                    m_unselectInterval = 0.3f;
                }
            }
            else if (m_inputManager.GetButton(InputAction.LB, LocalPlayerIndex))
            {
                if (m_selectInterval <= 0)
                {
                    Select(playerIndex, multiselect);
                    m_selectInterval = 0.2f;
                }

                if (m_unselectInterval <= 0)
                {
                    Unselect(playerIndex);
                    m_unselectInterval = 0.2f;
                }

                m_cameraController.IsInputEnabled = false;
            }
            else if (m_inputManager.GetButtonUp(InputAction.LB, LocalPlayerIndex, false, false))
            {
                m_cameraController.IsInputEnabled = true;
            }

            if (m_inputManager.GetButtonDown(InputAction.RB, LocalPlayerIndex))
            {
                m_multiselectMode = false;
            }
            else if (m_inputManager.GetButtonUp(InputAction.RB, LocalPlayerIndex))
            {
                if (!m_multiselectMode)
                {
                    if (!m_targetSelection.HasSelected(playerIndex))
                    {
                        m_wasSelected.Clear();
                        m_unitSelection.ClearSelection(playerIndex);
                    }
                }
            }
        }
        private void OnCommand(IConsole console, string cmd, params string[] args)
        {
            if (cmd == "unitinfo")
            {
                int    playerIndex = m_gameState.LocalToPlayerIndex(LocalPlayerIndex);
                long[] selection   = m_unitSelection.GetSelection(playerIndex, playerIndex);

                for (int i = 0; i < selection.Length; ++i)
                {
                    long unitIndex = selection[i];
                    IVoxelDataController dataController = m_gameState.GetVoxelDataController(playerIndex, unitIndex);

                    m_console.Echo(string.Format("unit = {0}, type = {1}",
                                                 unitIndex,
                                                 dataController.ControlledData.Type));

                    m_console.Echo(string.Format("coord = {0}", dataController.Coordinate));
                    m_console.Echo(string.Format("health = {0}", dataController.ControlledData.Health));
                    m_console.Echo("----------------------------------------------------------------------------");
                }
            }
            else if (cmd == "playerinfo")
            {
                int         playerIndex = m_gameState.LocalToPlayerIndex(LocalPlayerIndex);
                PlayerStats playerStats = m_gameState.GetStats(playerIndex);
                Player      player      = m_gameState.GetPlayer(playerIndex);

                m_console.Echo(string.Format("player index = {0} ", playerIndex));
                m_console.Echo(string.Format("player id = {0} ", player.Id));
                m_console.Echo(string.Format("player name = {0} ", player.Name));
                m_console.Echo(string.Format("bot type = {0} ", player.BotType));
                m_console.Echo(string.Format("ctrl units count = {0} ", playerStats.ControllableUnitsCount));
                m_console.Echo(string.Format("is in room = {0} ", playerStats.IsInRoom));

                var unitsByType = m_gameState.GetUnits(playerIndex).Select(unitIndex => m_gameState.GetVoxelDataController(playerIndex, unitIndex)).GroupBy(unit => unit.ControlledData.Type);
                foreach (var unitGroup in unitsByType)
                {
                    m_console.Echo(string.Format("units of type {0} = {1} ", unitGroup.Key, unitGroup.Count()));
                }

                m_console.Echo("----------------------------------------------------------------------------");
            }
            else if (cmd == "quit")
            {
                #if UNITY_EDITOR
                UnityEditor.EditorApplication.isPlaying = false;
                #endif

                Application.Quit();
            }
            else
            {
                cmd = cmd.ToLower();
                cmd = char.ToUpper(cmd[0]) + cmd.Substring(1);
                if (Enum.GetNames(typeof(PlayerUnitConsoleCmd)).Contains(cmd))
                {
                    IPlayerUnitController unitController = Dependencies.GameView.GetUnitController(LocalPlayerIndex);
                    unitController.SubmitConsoleCommand((PlayerUnitConsoleCmd)Enum.Parse(typeof(PlayerUnitConsoleCmd), cmd), args, console);
                }
            }
        }