Ejemplo n.º 1
0
	public void AddTransition(EntityTransition trans, EntityStateID id)
	{
		// Check if anyone of the args is invalid
		if (trans == EntityTransition.NullTransition)
		{
			Debug.LogError("FSMState ERROR: NullTransition is not allowed for a real transition");
			return;
		}
		
		if (id == EntityStateID.NullStateID)
		{
			Debug.LogError("FSMState ERROR: NullStateID is not allowed for a real ID");
			return;
		}
		
		// Since this is a Deterministic FSM,
		//   check if the current transition was already inside the map
		if (map.ContainsKey(trans))
		{
			Debug.LogError("FSMState ERROR: State " + entityStateID.ToString() + " already has transition " + trans.ToString() + 
			               "Impossible to assign to another state");
			return;
		}
		
		map.Add(trans, id);
	}
Ejemplo n.º 2
0
	/// <summary>
	/// This method places new states inside the FSM,
	/// or prints an ERROR message if the state was already inside the List.
	/// First state added is also the initial state.
	/// </summary>
	public void AddState(EntityFSMState s)
	{
		// Check for Null reference before deleting
		if (s == null)
		{
			Debug.LogError("FSM ERROR: Null reference is not allowed");
		}
		
		// First State inserted is also the Initial state,
		//   the state the machine is in when the simulation begins
		if (states.Count == 0)
		{
			states.Add(s);
			currentState = s;
			currentStateID = s.ID;
			return;
		}
		
		// Add the state to the List if it's not inside it
		foreach (EntityFSMState state in states)
		{
			if (state.ID == s.ID)
			{
				Debug.LogError("FSM ERROR: Impossible to add state " + s.ID.ToString() + 
				               " because state has already been added");
				return;
			}
		}
		states.Add(s);
	}
Ejemplo n.º 3
0
	/// <summary>
	/// This method delete a state from the FSM List if it exists, 
	///   or prints an ERROR message if the state was not on the List.
	/// </summary>
	public void DeleteState(EntityStateID id)
	{
		// Check for NullState before deleting
		if (id == EntityStateID.NullStateID)
		{
			Debug.LogError("FSM ERROR: NullStateID is not allowed for a real state");
			return;
		}
		
		// Search the List and delete the state if it's inside it
		foreach (EntityFSMState state in states)
		{
			if (state.ID == id)
			{
				states.Remove(state);
				return;
			}
		}
		Debug.LogError("FSM ERROR: Impossible to delete state " + id.ToString() + 
		               ". It was not on the list of states");
	}
Ejemplo n.º 4
0
	/// <summary>
	/// This method tries to change the state the FSM is in based on
	/// the current state and the transition passed. If current state
	///  doesn't have a target state for the transition passed, 
	/// an ERROR message is printed.
	/// </summary>
	public void PerformTransition(EntityTransition trans)
	{
		// Check for NullTransition before changing the current state
		if (trans == EntityTransition.NullTransition)
		{
			Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition");
			return;
		}
		
		// Check if the currentState has the transition passed as argument
		EntityStateID id = currentState.GetOutputState(trans);
		if (id == EntityStateID.NullStateID)
		{
			Debug.LogError("FSM ERROR: State " + currentStateID.ToString() +  " does not have a target state " + 
			               " for transition " + trans.ToString());
			return;
		}
		
		// Update the currentStateID and currentState		
		currentStateID = id;
		foreach (EntityFSMState state in states)
		{
			if (state.ID == currentStateID)
			{
				// Do the post processing of the state before setting the new one
				currentState.DoBeforeLeaving();
				
				currentState = state;
				
				// Reset the state to its desired condition before it can reason or act
				currentState.DoBeforeEntering();
				break;
			}
		}
		
	} // PerformTransition()