/** * Pone a nulo el estado actual. La máquina de estados deja de tener un estado * actual. Si existía un estado actual, invoca a su método onExit(). */ public void ClearCurrentState() { if (_currentState != null) { _currentState.OnExit(); _currentState.enabled = false; } _currentState = null; }
/** * Añade un nuevo estado a la máquina de estados. El estado debe haber sido * creado previamente. El identificador del estado (establecido de algún modo * en su constructor) debe ser correcto y no estar repetido en ningun estado * añadido previamente a la máquina de estados. * * @param state Nuevo estado a añadir. No se hace copia del estado. * @return Cierto si el estado pudo añadirse (no se están repitiendo * identificadores). */ public bool AddState(FiniteStateComponent <EnumClass> newState) { // Comprobamos si el estado se ha instanciado y si no existe ya if (newState == null || _states.ContainsKey(newState.Id)) { return(false); } _states.Add(newState.Id, newState); Debug.Log("Added new state: " + newState.Id.ToString()); return(newState.OnCreate()); }
// member functions (getter/setters) // --------------------------------- /** * Establece un nuevo estado como el actual. Si previamente existía un estado * establecido, se llamará a su método onExit. También se llama al método onEnter * del nuevo estado. * @param state Identificador del nuevo estado a establecer. * @return Cierto si pudo establecerse el estado como actual (existe un estado * con ese identificador). En caso contrario, devuelve falso y no se invocará * a onExit del estado actual. En concreto, devuelve falso si el estado * actual es el mismo que el que se intenta establecer. */ public virtual bool SetCurrentState(EnumClass targetStateId) { if (_currentState != null && _currentState.Id.Equals(targetStateId)) { _currentState.enabled = true; // El estado objetivo es el mismo que ahora. No hacemos nada return(true); } if (!_stopped && _currentState != null) { _currentState.OnExit(); _currentState.enabled = false; // Fix previous state id if (_currentState != null) { if (_currentState.ReplacePreviousStateOnExit()) { Debug.Log("Previous State: " + _previousStateId.ToString()); Debug.Log("State: " + targetStateId.ToString()); _previousStateId = _currentState.Id; } // else Keep current previous state } else { _previousStateId = targetStateId; } } FiniteStateComponent <EnumClass> newState = _states[targetStateId]; if (newState == null) { // No existe un identificador con ese estado. // Se detiene la maquina de estados. return(false); } _currentState = newState; _currentState.enabled = true; _currentState.OnEnter(); return(true); }
// constructors // ------------ /** * Constructor vacío. */ public void Awake() { _states = new Dictionary <EnumClass, FiniteStateComponent <EnumClass> >(); _currentState = null; _stopped = true; }