/// <summary> /// Called on start and call's OnInitialize. /// </summary> private void Initialize() { m_states = new Dictionary <string, State>(); Context = new Context { Owner = GetComponent <Player>(), OwnerAgent = GetComponent <NavMeshAgent>() }; foreach (var state in States) { AddState(state); } OnInitialize(); if (!string.IsNullOrEmpty(StartState)) { Active = true; ChangeState(StartState); } else if (StartState == null) { FSMHelper.Log(LogVerbose, $"Start State is null! Please assign one."); } }
/// <summary> /// Remove state.If needed ?w /// </summary> /// <param name="state"></param> public void RemoveState(string state) { if (m_states.ContainsKey(state)) { m_states.Remove(state); FSMHelper.Log(LogVerbose, $"State removed : {state}"); } }
/// <summary> /// Add a new state that will be taken care of /// </summary> /// <param name="state"></param> public void AddState(State state) { if (!m_states.ContainsKey(state.Name())) { state.Setup(this); m_states.Add(state.Name(), state); FSMHelper.Log(LogVerbose, $"New State added : {state.Name()}"); } }
/// <summary> /// State will be changed immediately. /// </summary> /// <param name="newState"></param> public void ChangeState(string newState) { if (m_states.ContainsKey(newState) && !string.Equals(CurrentState, newState)) { m_previousState = m_currentState; m_previousState?.OnLeave(); m_currentState = m_states[newState]; m_currentState.OnEnter(); CurrentState = newState; FSMHelper.Log(LogVerbose, $"State changed from {m_previousState.Name()} to {m_currentState.Name()}"); } }
/// <summary> /// Update every decision. /// </summary> private void UpdateDecisions() { if (m_currentState.Decisions == null) { return; } foreach (var decision in m_currentState.Decisions) { if (decision.IsValid(Context)) { LastDecisionMade = decision.Name(); FSMHelper.Log(LogVerbose, $"Decisions validated : {LastDecisionMade}"); } } }