Пример #1
0
    // Update is called once per frame
    void Update()
    {
        // Debug.Log(state);
        switch (state)
        {
        case State.IDLE:
            detectedTarget = false;
            body.velocity  = new Vector2(0f, 0f);
            break;

        case State.PATROLING:
            speed = 1;
            Debug.Log("PATROLING");
            if (choseAnotherRandomPosition)
            {
                Debug.Log("getting random path");
                randomPosition = (Vector3)Random.insideUnitCircle * 3 + transform.position;
                GetPath(randomPosition);
                choseAnotherRandomPosition = false;
            }
            GoToTarget(path[0]);
            float patrolDistance = PathFinder.ManhattanDistance(transform.position, path[0]);
            if (patrolDistance <= 0.1f)
            {
                Debug.Log("already at point");
                path.RemoveAt(0);
            }
            if (path.Count <= 2)
            {
                choseAnotherRandomPosition = true;
            }
            break;

        case State.GETPATH:
            path      = PathFinder.GetPath(transform.position, targetPosition);
            pathIndex = 0;
            state     = State.FOLLOWING;
            break;

        case State.FOLLOWING:
            speed = 2;
            GoToTarget(path[0]);
            float distance = PathFinder.ManhattanDistance(transform.position, path[0]);
            if (distance <= 0.1f && path.Count >= 2)
            {
                path.RemoveAt(0);
                Debug.Log("path count = " + path.Count);
            }
            recalculationTimer -= Time.deltaTime;
            if (recalculationTimer <= 0)
            {
                path = PathFinder.GetPath(transform.position, player.GivePosition());
                recalculationTimer = 2;
                playerDistance     = PathFinder.ManhattanDistance(transform.position, player.GivePosition());
            }
            if (path.Count == 1)
            {
                Debug.Log("GOT TO ZERO");
                path = PathFinder.GetPath(transform.position, player.GivePosition());
            }
            if (playerDistance >= 10f)
            {
                choseAnotherRandomPosition = true;
                state = State.PATROLING;
            }
            if (playerDistance <= 1f)
            {
                Debug.Log("YOOOOOOOOO");
                state = State.ATTACKING;
            }

            if (player == null)
            {
                Debug.Log("PLAYER NOT HERE");
                choseAnotherRandomPosition = true;
                player         = FindObjectOfType <PlayerController>();
                detectedTarget = false;
                state          = State.PATROLING;
            }
            break;

        case State.ATTACKING:
            // Debug.Log("ATTACKING");
            Debug.Log("close enough");
            body.velocity  = (player.GivePosition() - transform.position).normalized * speed;
            playerDistance = PathFinder.ManhattanDistance(transform.position, player.GivePosition());
            if (playerDistance >= 1f)
            {
                path  = PathFinder.GetPath(transform.position, player.GivePosition());
                state = State.FOLLOWING;
            }

            // StopMovement();
            //attackTimer -= Time.deltaTime;
            //if(attackTimer<=0)
            //{
            //    EnemyAttack.Attack();
            //    attackTimer = attackTime;
            //}

            break;
        }
        if (player == null)
        {
            Debug.Log("PLAYER NOT HERE");
            choseAnotherRandomPosition = true;
            player         = FindObjectOfType <PlayerController>();
            detectedTarget = false;
            state          = State.PATROLING;
        }
        //Debug.Log("XVELOCITY =" + xVelocity);
        if (xVelocity * speed != 0 || yVelocity * speed != 0)
        {
            anim.SetBool("isRunning", true);
        }
        else
        {
            anim.SetBool("isRunning", false);
        }

        if (xVelocity > 0 && !facingLeft)
        {
            facingLeft  = true;
            facingRight = false;
            anim.transform.Rotate(0, 180, 0);
        }
        if (xVelocity < 0 && !facingRight)
        {
            facingRight = true;
            facingLeft  = false;
            anim.transform.Rotate(0, 180, 0);
        }
    }