Пример #1
0
    public void AILogic()
    {
        bool  foundPlayer = false;
        float distance    = Vector2.Distance(PlayerController.instance.getFocusObject().transform.position, transform.position);

        if (requireVisual)
        {
            foundPlayer = LookForPlayer();
        }

        if (requireDistance && foundPlayer == false)
        {
            if (distance <= hearingDistance)
            {
                foundPlayer = true;
                lastKnowLocationOfPlayer = PlayerController.instance.getFocusObject().transform.position;
            }
        }

        float directOfPlayer        = UtilityHelper.getAngleBetweenTwoPoints(PlayerController.instance.getFocusObject().transform.position, transform.position);
        float directOpositeToPlayer = UtilityHelper.getAngleBetweenTwoPoints(transform.position, PlayerController.instance.getFocusObject().transform.position);

        switch (brain)
        {
        case AIBrain.Chaser:

            if (foundPlayer)
            {
                agent.enabled = true;
                agent.SetDestination(lastKnowLocationOfPlayer);
                Debug.Log("Rat angle = " + directOfPlayer);
                mover.walk(directOfPlayer, false, true);   //face play and change
                currentCountForAttackRate -= Time.deltaTime;

                if (distance <= (agent.stoppingDistance - 0.1) && currentCountForAttackRate < 0)
                {
                    GameHandler.instance.audioSystem.playSoundEffect("hit2");
                    mover.playattackAnimation();
                    PlayerController.instance.attacker.DamageTarget(attacker.AttackDamage);
                    currentCountForAttackRate = AttackRate;
                    var claw_fx = Instantiate(GameHandler.instance.ClawFxPrefab);
                    claw_fx.transform.position = PlayerController.instance.getFocusObject().transform.position;
                }
            }

            break;

        case AIBrain.Scared:

            if (distance < fleeDistance)
            {
                agent.enabled = true;
                mover.walk(directOpositeToPlayer, true, true);    //fleeing move in the other direction face away
            }
            else
            {
                agent.enabled = false;
                mover.rigidbody2D.velocity = Vector2.zero;
                mover.state = Mover.characterState.Idle;
                mover.Animation();
            }
            break;
        }
    }