Example #1
0
        private async void OnMouseDown(IUnit[] units)
        {
            // Input lock is handled by the VC since it owns other sub VCs, etc..
            // Ideally, we would lock here and await for the root menu to be closed.
            _unitSelectionHighlighter.HighlightUnits(new[] { units[0] });
            // TODO: This currently only awaits for menu to be open. Should create "ShowAndWaitForAction"
            await _unitMenuViewController.Show(units[0]);

            _unitSelectionHighlighter.ClearHighlights();
        }
        /// <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);
        }