示例#1
0
        private void Select(int playerIndex, bool multiselect)
        {
            long[] units = m_gameState.GetUnits(playerIndex).ToArray();

            long selectedIndex = -1;

            long[] selection = m_unitSelection.GetSelection(playerIndex, playerIndex);
            if (selection.Length > 0)
            {
                selectedIndex = selection[0];
            }

            if (!multiselect)
            {
                if (m_mapCursor != m_cameraController.MapCursor)
                {
                    m_wasSelected.Clear();
                }
            }

            VoxelData unitData = FindClosestTo(PlayerIndex, selectedIndex, units, false);

            if (unitData != null)
            {
                if (m_wasSelected.Count == 0)
                {
                    m_unitSelection.Select(playerIndex, playerIndex, new[] { unitData.UnitOrAssetIndex });
                }
                else
                {
                    m_unitSelection.AddToSelection(playerIndex, playerIndex, new[] { unitData.UnitOrAssetIndex });
                }

                m_wasSelected.Add(unitData.UnitOrAssetIndex);

                if (m_wasSelected.Count == 1)
                {
                    IVoxelDataController dc    = m_gameState.GetVoxelDataController(playerIndex, unitData.UnitOrAssetIndex);
                    Coordinate           coord = dc.Coordinate;
                    coord           = coord.ToWeight(m_cameraController.Weight);
                    coord.Altitude += dc.ControlledData.Height;

                    m_cameraController.MapPivot = coord.MapPos;
                    m_cameraController.SetVirtualMousePosition(coord, true, false);

                    m_mapCursor = m_cameraController.MapCursor;
                }
            }
        }
示例#2
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 OnDiminish()
 {
     SubmitStdCommand(() => new Cmd(CmdCode.Diminish), (playerIndex, unitId) =>
     {
         IVoxelDataController dataController = m_gameState.GetVoxelDataController(playerIndex, unitId);
         if (dataController.CanDiminishImmediate() != CmdResultCode.Success)
         {
             Debug.LogWarning("Can't diminish unit " + unitId);
             return(false);
         }
         return(true);
     });
 }
        private void UpdateState()
        {
            int playerIndex = m_gameState.LocalToPlayerIndex(LocalPlayerIndex);

            long[] selection = m_selection.GetSelection(playerIndex, playerIndex);

            if (selection.Length == 0)
            {
                IsOpen = false;
                return;
            }

            m_cancelBtn.gameObject.SetActive(selection.Length > 0);
            m_attackBtn.gameObject.SetActive(selection.Length > 0);
            m_moveBtn.gameObject.SetActive(selection.Length > 0);
            m_autoBtn.gameObject.SetActive(selection.Length > 0);

            m_bombBtn.gameObject.SetActive(false);
            m_wallBtn.gameObject.SetActive(false);
            m_spawnButton.gameObject.SetActive(false);
            m_growButton.gameObject.SetActive(false);
            m_diminishButton.gameObject.SetActive(false);
            m_splitButton.gameObject.SetActive(false);
            m_split4Button.gameObject.SetActive(false);

            HUDControlBehavior hcbBomb     = m_bombBtn.GetComponent <HUDControlBehavior>();
            HUDControlBehavior hcbWall     = m_wallBtn.GetComponent <HUDControlBehavior>();
            HUDControlBehavior hcbSpawn    = m_spawnButton.GetComponent <HUDControlBehavior>();
            HUDControlBehavior hcbGrow     = m_growButton.GetComponent <HUDControlBehavior>();
            HUDControlBehavior hcbDiminish = m_diminishButton.GetComponent <HUDControlBehavior>();
            HUDControlBehavior hcbSplit    = m_splitButton.GetComponent <HUDControlBehavior>();
            HUDControlBehavior hcbSplit4   = m_split4Button.GetComponent <HUDControlBehavior>();

            hcbBomb.IsDisabled     = true;
            hcbWall.IsDisabled     = true;
            hcbSpawn.IsDisabled    = true;
            hcbGrow.IsDisabled     = true;
            hcbDiminish.IsDisabled = true;
            hcbSplit.IsDisabled    = true;
            hcbSplit4.IsDisabled   = true;

            for (int i = 0; i < selection.Length; ++i)
            {
                IVoxelDataController dc = m_gameState.GetVoxelDataController(playerIndex, selection[i]);
                if (dc != null)
                {
                    CmdResultCode canBomb = dc.CanConvertImmediate((int)KnownVoxelTypes.Bomb);
                    if (canBomb != CmdResultCode.Fail_NotSupported)
                    {
                        m_bombBtn.gameObject.SetActive(true);
                        if (canBomb == CmdResultCode.Success)
                        {
                            hcbBomb.IsDisabled = false;
                        }
                    }

                    CmdResultCode canGround = dc.CanConvertImmediate((int)KnownVoxelTypes.Ground);
                    if (canGround != CmdResultCode.Fail_NotSupported)
                    {
                        m_wallBtn.gameObject.SetActive(true);
                        if (canGround == CmdResultCode.Success)
                        {
                            hcbWall.IsDisabled = false;
                        }
                    }

                    CmdResultCode canSpawner = dc.CanConvertImmediate((int)KnownVoxelTypes.Spawner);
                    if (canSpawner != CmdResultCode.Fail_NotSupported)
                    {
                        m_spawnButton.gameObject.SetActive(true);
                        if (canSpawner == CmdResultCode.Success)
                        {
                            hcbSpawn.IsDisabled = false;
                        }
                    }

                    CmdResultCode canGrow = dc.CanGrowImmediate();
                    if (canGrow != CmdResultCode.Fail_NotSupported)
                    {
                        m_growButton.gameObject.SetActive(true);
                        if (canGrow == CmdResultCode.Success)
                        {
                            hcbGrow.IsDisabled = false;
                        }
                    }

                    CmdResultCode canDiminish = dc.CanDiminishImmediate();
                    if (canDiminish != CmdResultCode.Fail_NotSupported)
                    {
                        m_diminishButton.gameObject.SetActive(true);
                        if (canDiminish == CmdResultCode.Success)
                        {
                            hcbDiminish.IsDisabled = false;
                        }
                    }

                    CmdResultCode canSplit = dc.CanSplitImmediate();
                    if (canSplit != CmdResultCode.Fail_NotSupported)
                    {
                        m_splitButton.gameObject.SetActive(true);
                        if (canSplit == CmdResultCode.Success)
                        {
                            hcbSplit.IsDisabled = false;
                        }
                    }

                    CmdResultCode canSplit4 = dc.CanSplit4Immediate();
                    if (canSplit4 != CmdResultCode.Fail_NotSupported)
                    {
                        m_split4Button.gameObject.SetActive(true);
                        if (canSplit4 == CmdResultCode.Success)
                        {
                            hcbSplit4.IsDisabled = false;
                        }
                    }
                }
            }


            if (m_eventSystem.currentSelectedGameObject != null && m_eventSystem.currentSelectedGameObject.GetComponent <HUDControlBehavior>().IsDisabled)
            {
                m_eventSystem.SetSelectedGameObjectOnLateUpdate(m_autoBtn.gameObject);
            }
        }
        public void Execute(Cmd[] commands, long tick, long lagTicks)
        {
            HashSet <long> deadUnitsHs      = null;
            List <long>    spawnedUnitsList = null;

            for (int c = 0; c < commands.Length; ++c)
            {
                Cmd cmd = commands[c];
                if (cmd == null)
                {
                    continue;
                }

                if (cmd.Code == CmdCode.LeaveRoom)
                {
                    m_isInRoom = false;
                }
                else
                {
                    IMatchUnitControllerCli unitController = m_idToUnit[cmd.UnitIndex];

                    long duration = cmd.Duration;
                    duration -= lagTicks;
                    duration  = Math.Max(0, duration);

                    cmd.Duration = (int)duration;

                    IVoxelDataController dc        = unitController.DataController;
                    Coordinate           prevCoord = dc.Coordinate;

                    int radius = unitController.DataController.Abilities.VisionRadius;

                    unitController.ExecuteCommand(cmd, tick);
                    if (prevCoord != dc.Coordinate)
                    {
                        HandleCoordinateChange(dc, prevCoord, radius);
                    }

                    IList <VoxelDataCellPair> createdVoxels = unitController.CreatedVoxels;
                    if (createdVoxels.Count != 0)
                    {
                        CreateAssets(createdVoxels);
                        for (int i = 0; i < m_otherPlayerControllers.Length; ++i)
                        {
                            m_otherPlayerControllers[i].CreateAssets(createdVoxels);
                        }
                    }

                    IList <VoxelData> eatenOrDestroyed = unitController.EatenOrDestroyedVoxels;
                    if (eatenOrDestroyed.Count != 0)
                    {
                        RemoveAssets(eatenOrDestroyed);
                        for (int i = 0; i < m_otherPlayerControllers.Length; ++i)
                        {
                            m_otherPlayerControllers[i].RemoveAssets(eatenOrDestroyed);
                        }
                    }

                    if (cmd.Code == CmdCode.Composite)
                    {
                        CompositeCmd compositeCmd = (CompositeCmd)cmd;
                        for (int i = 0; i < compositeCmd.Commands.Length; ++i)
                        {
                            spawnedUnitsList = PostprocessCommand(spawnedUnitsList, compositeCmd.Commands[i], unitController);
                        }
                    }
                    else
                    {
                        spawnedUnitsList = PostprocessCommand(spawnedUnitsList, cmd, unitController);
                    }

                    //if (unitController.DataController.ControlledData.Unit.State == VoxelDataState.Dead)
                    if (!unitController.DataController.IsAlive)
                    {
                        //Voxel voxel = unitController.DataController.ControlledData.VoxelRef;
                        //Debug.Assert(voxel == null);//

                        if (deadUnitsHs == null)
                        {
                            deadUnitsHs = new HashSet <long>();
                        }

                        if (deadUnitsHs.Contains(cmd.UnitIndex))
                        {
                            Debug.LogError("Dead unit could not execute commands");
                        }
                        else
                        {
                            deadUnitsHs.Add(cmd.UnitIndex);
                        }
                    }
                }
            }


            if (deadUnitsHs != null)
            {
                long[] deadUnits = deadUnitsHs.ToArray();
                for (int i = 0; i < m_gameState.PlayersCount; ++i)
                {
                    m_selection.Unselect(i, m_playerIndex, deadUnits);
                    m_targetSelection.Unselect(i, m_playerIndex, deadUnits);
                }
                for (int i = 0; i < deadUnits.Length; ++i)
                {
                    long unitId = deadUnits[i];

                    RemoveUnitController(unitId);
                }
            }

            if (m_isLocalPlayer)
            {
                if (spawnedUnitsList != null)
                {
                    long[] spawnedUnits = spawnedUnitsList.ToArray();
                    spawnedUnits = spawnedUnits.Where(
                        u => m_gameState.GetVoxelDataController(m_playerIndex, u) != null &&
                        VoxelData.IsControllableUnit(m_gameState.GetVoxelDataController(m_playerIndex, u).ControlledData.Type)).ToArray();

                    m_selection.AddToSelection(m_playerIndex, m_playerIndex, spawnedUnits);
                }
            }
        }
        private void ProcessRequest(ClientRequest request, Action <ClientRequest> callback)
        {
            Cmd cmd = request.Cmd;

            if (cmd != null)
            {
                if (cmd.Code == CmdCode.GrantBotCtrl)
                {
                    m_bots = new IBotController[m_game.PlayersCount];
                    for (int i = 0; i < m_bots.Length; ++i)
                    {
                        Player player = m_game.GetPlayer(i);
                        if (player.IsActiveBot)
                        {
                            IBotController bot = MatchFactoryCli.CreateBotController(player, m_taskEngines[i], new DefaultStrategy());
                            bot.Init();
                            m_bots[i] = bot;
                        }
                    }
                    callback(request);
                }
                else if (cmd.Code == CmdCode.DenyBotCtrl)
                {
                    m_bots = new IBotController[m_game.PlayersCount];
                    for (int i = 0; i < m_bots.Length; ++i)
                    {
                        IBotController bot = m_bots[i];
                        if (bot != null)
                        {
                            bot.Reset();
                        }
                    }
                    m_bots = null;
                    callback(request);
                }
                else
                {
                    IVoxelDataController dc = m_game.GetVoxelDataController(request.PlayerIndex, cmd.UnitIndex);
                    if (cmd.Code != CmdCode.Move || dc == null)
                    {
                        if (dc == null)
                        {
                            request.Cmd.ErrorCode = CmdResultCode.Fail_NoUnit;
                        }
                        else
                        {
                            request.Cmd.ErrorCode = CmdResultCode.Fail_NotSupported;
                        }

                        SubmitResponse(request);
                    }
                    else
                    {
                        CoordinateCmd coordinateCmd = (CoordinateCmd)cmd;
                        Debug.Assert(coordinateCmd.Coordinates.Length > 1);
                        IPathFinder pathFinder = m_pathFinders[request.PlayerIndex];
#warning PathFinder should igore dataController.ControlledVoxelData
                        pathFinder.Find(cmd.UnitIndex, -1, dc.Clone(), coordinateCmd.Coordinates, (unitIndex, path) =>
                        {
                            coordinateCmd.Coordinates = path;
                            request.Cmd = coordinateCmd;
                            callback(request);
                        }, null);
                    }
                }
            }
        }
        private void Update()
        {
            if (m_gameState.IsActionsMenuOpened(LocalPlayerIndex))
            {
                return;
            }

            if (m_gameState.IsMenuOpened(LocalPlayerIndex))
            {
                return;
            }

            if (m_gameState.IsContextActionInProgress(LocalPlayerIndex))
            {
                return;
            }

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

            int playerIndex = PlayerIndex;

            if (m_mapCursor != m_cameraController.MapCursor)
            {
                m_mapCursor = m_cameraController.MapCursor;


                VoxelData newSelectedTarget = null;
                VoxelData target            = null;
                int       width             = m_map.Map.GetMapSizeWith(GameConstants.MinVoxelActorWeight);
                if (m_mapCursor.Row >= 0 && m_mapCursor.Col >= 0 && m_mapCursor.Row < width && m_mapCursor.Col < width)
                {
                    MapCell cell = m_map.Map.Get(m_mapCursor.Row, m_mapCursor.Col, m_cameraController.Weight);

                    for (int i = 0; i < m_selectedUnitDescriptors.Length; ++i)
                    {
                        SelectionDescriptor descriptor = m_selectedUnitDescriptors[i];
                        bool lowestPossible            = true;
                        cell.GetDefaultTargetFor(descriptor.Type, descriptor.Weight, playerIndex, lowestPossible, out target, playerIndex);
                        if (target != null)
                        {
                            break;
                        }
                    }
                }

                if (target != null && target.VoxelRef != null && target.UnitOrAssetIndex != -1)
                {
                    //Player could not destroy own units (and should not be able to select them as target)
                    if (!VoxelData.IsControllableUnit(target.Type) || target.Owner != playerIndex)
                    {
                        newSelectedTarget = target;
                    }
                }

                if (m_previouslySelected != newSelectedTarget)
                {
                    if (newSelectedTarget == null)
                    {
                        ClearSelection();
                        m_selectedTarget     = null;
                        m_previouslySelected = null;
                    }
                    else
                    {
                        m_previouslySelected = newSelectedTarget;
                        TryEnableTargetAutoSelectionMode();
                        if (m_isTargetAutoSelectionMode)
                        {
                            m_selectedTarget = newSelectedTarget;
                            TargetSelectionSelect(playerIndex, target.Owner, m_selectedTarget.UnitOrAssetIndex);
                        }
                    }
                }
            }
            else
            {
                if (m_selectedTarget != null)
                {
                    IVoxelDataController dc = m_gameState.GetVoxelDataController(m_selectedTarget.Owner, m_selectedTarget.UnitOrAssetIndex);
                    if (dc != null && dc.IsAlive)
                    {
                        Coordinate cursorCoord = new Coordinate(m_cameraController.MapCursor, GameConstants.MinVoxelActorWeight, 0).ToWeight(dc.ControlledData.Weight);
                        if (cursorCoord.MapPos != dc.Coordinate.MapPos)
                        {
                            Coordinate coord = dc.Coordinate.ToWeight(GameConstants.MinVoxelActorWeight);
                            m_cameraController.MapPivot = coord.MapPos;
                            m_cameraController.SetVirtualMousePosition(coord, true, false);
                            m_mapCursor = m_cameraController.MapCursor;
                        }
                    }
                }
            }

            if (m_inputManager.GetButtonDown(InputAction.X, LocalPlayerIndex))
            {
                bool hasSelected = m_targetSelection.HasSelected(playerIndex);

                Select(playerIndex);

                if (!hasSelected)
                {
                    m_isTargetAutoSelectionMode = true;
                }
            }
            else if (m_inputManager.GetButtonUp(InputAction.RB, LocalPlayerIndex))
            {
                m_isTargetAutoSelectionMode = false;

                ClearSelection();
            }
        }
示例#8
0
        private void Explode(int targetPlayerIndex, long targetIndex, Coordinate to)
        {
            VoxelData            explodeData;
            IVoxelDataController dataController = m_game.GetVoxelDataController(targetPlayerIndex, targetIndex);

            if (dataController != null)
            {
                explodeData = dataController.ControlledData;
            }
            else
            {
                MatchAssetCli asset = m_game.GetAsset(targetPlayerIndex, targetIndex);
                explodeData = asset.VoxelData;
            }

            CmdResultCode noFail = m_dataController.Explode(to, explodeData, EatOrDestroyCallback, ExpandCallback, ExplodeCallback);

            if (noFail != CmdResultCode.Success)
            {
                throw new InvalidOperationException();
            }
            VoxelData voxelData = m_dataController.ControlledData;
            MapPos    mapPos    = m_dataController.Coordinate.MapPos;
            int       weight    = m_dataController.Coordinate.Weight;

            Debug.Assert(weight == m_dataController.ControlledData.Weight);

            // AcquireReleaseVisibility(voxelData, mapPos, weight);

            if (m_controlledVoxel == null)
            {
                CollapseEatExpandClear(m_currentTick);
            }
            else
            {
                Collapse(m_currentTick, 0);
                //this code moved above m_controlldeVoxel.Kill to prevent cleanup of eatables
                EatAndExpand(m_currentTick, 0);

                //if (m_controlledVoxel != null)
                //{
                //    m_controlledVoxel.Explode(0);
                //}

                if (explodeData.VoxelRef != null)
                {
                    //explodeData.VoxelRef.Explode(0);

                    VoxelData next = explodeData.Next;
                    while (next != null)
                    {
                        if (next.VoxelRef != null)
                        {
                            next.VoxelRef.ChangeAltitude(next.VoxelRef.Altitude, next.Altitude, m_currentCmdDuration);
                        }

                        next = next.Next;
                    }
                }
            }

            Explode(0);
        }
        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);
                }
            }
        }