public UniTask Show(IUnit unit) { var unitCoords = _gridUnitManager.GetUnitCoords(unit); if (unitCoords == null) { var msg = $"Unit not in tile: {unit}"; _logger.LogError(LoggedFeature.Units, msg); return(UniTask.FromException(new Exception(msg))); } // Acquire input lock. _lockToken = _inputLock.Lock(); // Set selected unit and events _unit = unit; _moveUnitButton.onClick.AddListener(HandleMoveUnitButtonPressed); _removeUnitButton.onClick.AddListener(HandleRemoveUnitButtonPressed); _rotateUnitButton.onClick.AddListener(HandleRotateUnitPressed); _cancelButton.onClick.AddListener(HandleCancelButtonPressed); // Show radial menu var worldPosition = _gridPositionCalculator.GetTileCenterWorldPosition(unitCoords.Value); _menuScreenPositon = _camera.WorldToScreenPoint(worldPosition); return(_radialMenu.Show(_menuScreenPositon)); }
public void HandleActionConfirmed(IUnit unit) { // This can be checked (throw exception if null) because our stream guarantees that they are not. IntVector2 unitCoords = _gridUnitManager.GetUnitCoords(unit).GetValueChecked(); IntVector2?mouseCoords = _gridInputManager.TileAtMousePosition; if (mouseCoords == null) { // TODO: Maybe we want to commit the tile the unit is at instead? HandleActionCanceled(unit); return; } CommitUnitMovement(unit, mouseCoords.Value - unitCoords); _disposable?.Dispose(); _disposable = null; }
public IObservable <UniRx.Unit> Run() { _unit = _unitRegistry.GetUnit(_data.unitId); if (_unit == null) { string errorMsg = $"Unit not found: {_data.unitId}"; _logger.LogError(LoggedFeature.Units, errorMsg); return(Observable.Throw <UniRx.Unit>(new IndexOutOfRangeException(errorMsg))); } _tileCoords = _gridUnitManager.GetUnitCoords(_unit); // Only despawn the unit. This does not remove pet units. _logger.Log(LoggedFeature.Units, "Despawning: {0}", _unit.UnitId); _unitPool.Despawn(_unit.UnitId); _gridUnitManager.RemoveUnit(_unit); return(Observable.ReturnUnit()); }
public void HandleActionPlanned(IUnit unit) { var coords = _gridUnitManager.GetUnitCoords(unit); if (coords == null) { _logger.LogError(LoggedFeature.Units, "Unit not in tile: {0}", unit); return; } var baseSpeedTiles = _gridPositionCalculator.GetTilesAtDistance(coords.Value, unit.UnitData.UnitStats.speed / 5); foreach (var tileCoords in baseSpeedTiles) { _gridCellHighlightPool.Spawn(tileCoords, new Color(0, 1, 0, 0.6f)); _validTiles.Add(tileCoords); } }
public void HandleActionPlanned(IUnit unit) { if (!_gridInputManager.TileAtMousePosition.HasValue) { return; } IntVector2?unitCoords = _gridUnitManager.GetUnitCoords(unit); if (unitCoords == null) { _logger.LogError(LoggedFeature.Units, "UnitCoords not found for unit: {0}", unit); return; } IntVector2 moveDistance = _gridInputManager.TileAtMousePosition.Value - unitCoords.Value; _commandQueue.Enqueue <MoveUnitCommand, MoveUnitData>(new MoveUnitData(unit.UnitId, moveDistance), CommandSource.Game); }
/// <summary> /// Shows the batch selection UI and highlights the selected units. /// Returns a task that is completed once the UI has closed or the batch action is complete. /// </summary> /// <param name="units"></param> /// <returns></returns> public async UniTask ShowAndWaitForAction(IUnit[] units) { // Show the menu var unitCoords = _gridUnitManager.GetUnitCoords(units[0]); if (unitCoords == null) { var msg = $"Unit not in tile: {units[0]}"; _logger.LogError(LoggedFeature.Units, msg); throw new Exception(msg); } _selectedUnits = units; var worldPosition = _gridPositionCalculator.GetTileCenterWorldPosition(unitCoords.Value); _radialMenu.Show(_camera.WorldToScreenPoint(worldPosition)); // Add listeners _removeUnitButton.onClick.AddListener(HandleRemoveUnitPressed); _rotateUnitButton.onClick.AddListener(HandleRotateUnitPressed); _cancelSelectionButton.onClick.AddListener(HandleCancelSelectionPressed); // Highlights _unitSelectionHighlighter.HighlightUnits(units); // Mouse Up / Down streams var mouseUpStream = Observable.EveryUpdate() .Where(_ => Input.GetMouseButtonUp(0)); var mouseDownStream = Observable.EveryUpdate() .Where(_ => Input.GetMouseButtonDown(0)) .Where(_ => _gridUnitInputManager.UnitsAtMousePosition.Length > 0) .Where(_ => units.Intersect(_gridUnitInputManager.UnitsAtMousePosition).Any()) .First(); _observer = mouseDownStream.Select(_ => mouseUpStream).Subscribe(_ => { HandleUnitMouseDown(units); }); await UniTask.WaitUntil(() => _selectedUnits == null); }
private IObservable <UniRx.Unit> DoMoveUnit(UnitId unitId, IntVector2 moveDistance) { IUnit unit = _unitRegistry.GetUnit(unitId); if (unit == null) { var errorMsg = $"Unit not found in registry: {unitId}"; _logger.LogError(LoggedFeature.Units, errorMsg); return(Observable.Throw <UniRx.Unit>(new Exception(errorMsg))); } IntVector2?previousCoords = _gridUnitManager.GetUnitCoords(unit); if (previousCoords == null) { var errorMsg = $"Unit position not found: {unitId}"; _logger.LogError(LoggedFeature.Units, errorMsg); return(Observable.Throw <UniRx.Unit>(new Exception(errorMsg))); } _gridUnitManager.PlaceUnitAtTile(unit, moveDistance + previousCoords.Value); return(Observable.ReturnUnit()); }