Beispiel #1
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();
            }
        };
    }
Beispiel #2
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();
            }
        };
    }