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 returns the new state the FSM should be if
	///    this state receives a transition and 
	/// </summary>
	public EntityStateID GetOutputState(EntityTransition trans)
	{
		// Check if the map has this transition
		if (map.ContainsKey(trans))
		{
			return map[trans];
		}
		return EntityStateID.NullStateID;
	}
Ejemplo n.º 3
0
	/// <summary>
	/// This method deletes a pair transition-state from this state's map.
	/// If the transition was not inside the state's map, an ERROR message is printed.
	/// </summary>
	public void DeleteTransition(EntityTransition trans)
	{
		// Check for NullTransition
		if (trans == EntityTransition.NullTransition)
		{
			Debug.LogError("FSMState ERROR: NullTransition is not allowed");
			return;
		}
		
		// Check if the pair is inside the map before deleting
		if (map.ContainsKey(trans))
		{
			map.Remove(trans);
			return;
		}
		Debug.LogError("FSMState ERROR: Transition " + trans.ToString() + " passed to " + entityStateID.ToString() + 
		               " was not on the state's transition list");
	}
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()
Ejemplo n.º 5
0
        void PerformTransition(EntityTransition entityTransition)
        {
            Room newRoom = null;
            Room room = entityTransition.Room;
            Entity entity = entityTransition.Entity;

            if (room.IsEntityLeftOfRoom(entity))
            {
                newRoom = GetRoom(room.SeaX - 1, room.SeaY);

                if (newRoom == null)
                {
                    return;
                }

                entity.X = newRoom.Size.X - 2;
            }

            if (room.IsEntityRightOfRoom(entity))
            {
                newRoom = GetRoom(room.SeaX + 1, room.SeaY);

                if (newRoom == null)
                {
                    return;
                }

                entity.X = -entity.Width + 1;
            }

            if (room.IsEntityAboveRoom(entity))
            {
                newRoom = GetRoom(room.SeaX, room.SeaY - 1);

                if (newRoom == null)
                {
                    return;
                }

                entity.Y = -newRoom.Size.Y - 2;
            }

            if (room.IsEntityBelowRoom(entity))
            {
                newRoom = GetRoom(room.SeaX, room.SeaY + 1);

                if (newRoom == null)
                {
                    return;
                }

                entity.Y = -entity.Height + 1;
            }

            if (newRoom != null)
            {
                room.RemoveEntity(entity);
                newRoom.AddEntity(entity);
            }
        }
Ejemplo n.º 6
0
        public void OnLeftRoom(Entity entity, Room room)
        {
            if (room == boat)
            {
                OnLeftBoat(entity);
                return;
            }

            if (entity == diver)
            {
                if (room.IsEntityLeftOfRoom(entity))
                {
                    MakeRoomActive(room.SeaX - 1, room.SeaY);
                    entity.X = room.Size.X - 2;
                }

                if (room.IsEntityRightOfRoom(entity))
                {
                    MakeRoomActive(room.SeaX + 1, room.SeaY);
                    entity.X = -entity.Width + 1;
                }

                if (room.IsEntityBelowRoom(entity))
                {
                    MakeRoomActive(room.SeaX, room.SeaY + 1);
                    entity.Y = -entity.Height + 1;
                }
                if (room.IsEntityAboveRoom(entity))
                {
                    MakeRoomActive(room.SeaX, room.SeaY - 1);
                    entity.Y = room.Size.Y - 2;
                }

                return;
            }

            if (entity.IsTransitionable)
            {
                EntityTransition entityTransistion = new EntityTransition(entity, room);
                entityTransitions.Add(entityTransistion);
            }
        }
Ejemplo n.º 7
0
 public void DoStateTransition(EntityTransition entityTransition)
 {
     fsm.PerformTransition(entityTransition);
 }