/// <summary> /// Send OnStateExit notification to previous state. /// </summary> private void NotifyOnStateExit() { string prev = previousState.GetType().ToString(); string active = currentState.GetType().ToString(); IAiState state = GetComponent(prev) as IAiState; state.OnStateExit(prev, active); }
/// <summary> /// Start this instance. /// </summary> void Start() { // Get all AI states from this gameobject IAiState[] states = GetComponents <IAiState>(); if (states.Length > 0) { foreach (IAiState state in states) { // Add state to list aiStates.Add(state); } if (defaultState != null) { // Set active and previous states as default state previousState = currentState = GetComponent(defaultState) as IAiState; if (currentState != null) { // Go to active state ChangeState(currentState.GetType().ToString()); } else { Debug.LogError("Incorrect default AI state " + defaultState); } } else { Debug.LogError("AI have no default state"); } } else { Debug.LogError("No AI states found"); } }
/// <summary> /// Turn on active AI state component. /// </summary> private void EnableNewState() { MonoBehaviour state = GetComponent(currentState.GetType().ToString()) as MonoBehaviour; state.enabled = true; }