Ejemplo n.º 1
0
    // When hit by player receiving damage.
    public void ReceiveDamage(int amount)
    {
        emotion.AddFear(2);

        bloodParticle.Play();
        if (dead == false)
        {
            audioSource.PlayOneShot(hitSound);
            health -= amount;
        }
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        Stopwatch alarmWatch = new Stopwatch();

        alarmWatch.Start();
        if (agentScript.dead)
        {
            return;
        }

        decisionTimer += Time.deltaTime;

        if (playerScript.attackedAgent == this.gameObject)
        {
            underAttack = true;
            alarm       = true;
            emotion.AddAdrenaline(2);
        }
        if (playerScript.attackedCover == coverFinder.currentCover && coverFinder.nearCover)
        {
            coverUnderAttack = true;
            alarm            = true;
            emotion.AddFear(1);
            emotion.AddAdrenaline(2);
        }
        if (agentScript.ammo <= 0)
        {
            outOfAmmo = true;
            alarm     = true;
        }

        if (alarm && alarmCoolDown >= 0)
        {
            alarmCoolDown -= Time.deltaTime;
        }
        else
        {
            alarm            = false;
            alarmActionTaken = false;
            alarmCoolDown    = 5;
        }

        if (agentScript.playerVisible)
        {
            attack = true;
            alarm  = true;
        }

        if (agentScript.attackCover && playerScript.playerCover == null)
        {
            agentScript.attackCover = false;
            agentScript.shoot       = false;
        }

        // Stoping the loop if alarm is not on or if the not enough time elapsed to make decision
        if (alarm == false && decisionSpeed > decisionTimer)
        {
            return;
        }
        else
        if (decisionSpeed <= decisionTimer)
        {
            ClearActions();
        }

        // ALARMS
        if (underAttack)
        {
            // Checking which cover is closer to agent, previous or the chosen one
            if (coverFinder.currentCover != null && coverFinder.nextCover != null)
            {
                if (coverFinder.nearCover == false)
                {
                    if (Vector3.Distance(gameObject.transform.position, coverFinder.currentCover.transform.position) < Vector3.Distance(gameObject.transform.position, coverFinder.nextCover.transform.position))
                    {
                        returnToCover = true;
                    }
                    else
                    {
                        moveToCover = true;
                    }
                }
            }
            else
            if (coverFinder.nextCover == null)
            {
                moveToCover = true;
            }

            hide = true;
        }

        if (coverUnderAttack)
        {
            agentScript.ChangeActionText("Cover under attack - Hiding");
            hide   = true;
            attack = false;
            movementScript.crouch = true;
        }

        if (outOfAmmo)
        {
            agentScript.ChangeActionText("Reloading");
            agentScript.reload       = true;
            attackPlayer             = false;
            agentScript.attackPlayer = false;
            attackPlayerCover        = false;
            agentScript.attackCover  = false;
            attack = false;

            hide = true;
        }

        if (coverFinder.nearCover && hide && attack == false)
        {
            if (coverFinder.currentCover != null)
            {
                if (coverFinder.currentCover.CompareTag("Small Object"))
                {
                    movementScript.crouch = true;
                }
            }
        }

        alarmWatch.Stop();
        //UnityEngine.Debug.LogWarning("Agent " + this.name + ": Alarms in DecisionMakingScript took " + alarmWatch.Elapsed + "ms to check alarms");

        // END OF ALARMS

        Stopwatch decisionWatch = new Stopwatch();

        decisionWatch.Start();
        // Decisions tree 1
        if (alarmActionTaken == false)
        {
            firstTreeDecision = Random.Range(1, 11);
        }

        switch (emotion.currentState)
        {
        case EmotionScript.States.confidentState:
            if (firstTreeDecision < 5)
            {
                attack = true;
            }
            else if (firstTreeDecision >= 5 && firstTreeDecision < 8)
            {
                move = true;
            }
            else
            {
                defend = true;
            }
            break;

        case EmotionScript.States.fearfulState:
            if (firstTreeDecision < 3)
            {
                attack = true;
            }
            else if (firstTreeDecision >= 3 && firstTreeDecision < 5)
            {
                move = true;
            }
            else
            {
                defend = true;
            }
            break;

        case EmotionScript.States.rageState:
            if (firstTreeDecision < 7)
            {
                attack = true;
            }
            else if (firstTreeDecision >= 7 && firstTreeDecision < 9)
            {
                move = true;
            }
            else
            {
                defend = true;
            }
            break;

        case EmotionScript.States.determinedState:
            if (firstTreeDecision < 5)
            {
                attack = true;
            }
            else if (firstTreeDecision >= 5)
            {
                move = true;
            }
            break;

        case EmotionScript.States.normalState:
            if (firstTreeDecision < 3)
            {
                attack = true;
            }
            else if (firstTreeDecision >= 3 && firstTreeDecision < 7)
            {
                move = true;
            }
            else
            {
                defend = true;
            }
            break;

        default:
            if (firstTreeDecision < 4)
            {
                attack = true;
            }
            else if (firstTreeDecision >= 4 && firstTreeDecision < 7)
            {
                move = true;
            }
            else
            {
                defend = true;
            }
            break;
        }

        if (alarmActionTaken == false)
        {
            secondTreeDecision = Random.Range(1, 4); // 33%
            alarmActionTaken   = true;
        }

        if (move)
        {
            // Attacking only if player is visible - moving and attacking
            if (agentScript.playerVisible)
            {
                attack = true;
            }

            if (secondTreeDecision == 1 && coverFinder.currentCover != null)
            {
                returnToCover = true;
            }
            else
            if (coverFinder.nextCover != null)
            {
                moveToCover = true;
            }
        }

        if (attack)
        {
            // Will try to attack the player first
            if (agentScript.playerVisible || secondTreeDecision > 1)
            {
                // Checking if player is near cover
                if (agentScript.playerVisible || (playerScript.nearCover && playerScript.crouching == false && playerScript.playerCover.CompareTag("Small Object")))
                {
                    attackPlayer = true;
                }
                else if (playerScript.playerCover != null && agentScript.playerCoverVisible)
                {
                    attackPlayerCover = true;
                }
            }
            else
            if (playerScript.playerCover != null && agentScript.playerCoverVisible)
            {
                attackPlayerCover = true;
            }
            else
            {
                UnityEngine.Debug.Log("Unabel to attack");
            }
        }

        if (move)
        {
            defend = false;
        }

        if (attack)
        {
            hide = false;
        }

        if (defend)
        {
            if (coverFinder.currentCover != null)
            {
                if (coverFinder.currentCover.CompareTag("Small Object") && coverFinder.nearCover && secondTreeDecision == 1)
                {
                    attackFromCover       = true;
                    movementScript.crouch = false;
                    hide = false;
                }
            }

            if (secondTreeDecision > 1)
            {
                hide = true;
            }
        }

        if (movementScript.inAir)
        {
            hide = false;
        }

        decisionWatch.Stop();

        //UnityEngine.Debug.LogWarning("Agent " + this.name + ": Decision making in DecisionMakingScript took " + decisionWatch.Elapsed + "ms to make decision");
        // END OF DECISIONS

        // Assigning decisions to scripts
        // AgentScript
        if (attackPlayer)
        {
            agentScript.attackPlayer = true;
        }

        if (attackPlayerCover)
        {
            agentScript.attackCover = true;
        }

        if (attackFromCover)
        {
            movementScript.crouch = false;

            if (agentScript.playerVisible)
            {
                agentScript.attackPlayer = true;
            }
            else
            if (playerScript.playerCover != null)
            {
                agentScript.attackCover = true;
            }
        }

        // MovementScript and CoverFinderScript
        if (hide == false)
        {
            movementScript.crouch = false;
        }
        else
        {
            if (coverFinder.nearCover == false)
            {
                moveToCover = true;
            }
            else
            {
                movementScript.crouch    = true;
                agentScript.shoot        = false;
                attackPlayer             = false;
                attackPlayerCover        = false;
                agentScript.attackCover  = false;
                agentScript.attackPlayer = false;
            }
        }

        // Don't stop moving to cover until it is reached
        if (coverFinder.nextCover != null)
        {
            moveToCover = true;
        }

        if (moveToCover)
        {
            if (CanIFlank())
            {
                int flankRand = Random.Range(1, 4); // 33%

                if (flankRand == 1)
                {
                    flankPlayer = true;
                }
                else
                {
                    flankPlayer = false;
                }
            }
            else
            {
                flankPlayer = false;
            }

            if (coverFinder.nextCover == null)
            {
                addCover = false;
                MoveToCover();
            }
        }
        else
        if (returnToCover)
        {
            returnToCover = false;
            ReturnToCover();
        }
        // END OF ASSIGNING
    }