Exemple #1
0
    private void createIdleState()
    {
        idleState = (FiniteStateMachine, gameObj) =>
        {
            //Planning

            //Gets world state
            HashSet <KeyValuePair <string, object> > worldState = dataProvider.getWorldState();
            HashSet <KeyValuePair <string, object> > goal       = dataProvider.createGoalState();

            //Plan
            Queue <GOAPAction> plan = planner.plan(gameObject, actionsAvailable, worldState, goal);
            if (plan != null)
            {
                //Already Have Plan
                currentActions = plan;
                dataProvider.planFound(goal, plan);

                FiniteStateMachine.Pop(); //switch to PerformAction
                FiniteStateMachine.Push(performActionState);
            }
            else
            {
                //No Plan
                dataProvider.planFailed(goal);
                FiniteStateMachine.Pop(); //switch to idle
                FiniteStateMachine.Push(idleState);
            }
        };
    }
Exemple #2
0
    /// <summary>
    /// Create the idle state.
    /// </summary>
    private void CreateIdleState()
    {
        m_IdleState = (fsm, gameObject) =>
        {
            // Check the world state.
            HashSet <KeyValuePair <string, object> > worldState = m_DataProvider.GetWorldState();

            // Check the current goals.
            HashSet <KeyValuePair <string, object> > goal = m_DataProvider.CreateWorldGoal();

            // Create a plan to fulfill the agent's goal world state.
            Queue <GOAPAction> plan = m_Planner.Plan(gameObject, m_AvaliableActions, worldState, goal);

            // If a valid plan was created, begin enacting on that plan.
            if (plan != null)
            {
                m_CurrentActions = plan;
                m_DataProvider.PlanFound(goal, plan);

                fsm.RemoveState();
                fsm.AddState(m_PerformActionState);
            }
            // Else, no valid plan could be created, say plan failed and try again.
            else
            {
                m_DataProvider.PlanFailed(goal);
                fsm.RemoveState();
                fsm.AddState(m_IdleState);
            }
        };
    }
		public void AddState (FSMState newState) {
			foreach (FSMState state in _states) {
				if (state == newState || state.name.Equals(newState.name)) {
					return ;
				}
			}

			_states.Add(newState);
		}
Exemple #4
0
    private void createPerformActionState()
    {
        performActionState = (FiniteStateMachine, gameObj) =>
        {
            //performs action

            if (!hasActionPlan())
            {
                //no actions
                FiniteStateMachine.Pop();
                FiniteStateMachine.Push(idleState);
                dataProvider.actionsFinished();
                return;
            }

            GOAPAction action = currentActions.Peek();
            if (action.isDone())
            {
                //action done
                currentActions.Dequeue();
            }

            if (hasActionPlan())
            {
                //perform next action
                action = currentActions.Peek();
                bool inRange = action.mustBeInRange() ? action.isInRange() : true;

                if (inRange)
                {
                    //in range
                    bool success = action.perform(gameObj);

                    if (!success)
                    {
                        //action failed
                        FiniteStateMachine.Pop();
                        FiniteStateMachine.Push(idleState);
                        dataProvider.planAborted(action);
                    }
                }
                else
                {
                    //move there first
                    FiniteStateMachine.Push(moveState);
                }
            }
            else
            {
                //no actions left
                FiniteStateMachine.Pop();
                FiniteStateMachine.Push(idleState);
                dataProvider.actionsFinished();
            }
        };
    }
Exemple #5
0
    /// <summary>
    /// Create the perform action state.
    /// </summary>
    private void CreatePerformActionState()
    {
        m_PerformActionState = (fsm, gameObject) =>
        {
            // If the agent has no plan, go to the idle state and say we are doing nothing.
            if (!HasActionPlan())
            {
                fsm.RemoveState();
                fsm.AddState(m_IdleState);
                m_DataProvider.ActionsFinished();
                return;
            }

            GOAPAction action = m_CurrentActions.Peek();

            if (action.IsDone())
            {
                m_CurrentActions.Dequeue();
            }

            // If the agent has a plan.
            if (HasActionPlan())
            {
                action = m_CurrentActions.Peek();
                // Check if in range of action.
                bool inRange = action.RequiresInRange() ? action.IsInRange() : true;

                // If we are in range for the action.
                if (inRange)
                {
                    // Check if the agent was able to perform the action.
                    bool success = action.Perform(gameObject);

                    // If not, abort plan and go back to idle.
                    if (!success)
                    {
                        fsm.RemoveState();
                        fsm.AddState(m_IdleState);
                        m_DataProvider.PlanAborted(action);
                    }
                }
                // If not in range, start moving towards target.
                else
                {
                    fsm.AddState(m_MoveToState);
                }
            }
        };
    }
		public void ChangeState (string toState) {
			FSMState stateToChange = null;
			foreach (FSMState state in _states) {
				if (state.name.Equals(toState)) {
					stateToChange = state;
				}
			}

			if (stateToChange != null) {
				_currentState.Exit();
				_currentState = stateToChange;
				_currentState.Enter();
			}
			else {
				Debug.LogError("Invalid State name: " + toState);
			}
		}
Exemple #7
0
    private void createMoveToState()
    {
        moveState = (FiniteStateMachine, gameObj) =>
        {
            //move gameobject

            GOAPAction action = currentActions.Peek();
            if (action.mustBeInRange() && action.target == null)
            {
                FiniteStateMachine.Pop(); //move
                FiniteStateMachine.Pop(); //perform
                FiniteStateMachine.Push(idleState);
                return;
            }

            //agent moves itself
            if (dataProvider.moveAgent(action))
            {
                FiniteStateMachine.Pop();
            }

            //if (movable == null)
            //{
            //    FiniteStateMachine.Pop(); //move
            //    FiniteStateMachine.Pop(); //perform
            //    FiniteStateMachine.Push(idleState);
            //    return;
            //}

            //Variables
            var thisEnemy = gameObj.GetComponent <Enemy>();

            Rigidbody2D rb = gameObj.GetComponent <Rigidbody2D>();
            rb.velocity = new Vector2(Mathf.MoveTowards(rb.velocity.x, Mathf.Sign(action.target.transform.position.x - rb.position.x) * thisEnemy.maxSpeed, thisEnemy.runForce * Time.deltaTime), rb.velocity.y);

            if (gameObj.transform.position.Equals(action.target.transform.position.x))
            {
                //at target position
                action.setInRange(true);
                FiniteStateMachine.Pop();
            }
        };
    }
Exemple #8
0
    /// <summary>
    /// Create the move state.
    /// </summary>
    private void CreateMoveToState()
    {
        m_MoveToState = (fsm, gameObject) =>
        {
            GOAPAction action = m_CurrentActions.Peek();
            // If the current actions requires us to be in range of a target but we couldn't find a target, go to the idle state.
            if (action.RequiresInRange() && action.GetTarget() == null)
            {
                fsm.RemoveState();
                fsm.RemoveState();
                fsm.AddState(m_IdleState);
                return;
            }

            // If the agent has reached the target of their current action, move to the next state.
            if (m_DataProvider.MoveAgent(action))
            {
                fsm.RemoveState();
            }
        };
    }
Exemple #9
0
 public void RemoveState(FSMState oldState)
 {
     _states.Remove(oldState);
 }
		public void RemoveState (FSMState oldState) {
			_states.Remove (oldState);
		}
Exemple #11
0
 public void SwitchState(FSMState toState)
 {
     SwitchState(toState.name);
 }
Exemple #12
0
 public void Start(FSMState state)
 {
     Start(state.name);
 }
Exemple #13
0
 public FSMTranslation(FSMState fromState, FSMState toState, System.Action translationCallBack)
 {
     this.fromState           = fromState;
     this.toState             = toState;
     this.translationCallBack = translationCallBack;
 }