public async UniTask <UnitActionPlanResult> PlanBatchedAction(IUnit[] units, UnitAction action) { // Action Plan Event _logger.Log(LoggedFeature.Units, $"Action: {action} planned on {units.Length} units."); _batchedActionHandlers.Where(x => x.ActionType == action).ToList() .ForEach(x => x.HandleActionPlanned(units)); // Subscribe to confirm / cancel. They will fire before Tick if possible. UnitActionPlanResult result = null; var confirmObservables = _batchedActionHandlers.Where(x => x.ActionType == action).Select(x => x.ConfirmActionObservable); confirmObservables.Merge().Subscribe(coords => { HandleActionConfirmed(units, action); result = UnitActionPlanResult.MakeConfirmed(); }).AddTo(_disposables); var cancelObservables = _batchedActionHandlers.Where(x => x.ActionType == action).Select(x => x.CancelActionObservable); cancelObservables.Merge().Subscribe(coords => { HandleActionCanceled(units, action); result = UnitActionPlanResult.MakeCanceled(); }).AddTo(_disposables); // Wait until result is received await UniTask.WaitUntil(() => result != null); return(result); }
public async UniTask <UnitActionPlanResult> PlanAction(IUnit unit, UnitAction action) { // Action Plan Event HandleActionPlanned(unit, action); // Subscribe to confirm / cancel. They will fire before Tick if possible. UnitActionPlanResult result = null; var confirmObservables = _actionHandlers.Where(x => x.ActionType == action).Select(x => x.ConfirmActionObservable); confirmObservables.Merge().Subscribe(coords => { HandleActionConfirmed(unit, action); result = UnitActionPlanResult.MakeConfirmed(); }).AddTo(_disposables); var cancelObservables = _actionHandlers.Where(x => x.ActionType == action).Select(x => x.CancelActionObservable); cancelObservables.Merge().Subscribe(coords => { HandleActionCanceled(unit, action); result = UnitActionPlanResult.MakeCanceled(); }).AddTo(_disposables); // Wait until result is received await UniTask.WaitUntil(() => result != null); return(result); }