Ejemplo n.º 1
0
    public bool moveAgent(GOAPAction nextAction)
    {
        Vector3 direction      = nextAction.target.transform.position - transform.position;
        bool    right          = nextAction.target.transform.position.x > transform.position.x;
        var     spriteRenderer = GetComponent <SpriteRenderer>();

        if (right)
        {
            spriteRenderer.flipX = true;
        }
        else
        {
            spriteRenderer.flipX = false;
        }
        transform.Translate(direction * Time.deltaTime);
        float distance = Vector3.Distance(transform.position, nextAction.target.transform.position);

        if (distance < minTargetDistance)
        {
            nextAction.setInRange(true);
            return(true);
        }
        else
        {
            nextAction.setInRange(false);
            return(false);
        }
    }
Ejemplo n.º 2
0
    public virtual bool moveAgent(GOAPAction nextAction)
    {
        float dist = Vector3.Distance(transform.position, nextAction.target.transform.position);

        if (dist < aggroDist)
        {
            Vector3 moveDirection = player.transform.position - transform.position;

            setSpeed(speed);

            if (initialSpeed < terminalSpeed)
            {
                initialSpeed += acceleration;
            }

            Vector3 newPosition = moveDirection * initialSpeed * Time.deltaTime;
            transform.position += newPosition;
        }
        if (dist <= minDist)
        {
            nextAction.setInRange(true);
            return(true);
        }
        else
        {
            return(false);
        }
    }
Ejemplo n.º 3
0
    //gradually move the enemy towards the target location. (eg. for attack player, target will be player)
    public bool moveAgent(GOAPAction nextAction)
    {
        moving = true;


        float       step             = movementSpeed * Time.deltaTime;
        Rigidbody2D enemyBody        = gameObject.GetComponent <Rigidbody2D>();
        float       distanceToTarget = Vector3.Distance(transform.position, nextAction.target.transform.position);

        //move towards the object till you reach minimumDistance to Interact
        //function returns true when you reach the interactable range
        enemyBody.MovePosition(transform.position + Vector3.Normalize(nextAction.target.transform.position - transform.position) * movementSpeed * Time.deltaTime);


        if (distanceToTarget <= minimumDistanceToInteract)
        {
            nextAction.setInRange(true);
            moving = false;
            return(true);
        }
        else
        {
            return(false);
        }
    }
Ejemplo n.º 4
0
    public bool moveAgent(GOAPAction action)
    {
        //move to target
        Enemy       enemyScript = GetComponent <Enemy>();
        Rigidbody2D rb          = GetComponent <Rigidbody2D>();

        //flip
        if (action.target.transform.position.x - rb.transform.position.x > 0 && !enemyScript.facingRight)
        {
            enemyScript.Flip();
        }
        else if (action.target.transform.position.x - rb.transform.position.x < 0 && enemyScript.facingRight)
        {
            enemyScript.Flip();
        }

        rb.velocity = new Vector2(Mathf.MoveTowards(rb.velocity.x, rb.transform.localScale.x * enemyScript.maxSpeed, enemyScript.runForce * Time.deltaTime), rb.velocity.y);

        if (gameObject.transform.position.x.Equals(action.target.transform.position.x))
        {
            action.setInRange(true);
            return(true);
        }
        else
        {
            return(false);
        }
    }
Ejemplo n.º 5
0
    public virtual bool moveAgent(GOAPAction nextAction)
    {
        Vector3 moveDirection = workplace.transform.position - transform.position;

        Vector3 newPosition = moveDirection * Time.deltaTime * 0.3f;

        transform.position += newPosition;
        float dist = Vector3.Distance(transform.position, nextAction.target.transform.position);

        test++;

        /*
         *      if (dist < aggroDist) {
         *              //Vector3 moveDirection = player.transform.position - transform.position;
         *
         *              setSpeed (speed);
         *
         *              if (initialSpeed < terminalSpeed) {
         *                      initialSpeed += acceleration;
         *              }
         *
         *              //Vector3 newPosition = moveDirection * initialSpeed * Time.deltaTime;
         *              //transform.position += newPosition;
         *      }*/
        if (dist <= 1.5f)
        {
            nextAction.setInRange(true);
            return(true);
        }
        else
        {
            return(false);
        }
    }
Ejemplo n.º 6
0
 bool MovementCheck(float dist, GOAPAction nextAction)
 {
     /*NavMeshPath navMeshPath = new NavMeshPath();
      * bool canReach = agent.CalculatePath(nextAction.target.transform.position, navMeshPath);
      * if (navMeshPath.status == NavMeshPathStatus.PathInvalid)
      *  return false;*/
     if (dist < config.aggroDist && dist > config.attackDistance && !anim.GetBool("isWalking"))
     {
         anim.SetBool("isRolling", true);
         anim.speed        = Random.Range(0.8f, 1.2f);
         agent.destination = nextAction.target.transform.position;
         agent.isStopped   = false;
     }
     else if (dist < config.attackDistance * 2.5 && dist > config.attackDistance && !anim.GetBool("isRolling"))
     {
         anim.SetBool("isWalking", true);
         anim.speed        = Random.Range(0.8f, 1.2f);
         agent.destination = nextAction.target.transform.position;
         agent.isStopped   = false;
     }
     if (dist <= config.attackDistance)
     {
         //anim.SetBool("isRolling", false);
         anim.SetBool("isWalking", false);
         anim.speed      = 1;
         agent.isStopped = true;
         nextAction.setInRange(true);
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 7
0
    public override bool moveAgent(GOAPAction nextAction)
    {
        float dist = Vector3.Distance(this.transform.position, nextAction.target.transform.position);

        if (nextAction.requiesVision)
        {
            if (dist < config.aggroDist)
            {
                var teleporTo = BestPlaceToTeleport(nextAction.transform.transform.position);
                if (teleporTo != null)
                {
                    this.transform.position = teleporTo.position;
                }

                anim.Play("Move");
                anim.SetBool("isWalking", true);
                agent.destination = nextAction.target.transform.position;
                agent.isStopped   = false;
            }
            if (dist <= config.attackDistance)
            {
                agent.isStopped = true;
                nextAction.setInRange(true);
                return(true);
            }
            else
            {
                return(false);
            }
        }
        else if (!nextAction.requiesVision)
        {
            var teleporTo = BestPlaceToTeleport(nextAction.transform.transform.position);
            if (teleporTo != null)
            {
                this.transform.position = teleporTo.position;
            }

            agent.destination = nextAction.target.transform.position;
            agent.isStopped   = false;
            return(true);
        }
        else
        {
            anim.Play("Idle");
            return(false);
        }
    }
Ejemplo n.º 8
0
    public bool moveAgent(GOAPAction nextAction)
    {
        // To-do: Change this code to the pathfinding system
        float step = 1f * Time.deltaTime;

        gameObject.transform.position = Vector2.MoveTowards(gameObject.transform.position, nextAction.rTarget.transform.position, step);
        if (gameObject.transform.position.Equals(nextAction.rTarget.transform.position))
        {
            nextAction.setInRange(true);
            return(true);
        }
        else
        {
            return(false);
        }
    }
Ejemplo n.º 9
0
    public bool IsAgentInRange(GOAPAction nextAction)
    {
        if (nextAction.IsActionInterrupted(gameObject))
        {
            isActionInterrupted = true;
        }

        float dist = Vector3.Distance(transform.position, nextAction.target.transform.position);

        if (dist < nextAction.interactRange)
        {
            nextAction.setInRange(true);
            return(true);
        }
        else
        {
            return(false);
        }
    }
Ejemplo n.º 10
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();
            }
        };
    }