Example #1
0
    void Update()
    {
        if (worldSystem.shouldTick)
        {
            agent.enabled = true;
            float playerDistance = Vector3.Distance(transform.position, player.transform.position);
            remainingStunTime -= Time.deltaTime;
            if (startChargeTime >= chargeTime)
            {
                startChargeTime = 0f;
                stateMachine.SetPreviousStateFalse();
            }


            if (remainingStunTime <= 0)
            {
                stateMachine.stunned = false;
            }

            if (!stateMachine.seeingPlayer)
            {
                attackCooldown -= Time.deltaTime;
                huntTime       -= Time.deltaTime;

                if (huntTime <= 0f)
                {
                    if (!agent.pathPending && !stateMachine.stunned && agent.remainingDistance <= nextPointDistance)
                    {
                        if (waitTime == originalWaitTime)
                        {
                            IdleOnSpot();
                        }
                        waitTime -= Time.deltaTime;
                        if (waitTime < 0)
                        {
                            GoToNextWaypoint();
                            stateMachine.SetPreviousStateFalse();
                            stateMachine.patrolling = true;
                            waitTime = originalWaitTime;
                        }
                    }
                }

                if (playerLightComponent.lightSource.enabled && !stateMachine.stunned && playerDistance <= fleeTorchDistance)
                {
                    Retreating();
                    if (agent.remainingDistance <= 0.5)
                    {
                        waitTime = originalWaitTime;
                    }
                }

                for (int i = 0; i < lightBombs.Length; i++)
                {
                    if (lightBombs[i].activeInHierarchy)
                    {
                        Retreating();
                        if (agent.remainingDistance <= 0.5)
                        {
                            waitTime = originalWaitTime;
                        }
                    }
                }
            }
            if (stateMachine.seeingPlayer && !stateMachine.stunned)
            {
                attackCooldown -= Time.deltaTime;
                agent.SetDestination(player.transform.position);
                if (attackCooldown <= 0f)
                {
                    if (agent.remainingDistance > chargeDistance)
                    {
                        StalkPlayer();
                    }
                    if (agent.remainingDistance <= chargeDistance && agent.remainingDistance > attackDistance && stateMachine.stalking && startChargeTime < chargeTime ||
                        agent.remainingDistance <= chargeDistance && agent.remainingDistance > attackDistance && stateMachine.attacking && startChargeTime < chargeTime)
                    {
                        ChargePlayer();
                        startChargeTime += Time.deltaTime;
                    }
                    if (agent.remainingDistance <= attackDistance && stateMachine.charging)
                    {
                        AttackPlayer();
                    }
                }

                if (playerLightComponent.lightSource.enabled && playerDistance <= fleeTorchDistance)
                {
                    agent.speed = originalWalkSpeed;
                    Retreating();
                    if (agent.remainingDistance <= 0.5)
                    {
                        waitTime = originalWaitTime;
                    }
                }

                for (int i = 0; i < lightBombs.Length; i++)
                {
                    if (lightBombs[i].activeInHierarchy)
                    {
                        Retreating();
                        if (agent.remainingDistance <= 0.5)
                        {
                            waitTime = originalWaitTime;
                        }
                    }
                }
            }

            if (playerCombatComponent.isSwiping && playerSwipeDistance >= playerDistance)
            {
                Stunned();
            }
        }
        if (!worldSystem.shouldTick)
        {
            agent.enabled = false;
        }
    }