private void CreateUnitController(VoxelData voxelData, Coordinate coordinate) { voxelData.UnitOrAssetIndex = m_identity; IMatchUnitControllerCli unit = MatchFactoryCli.CreateUnitController(m_voxelMap.Map, coordinate, voxelData.Type, m_playerIndex, m_allAbilities); if (VoxelData.IsControllableUnit(voxelData.Type)) { m_controllableUnitsCount++; } m_idToUnit.Add(m_identity, unit); int radius = unit.DataController.Abilities.VisionRadius; m_voxelMap.Map.ForEachInRadius(coordinate, radius, (observedCell, pos) => { ObserveCell(unit.DataController.ControlledData.Owner, observedCell, pos, coordinate.Weight); }); m_minimap.Spawn(voxelData, coordinate); unchecked { m_identity++; } if (UnitCreated != null) { UnitCreated(unit); } }
private bool RemoveUnitController(bool unitsChanged, IMatchUnitController unitController) { if (m_idToUnit.ContainsKey(unitController.Id)) { if (VoxelData.IsControllableUnit(unitController.DataController.ControlledData.Type)) { m_controllableUnitsCount--; } MatchFactory.DestroyUnitController(unitController); m_idToUnit.Remove(unitController.Id); unitsChanged = true; if (unitController.Assignment != null) { AssignmentsController.RemoveAssignment(unitController, null); } if (unitController.TargetForAssignments != null) { AssignmentsController.RemoveTargetFromAssignments(unitController, null); } if (UnitRemoved != null) { UnitRemoved(unitController); } } return(unitsChanged); }
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 OnBoxSelectionFiltering(object sender, FilteringArgs e) { GameObject go = e.Object; Voxel voxel = go.GetComponentInParent <Voxel>(); if (voxel == null || !VoxelData.IsControllableUnit(voxel.Type) || voxel.Owner != PlayerIndex) { e.Cancel = true; } }
public PlayerStats GetStats(int index) { PlayerStats stats = m_playerStats[index]; long[] units = m_playerControllers[index].Units.OfType <IMatchUnitAssetView>().Select(uav => uav.Id).ToArray(); int count = 0; for (int i = 0; i < units.Length; ++i) { IVoxelDataController dc = GetVoxelDataController(index, units[i]); if (dc != null && VoxelData.IsControllableUnit(dc.ControlledData.Type) && dc.IsAlive) { count++; } } stats.ControllableUnitsCount = count; return(stats); }
private void CreateUnitController(VoxelData voxelData, Coordinate coordinate) { voxelData.UnitOrAssetIndex = m_identity; IMatchUnitController unit = MatchFactory.CreateUnitController(m_engine, coordinate, voxelData.Type, m_playerIndex, m_allAbilities); m_idToUnit.Add(m_identity, unit); if (VoxelData.IsControllableUnit(unit.DataController.ControlledData.Type)) { m_controllableUnitsCount++; } unchecked { m_identity++; } if (UnitCreated != null) { UnitCreated(unit); } }
private void RemoveUnitController(long unitId) { IMatchUnitControllerCli unitController = m_idToUnit[unitId]; if (VoxelData.IsControllableUnit(unitController.Type)) { m_controllableUnitsCount--; } Coordinate coordinate = unitController.DataController.Coordinate; int radius = unitController.DataController.Abilities.VisionRadius; m_voxelMap.Map.ForEachInRadius(coordinate, radius, (ignoredCell, pos) => { IgnoreCell(unitController.DataController.ControlledData.Owner, ignoredCell, pos, coordinate.Weight); }); m_minimap.Die(unitController.DataController.ControlledData, unitController.DataController.Coordinate); m_idToUnit.Remove(unitId); //Does not needed because all nessesary actions and event unsubscription performed in OnVoxelRefReset event handler //MatchFactoryCli.DestroyUnitController(unitController); if (unitController.Assignment != null) { AssignmentsController.RemoveAssignment(unitController, DieCallback); } if (unitController.TargetForAssignments != null) { AssignmentsController.RemoveTargetFromAssignments(unitController, DieCallback); } if (UnitRemoved != null) { UnitRemoved(unitController); } }
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 VoxelData FindClosestTo(int playerIndex, long selectedIndex, long[] units, bool unselectMode) { MapPos mapCursor = m_cameraController.MapCursor; Vector3 selectedPosition; if (selectedIndex == -1 || m_mapCursor != mapCursor) { selectedPosition = m_cameraController.Cursor; } else { selectedPosition = GetUnitPosition(playerIndex, selectedIndex); } float minDistance = float.PositiveInfinity; long closestIndex = -1; VoxelData closestVoxelData = null; Plane[] planes = GeometryUtility.CalculateFrustumPlanes(m_viewport.Camera); for (int i = 0; i < units.Length; ++i) { long unitIndex = units[i]; if (unselectMode) { if (!m_wasSelected.Contains(unitIndex)) { continue; } } else { if (m_wasSelected.Contains(unitIndex)) { continue; } if (unitIndex == selectedIndex && m_mapCursor != mapCursor) { continue; } } IVoxelDataController controller = m_gameState.GetVoxelDataController(playerIndex, unitIndex); Vector3 position = m_map.GetWorldPosition(controller.Coordinate); if (IsVisible(planes, controller.ControlledData.VoxelRef) && VoxelData.IsControllableUnit(controller.ControlledData.Type)) { Vector3 toVector = (position - selectedPosition); float distance = toVector.sqrMagnitude; if (distance < minDistance) { minDistance = distance; closestIndex = unitIndex; closestVoxelData = controller.ControlledData; } } } return(closestVoxelData); }
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 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(); } }