public async void RunTrial() { TrialStart?.Invoke(this, _TrialLoop.CurrentIteration); _Status = StatusType.TrialDelay; try { await TrialDelay.RunAsync(CancelToken); if (!CancelToken.IsCancellationRequested) { CurrentState = StartState; ScriptMachineEventArgs e = new ScriptMachineEventArgs(StartState); ScriptStart?.Invoke(this, e); _MazeDataPoint = new MazeDataPoint(CurrentState.Name); _MazeDataPoint.Session = _SessionLoop.CurrentIteration; _MazeDataPoint.Trial = _TrialLoop.CurrentIteration; DataPointReady?.Invoke(this, _MazeDataPoint); CurrentState.Activate(); _Status = StatusType.Running; _StartTime = DateTime.Now; } else { TrialAborted?.Invoke(this, new EventArgs()); } } catch (Exception ex) { TrialAborted?.Invoke(this, new EventArgs()); } }
/// <summary> /// Initializes the state system. /// </summary> private void InitStates() { PatrolState patrol = new PatrolState( this, path, direction, arriveDistance); FollowTargetState followTarget = new FollowTargetState(this); ShootState shoot = new ShootState(this); states.Add(patrol); states.Add(followTarget); states.Add(shoot); // Starts with the patrol state CurrentState = patrol; CurrentState.Activate(); }
/// <summary> /// Changes the AI state. /// </summary> /// <param name="targetState">AI state type</param> /// <returns></returns> public bool PerformTransition(AIStateType targetState) { if (!CurrentState.CheckTransition(targetState)) { Debug.Log("State change failed"); return(false); } bool result = false; AIStateBase state = GetStateByType(targetState); if (state != null) { CurrentState.StartDeactivating(); CurrentState = state; CurrentState.Activate(); result = true; //Debug.Log("Changed state to " + state); } return(result); }
public virtual State <TTrig, TName>?Next(TTrig triggerValue) { State <TTrig, TName>?nextState = CurrentState.Next(triggerValue); if (nextState == null) { return(null); } var enterArgs = new StateEnterArgs <TTrig, TName>() { Trigger = triggerValue, CurrentState = CurrentState, NextState = nextState, Context = _context, }; if (CurrentState != nextState) { var exitArgs = new StateExitArgs <TTrig, TName>() { Trigger = triggerValue, CurrentState = CurrentState, NextState = nextState, }; if (CurrentState.IsAsyncExit) { CurrentState.ExitAsync(exitArgs).ConfigureAwait(false).GetAwaiter().GetResult(); } else { CurrentState.Exit(exitArgs); } } PreviousState = CurrentState; CurrentState = nextState; OnStateChanged?.Invoke(PreviousState, CurrentState, triggerValue); if (CurrentState.IsAsyncEnter) { CurrentState.ActivateAsync(enterArgs).ConfigureAwait(false).GetAwaiter().GetResult(); } else { CurrentState.Activate(enterArgs); } State <TTrig, TName>?toReturn = CurrentState; for (int i = 0; i < _context.NextTriggers.Count; i++) { var next = _context.NextTriggers[i]; _context.NextTriggers.RemoveAt(i); if (next.isAsync) { toReturn = NextAsync(next.trig).ConfigureAwait(false).GetAwaiter().GetResult(); } else { toReturn = Next(next.trig); } } return(toReturn); }