/// <summary> /// Force the first state if we have an issue /// </summary> void forceState() { if (possibleStates.Count > 0) { currentState = possibleStates.ElementAt(0).Value[0]; currentState.OnEnterState(); } }
/// <summary> /// Leave the old state and enter the new state /// </summary> /// <param name="newState"> The AI script to update to </param> void updateCurrentState(AIStateBase newState) { if (currentState != newState) { currentState.OnExitState(); currentState = newState; currentState.OnEnterState(); } }
public override void OnInspectorGUI() { baseClass = (AIStateBase)target; GUILayout.Space(10f); DrawType(); DrawAction(); // Draw the base base.OnInspectorGUI(); }
/// <summary> /// Get a script based on the current state. /// This will be the first script in the list /// </summary> void assignCurrentState() { if (possibleStates.ContainsKey(currentStateType)) { currentState = possibleStates[currentStateType][0]; currentState.OnEnterState(); } else { if (Logging) { Debug.LogError($"[AIStateMachine.AssignScript()] This state ({currentStateType.ToString()}) is not allowed. It is not present in the dictionary"); } forceState(); return; } }
/// <summary> /// Determine if we need to update the /// current state type we are in and set /// an associated script /// </summary> void UpdateStateInformation() { // Determine the state in heres if (currentState == null) { return; } AIStateType newStateType = currentState.OnUpdate(); if (newStateType != currentStateType) { previousState = currentStateType; AIStateBase newState = null; // Get the script driving our state if (!possibleStates.ContainsKey(newStateType)) { if (Logging) { Debug.LogError($"There are no scripts available for {newStateType.ToString()}; defaulting to idle."); } newStateType = AIStateType.Idle; } newState = GetRandomScriptForState(newStateType); if (newState == null) { if (Logging) { Debug.LogError($"There are no states available for {newStateType.ToString()}"); } return; } updateCurrentState(newState); currentStateType = newStateType; } }